AES CTR Encryption in Java under different Android Version

Multi tool use
Multi tool use


AES CTR Encryption in Java under different Android Version



I was trying to encode a string using Cipher with "AES/CTR/PKCS5PADDING" but I got different encrypted string (different in the length of the string as well) when the same coding is used in a physical android phone (android 4.4 API 19) and virtual android device (Nexus 5X API 26) under the same keygen. The virtual android device could generate string with 32bytes but the physical one couldn't (which has the same length of the string to be encoded).



Would it be the problem of cipher which is not supported for old version android?



Below is the testing coding used:


byte Input = "Testing123".getBytes();
byte encoded_key = Base64.decode("ABCDEFGHIJKLMNOPQABCDEFGHIJKLMNOPQABCDEFGHI", Base64.DEFAULT);
SecretKey secretKey = new SecretKeySpec(encoded_key, 0, encoded_key.length, "AES");
byte iv = new byte[128 / 8];
Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));
String Output = Base64.encodeToString(cipher.doFinal(Input), Base64.DEFAULT);



Under old android, output = "nG3YdbntTrRBxQ=="
while under new android, output = "nG3YdbntTrRBxQd2fUg1ow=="



Thanks~



Finally, i found out this is because old android doesnt support with padding encryption.





Are you using the same 'block-size'?
– n247s
Jul 2 at 5:12





How to set the block size? The string to be encoded are the same but not in 32 byte. It seems that padding is not working in some android version...
– Hiu leong Ng
Jul 2 at 5:13





So under the old physical android, the output byte is the same length of the input byte but its not enough as i will treat it as base 64 to convert it as string
– Hiu leong Ng
Jul 2 at 5:14





@n247s Block size of AES is fixed at 128 bits.
– Luke Joshua Park
Jul 2 at 5:40





Post your code. "Couldn't" is a very poor description of the problem.
– Luke Joshua Park
Jul 2 at 5:41









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

nOKeenPza0,RquLy32m7N,8
uh7L1y6gTIJvyETEUG

Popular posts from this blog

Rothschild family

Boo (programming language)