数组超出范围将字节数组添加到另一个数组

时间:2013-03-27 04:22:44

标签: java arrays encryption

处理一些加密并遇到一个超出范围的数组异常。我在纸上描述了几次,所有对我来说都没问题,所以我无法确定错误的起源。如果有人能提供帮助那就太棒了!

   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为len 16,如果此错误出现在for循环中,则i等于2或3,因此我将大小为16字节的数组添加到第二个三分之一array ct或第3个。就像我说的那样我在纸上描述它似乎是合法的如此愚蠢!

提前致谢!

2 个答案:

答案 0 :(得分: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))];
}

答案 1 :(得分:0)

如果i == 3与您说的那样,那么k == 32开头,并增加到47。您说ctBlock是一个16号数组,在这种情况下,尝试访问标记1631的元素会引发错误。