2010-12-01 7 views
5

나는 탄력이 성으로 AES 알고리즘을 사용하여 암호화 및 암호 해독을하고있는 중이 야J2ME AES 암호 해독 오류 (org.bouncycastle.crypto.InvalidCipherTextException : 패드 블록 손상은)

내 암호화 및 암호 해독이 작품을 좋아하지만 나에게 때 내 일반 오류를 제공

: 텍스트 크기는 비 해독 된 데이터

public static boolean setEncryptionKey(String keyText) 
{ 
    byte[] keyBytes = keyText.getBytes(); 

    key = new KeyParameter(keyBytes); 
    engine = new AESFastEngine(); 
    cipher = new PaddedBufferedBlockCipher(engine); 

    return true; 
} 

암호화를주고 심지어 때로는 더 큰

입니다3210

public static String encryptString(String plainText) 
{ 

     byte[] plainArray = plainText.getBytes(); 

     cipher.init(true, key); 
     byte[] cipherBytes = new byte[cipher.getOutputSize(plainArray.length)]; 
     int cipherLength = cipher.processBytes(plainArray, 0, plainArray.length, cipherBytes, 0); 
     cipher.doFinal(cipherBytes, cipherLength); 
     String cipherString = new String(cipherBytes); 
     return cipherString; 
    } 

암호 해독 :

public static String decryptString(String encryptedText) 
{ 

     byte[] cipherBytes = encryptedText.getBytes(); 
     cipher.init(false, key); 
     byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)]; 
     int decryptedLength = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0); 
     cipher.doFinal(decryptedBytes, decryptedLength); 
     String decryptedString = new String(decryptedBytes); 

     int index = decryptedString.indexOf("\u0000"); 
     if (index >= 0) 
     { 
      decryptedString = decryptedString.substring(0, index); 
     } 
     return decryptedString; 
    } 

이 암호 해독주고 나에게 다음과 같은 오류

org.bouncycastle.crypto.InvalidCipherTextException: pad block corrupted 
     at org.bouncycastle.crypto.paddings.PKCS7Padding.padCount(+30) 
     at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(+190) 
     at com.NewCrypto.decryptString(NewCrypto.java:103) 
     at com.New_Midlet.startApp(New_Midlet.java:23) 
     at javax.microedition.midlet.MIDletProxy.startApp(MIDletProxy.java:44) 
     at com.sun.midp.midlet.Scheduler.schedule(Scheduler.java:375) 
     at com.sun.midp.main.Main.runLocalClass(Main.java:477) 
     at com.sun.midp.main.Main.main(+80) 

무슨 문제가 될 수 있을까?

답변

2

라인

String cipherString = new String(cipherBytes); 

버그이다. cipherBytes은 임의의 값을 가진 바이트 배열이며 Java 문자열 디코더를 사용하여 문자열로 변환 할 수 없습니다. 암호는 바이트 배열로 보내거나 저장해야합니다. 문자열로 만들어야한다면 엔코더를 사용해야합니다. Base64 (16 진수)와 같이 Base64 인코더가 자주 사용됩니다. Apache Commons Codec 또는 내 즐겨 찾기 인 Harder Base64 codec을 사용할 수 있습니다.

+0

여전히 문자 대신 바이트를 출력하는 모든 base64 코더는 제 생각에는 약간 바보입니다. 누군가 UTF-16 XML 파일로 스트리밍하려고 할 때 이미 공포를 볼 수 있습니다. 또한, default64보다 다른 base64 형식을 지원하는 것 같지 않습니다. 음, 아마도 내 인코더를 사용할 수있게해야합니다. –

+0

@owlstead : 동의합니다. Harder 코덱은 문자열을 출력 할뿐만 아니라 바보 같은 Apache commons 스타일을 지원합니다. –

관련 문제