I have been trying to encrypt a string with AES, CBC and PKCS#7 on Android for about 2 days now, I cannot get it to work! I have a shared key that I want to encrypt the string with. This is my code:
My method cutArray at line 4 looks like this:
The reason for this method is that if I dont use it, I get an exception because my key isnt 128, 196 or 256 bits. And now that I do cut my array in size, my output is messed up. I
Code:
String plainText = "24124124123";
String pwd = "BobsPublicPassword";
byte[] key = pwd.getBytes("UTF-16");
key = cutArray(key, 16);
byte[] input = plainText.getBytes("UTF-16");
byte[] output = null;
SecretKeySpec keySpec = null;
keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
output = cipher.doFinal(input);
My method cutArray at line 4 looks like this:
Code:
private static byte[] cutArray(byte[] arr, int length){
byte[] resultArr = new byte[length];
for(int i = 0; i < length; i++ ){
resultArr[i] = arr[i];
}
return resultArr;
}
The reason for this method is that if I dont use it, I get an exception because my key isnt 128, 196 or 256 bits. And now that I do cut my array in size, my output is messed up. I