i want to encript a byte array following is mycode it works fine but gives an output newByteArray of long size i want outputed array should be of size 16 is it possible
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));
here is the output Encrypted Array : [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]
最佳答案
下面的代码在这里工作正常:
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)
但是这是预期的,因为没有使用填充。如果这是您遇到的异常(而不是您的问题所说的“无效 key ”),请修复输入数据的长度。否则,请将准确且完整的错误堆栈跟踪粘贴到您的问题中。
关于java - 如何在java中的AES加密中将加密数组截断为大小16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15430085/