0

我想在 mycode 之后编写一个字节数组,它工作正常,但给出一个长大小的输出 newByteArray 我希望输出数组的大小应该是 16 是否可能

 byte [] keyForEncription= new byte[16];
        byte [] keyForDecription= new byte[16];
        long  FixedKey= 81985526925837671L;
        long  VariableKey=744818830;
            for (int i1 = 0; i1 < 8; i1++)
        {

            keyForEncription[i1] = (byte)(FixedKey >> (8 * i1));
            keyForEncription[i1 + 8] = (byte)(VariableKey >> (8 * i1));
        }


    byte[] data = new byte[255];

    data[0]= 2;
    data[1]= 0;
    data[2]= 0;
    data[3]= 0;
    data[4]= 0;
    data[5]= 6;
    data[6]= 6;
    data[7]= 81;
    data[8]= 124;
    data[9]= 123;
    data[10]= 123;
    data[11]= 12;
    data[12]= 3;
    data[13]= 27;
    data[15]= 12;
    data[16]= 0;
    data[17]= 0;
    data[18]= 0;
    data[19]= 0;

    System.out.println("Original byte Array : " +Arrays.toString(data));

    SecretKeySpec skeySpec = new SecretKeySpec(keyForEncription, "AES");
   Cipher cipher1 = Cipher.getInstance("AES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] newByteArray = new byte[data.length];
    newByteArray = cipher.doFinal(byteArray);
    System.out.println("Encrypted Array : " +Arrays.toString(newByteArray));

这是输出加密数组: [110, -118, -119, -88, 73, -118, 57, 15, -52, -78, 100, 104, 102, -42, -102, -45, - 50, -116, -47, -53, 103, -40, -61, 62, 42, 15, -124, 98, -28, -77, 94, 78, -50, -116, -47, - 53、103、-40、-61、62、42、15、-124、98、-28、-77、94、78、-50、-116、-47、-53、103、-40、-61 , 62, 42, 15, -124, 98, -28, -77, 94, 78, -50, -116, -47, -53, 103, -40, -61, 62, 42, 15, -124 , 98, -28, -77, 94, 78, -50, -116, -47, -53, 103, -40, -61, 62, 42, 15, -124, 98, -28, -77, 94, 78]

4

1 回答 1

1

以下代码在这里可以正常工作:

    byte[] key = new byte[16];
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    byte[] data = "hello world.....".getBytes();
    byte[] encrypted = cipher.doFinal(data);
    System.out.println("Encrypted Array : " + Arrays.toString(encrypted));

但它不起作用"hello world".getBytes(),因为输入大小不是 16 的倍数:

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes
    at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:854)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:828)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
    at javax.crypto.Cipher.doFinal(Cipher.java:2086)
    at com.foo.bar.CryptoTest.main(CryptoTest.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

但这是意料之中的,因为没有使用填充。如果这是您得到的异常(而不是您的问题所说的“无效密钥”),那么请修复输入数据的长度。否则,将准确且完整的错误堆栈跟踪粘贴到您的问题中。

于 2013-03-15T10:47:00.830 回答