Greetings. For training purposes, I wrote a password storage program in Java using AES encryption. Wrote a class that uses methods from the javax.crypto package. In an application on Windows, everything works fine, encrypts and decrypts. I decided to make a mobile version of this application on Android, for aes I used the same class, but there was a problem - the data is not decrypted, throws an exception indicating a decryption error. Why it happens?
An exception:
javax.crypto.BadPaddingException: pad block corrupted
My class code:
public class AES { public static SecretKeySpec getSecretKeySpec(String data) { try { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(data.getBytes()); KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(128, sr); return (new SecretKeySpec((kg.generateKey()).getEncoded(), "AES")); } catch (Exception e) { System.out.println("ΠΡΠΈΠ±ΠΊΠ° ΠΏΡΠΈ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠ½ΠΎΠ³ΠΎ ΠΊΠ»ΡΡΠ°!"); } return null; } public static byte[] Encode(SecretKeySpec sks, byte[] data) { try { Cipher c = Cipher.getInstance("AES"); c.init(Cipher.ENCRYPT_MODE, sks); return c.doFinal(data); } catch (Exception e){ System.out.println("ΠΡΠΈΠ±ΠΊΠ° ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠΈΡΡΠΎΠ²Π°Π½ΠΈΡ!"); } return null; } public static byte[] Decode(SecretKeySpec sks, byte[] data) { try { Cipher c=Cipher.getInstance("AES"); c.init(Cipher.DECRYPT_MODE,sks); return c.doFinal(data); } catch (Exception e) { System.out.println("ΠΡΠΈΠ±ΠΊΠ° ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π΄Π΅ΡΠΈΡΡΠΎΠ²ΠΊΠΈ"); } return null; } }