简体   繁体   English

Bouncycastle Java编码/解码。 缺少字节?

[英]Bouncycastle java encoding/decoding. Missing bytes?

I need to encode/decode a text using CBC Rijndael encryption. 我需要使用CBC Rijndael加密对文本进行编码/解码。

Input: The force is strong in this looooooooooooooooooo000000000oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong String 输入:此oooooooooooooooooooooooo000000000oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong字符串中的力量很强

Encoded input: ?†´Ú½mΗ“AŽyÝ¢ƒô]5X-å;'Bdž.̵¼èüÈíÖXÈ*©Ã¼ç–hKBµ$híƒEu-ȸU ¤'AÓÈÿ?Ÿûä¸:OW?B>ÐZ²ñ ,zÅë(C'®5ÐixRópE%€.@vhrm6µ5©bŠ?Ç¡$q¿J^÷g“e†ì??bt ì%q'ÕQÚ5µã?ƒ 编码输入: ?†Ú½mÎ-“AŽyÝ¢ƒô]5X-å;'Bdž.̵¼èüÈíÖXÈ *©Ã¼ç–hKBµ $híƒEu-ȸU¤'AÓÈÿ?Ÿûä¸:OW?B>ÐZ²ñ,zÅë(C' ®5ÐixRópE%€。@ vhrm6µ5©bŠ?Ç¡qqJJ÷÷g” e†ì?? btì%q'ÕQÚ5µãƒ

Decoded input: "The force is strong in this looooooooooooooooooo000000000ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo " - and 32 spaces in place of the ending (byte value is 0) 解码后的输入: “此oooooooooooooooooooooooo000000000ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo中的力量很强-并以32个空格代替结尾(字节值为0)

I'm missing the final bytes. 我错过了最后的字节。 Can anyone tell me why ? 谁能告诉我为什么?

This is my code: 这是我的代码:

public class BouncyDecoder { 公共类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;
}

} }

Your loop doesn't seem process all of the bytes in input string: 您的循环似乎未处理输入字符串中的所有字节:

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

it only processes bytes from 0 to (text.Length/32)*blockSize . 它仅处理从0到(text.Length/32)*blockSize

So if length of input array is 35 bytes, last 3 bytes are never getting processed. 因此,如果输入数组的长度为35个字节,则最后3个字节将永远不会得到处理。

What about using something like this instead: 那用这样的东西呢:

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

If this one is going to work you'll definitely know that the problem is off-by-one error in loop counter. 如果这个程序可以正常工作,那么您肯定会知道问题出在循环计数器中是一个错误。

UPDATE: Or you can try something like this if you want to encrypt a block at a time: 更新:或者,如果您想一次加密一个块,则可以尝试这样的操作:

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);

Same goes for decryption 解密也一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM