2013-06-25 85 views
1

我需要使用CBC Rijndael加密對文本進行編碼/解碼。Bouncycastle java編碼/解碼。缺少字節?

輸入: 力是強在這個looooooooooooooooooo000000000oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong字符串

編碼輸入:†'Ú½mÎ-「AŽyÝ¢ƒô] 5X-A;「BC†.Ìμ¼èüÈíÖXÈ*©Ã¼ç,hKBμ $híƒEu-ȸU¤'AÓÈÿ?æûä¸:OW?B>ÐZ¡ ,zëë(C'®5ÐixRÉp% ì%q'ÕQÚ5μッ

解碼輸入: 「的力量是強大的在這looooooooooooooooooo000000000ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo」 - 在地方結束的32位(字節值是0)

我錯過了最後的字節。誰能告訴我爲什麼?

這是我的代碼:

公共類BouncyDecoder {

byte[] IV = null; 
byte[] encryptionKey = null; 
Cipher cipher; 
SecretKeySpec key; 
BlockCipher blockCipher; 
ParametersWithIV _param; 

PaddedBufferedBlockCipher mode; 
int blockSize; 

public BouncyDecoder() { 
    Security.addProvider(new BouncyCastleProvider()); 
    try { 
     IV = "1234567891234567891234567891234".getBytes("UTF-8"); 
     encryptionKey = "1234567891123453456789123456781".getBytes("UTF-8"); 

     blockCipher = new CBCBlockCipher(new RijndaelEngine(256)); 
     _param = new ParametersWithIV(new KeyParameter(encryptionKey), IV); 
     mode = new PaddedBufferedBlockCipher(blockCipher); 
     blockSize = blockCipher.getBlockSize(); 
    } catch (Exception e) { 
    } 
} 

public byte[] decrypt(byte[] encodedText) { 
    byte[] decoded = new byte[mode.getOutputSize(encodedText.length)]; 
    try { 
     mode.init(false, _param); 

     int bytesProcessed = 0; 
     int i=0; 
     for (i = 0; i < (encodedText.length/32) ; i++){    
      bytesProcessed += mode.processBytes(encodedText, i * blockSize, blockSize, decoded, bytesProcessed); 
     } 

     mode.doFinal(decoded, (i-1)*blockSize); 
    } catch (Exception e) { 
    } 
    return decoded; 
} 

public byte[] encrypt(byte[] normalText) { 
    byte[] encryptedText = new byte[mode.getOutputSize(normalText.length)]; 
    try { 
     mode.init(true, _param); 

     int bytesProcessed = 0; 
     int i=0; 
     for (i = 0; i < (normalText.length/32); i++) { 
      bytesProcessed += mode 
        .processBytes(normalText, i * blockSize, blockSize, encryptedText, bytesProcessed); 
     } 

     mode.doFinal(encryptedText, (i-1)*blockSize); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return encryptedText; 
} 

}

+1

我的臉我的臉ᵒh神,不,不拿去ONΘ停止安排* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ŝ –

回答

1

你的循環似乎並不過程都在輸入字符串中的字節:

for (i = 0; i < (normalText.length/32); i++) { 
     bytesProcessed += mode 
       .processBytes(normalText, i * blockSize, blockSize, encryptedText, bytesProcessed); 
} 

它只處理字節m 0至(text.Length/32)*blockSize

所以如果輸入數組的長度是35字節,最後3個字節永遠不會得到處理。

怎麼樣使用這樣的事情,而不是:

bytesProcessed = mode.processBytes(normalText, 0, normalText.length, encryptedText,0); 
//second argument of doFinal is offset in output buffer. 
mode.doFinal(encryptedText, bytesProcessed); 

如果這個人是去工作,你一定會知道,這個問題是差一錯誤的循環計數器。

UPDATE:或者你可以嘗試這樣的事情,如果你想要一個塊同時加密:

for(int i=0; i<=(normalText.length/blockSize); i++) { 
    int offset = i*blockSize; 
    //To handle last block of bytes in input 
    int len = Math.min(blockSize,normalText.length-offset); 
    bytesProcessed += mode.processBytes(normalText,offset,len,encryptedText,bytesProcessed); 
} 
mode.doFinal(encryptedText, bytesProcessed); 

也是一樣的解密

+0

THX!它做到了。 – Flavian