2

处理一些加密货币并遇到数组越界异常。我在纸上追踪了几次,对我来说一切似乎都很好,所以我无法真正确定错误的根源。如果有人可以提供帮助,那就太棒了!

   static byte[] encrypt(byte[] ptBytes, javax.crypto.SecretKey key, byte[] IV){

    byte [] ct; 
    byte [] pt;
    byte [] ptBlock, ctBlock;
    int bytes;              //the number of bytes left over in the last block // this comes into play w/ the last 2 blocks witht the swap and stuff

    //get the extra bytes in case last block of plain text isn't whole
    bytes = ptBytes.length%16;

    //pad the plain text array to proper length
    pt = Arrays.copyOf(ptBytes, (((ptBytes.length )/16) + 1) * 16 );
    System.out.println(Arrays.toString(pt));

    //ctBlock = one block of cipher text
    ctBlock = new byte [16];

    //make ct the length of the padded pt 
    ct = new byte [pt.length];

    //do the encryption
    //i is for the current block of plain / cipher text we are on
    for( int i = 1; i <= ((ptBytes.length )/16)+1; i++){
        if( i == 1 ){

            //make ptBlock the first block of the entire plain text
            ptBlock = Arrays.copyOfRange(pt, 0, (i*16));

            //since i = 1 do the XOR to get new plain text with IV
            for (int j = 0; j < ptBlock.length - 1; j++){
                ptBlock[j] = (byte)(ptBlock[j] ^ IV[j]);
            }

            //now time to do the encryption between the current block of plain text and the key
            try {
                ctBlock = AES.encrypt(ptBlock, key);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //now put the cipher text block we just got into the final cipher text array
            for( int k = 0; k < ctBlock.length; k++){
                ct[k] = ctBlock[k];
            }


        }
        else{
            //make ptBlock the current number block of entire plain text
            ptBlock = Arrays.copyOfRange(pt, (i-1)*16, (i*16));

            //now XOR the plain text block with the prior cipher text block
            for(int j = 0; j < ptBlock.length - 1; j++){
                ptBlock[i] = (byte) (ptBlock[j] ^ ctBlock[j]);
            }

            //now time to do the encryption between the current block of plain text and the key
            try {
                ctBlock = AES.encrypt(ptBlock, key);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //now put the cipher text block we just got into the final cipher text array
            for( int k = (i-1)*16; k < (i*16)-1; k++){
                ct[k] = ctBlock[k-16];
            }
        }
    }

    return ct;
}

它说错误在这一行

ct[k] = ctBlock[k-16];

这没有多大意义。数组 ct 的长度为 48,而 ctBlock 的长度为 16,如果此错误出现在 for 循环中,i 等于 2 或 3,因此我将一个大小为 16 字节的数组添加到第二个三分之一数组 ct 或第三个第三个。就像我说的那样,我在纸上追踪了它,它看起来很合法,所以 idk!

提前致谢!

4

2 回答 2

1

考虑当 时的情况i = 3

for( int k = (i-1)*16; k < (i*16)-1; k++){
    ct[k] = ctBlock[k-16];
}

这里 -

  • k 从 32 开始
  • 条件变为 32 < 47
  • 数组索引ctBlock变为 32 - 16 = 16,并且 bam! 数组索引超出范围!

快速解决 -

for( int k = (i - 1) * 16; k < (i * 16) - 1; k++){
    ct[k] = ctBlock[k - (16 * (i - 1))];
}
于 2013-03-27T04:33:04.317 回答
0

如果i == 3像你说的那样,那么k == 32开始并增加到47。您说这ctBlock是一个大小为 16 的数组,在这种情况下,尝试16通过索引访问元素31会引发错误。

于 2013-03-27T04:32:16.697 回答