I am trying to implement AES128 encryption on Android. I have a solution running on iPhone with Objective-C, but I have problems porting to Android. I was looking for stackoverflow for a solution, but it looks like I'm doing something wrong. I am new to Java, so I think I have something to do with data, string conversion.
Here is my iPhone encrypted:
char keyPtr[kCCKeySizeAES128+1]; [keyString getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSASCIIStringEncoding];
Here is my Java:
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
Using the same key and plaintext in iPhone and Java gives different results. My iPhone result works the way I need, so I'm trying to get Java to give me the iPhone result. I missed something in Java, just not sure what it is.
change
Based on the suggestions below, I changed my Java to this
byte[] keyBytes = plainTextKey.getBytes("US-ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(plainText.getBytes("US-ASCII"));
but I am still getting different results between Android and iPhone.
source share