Reputation: 91
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]
Upvotes: 0
Views: 806
Reputation: 692121
The following code works fine here:
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));
It won't work with "hello world".getBytes()
though, since the input size is not a multiple of 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)
Bute that's expected, since no padding is used. If that's the exception you get (and not "invalide key" as your question says), then fix the length of your input data. Else, paste the exact and complete error stack trace in your question.
Upvotes: 1