Reputation: 1207
I have already use below library for encryption and decryption in iOS.
https://github.com/dev5tec/FBEncryptor
Now I want same functionality in Android. Is there any support for Android also? If not then how can I use this library to fulfil my need in Android also or please suggest another other encryption library that work same as FBEncryptor.
I have implemented the following code.
public class AESHelper {
private final Cipher cipher;
private final SecretKeySpec key;
private AlgorithmParameterSpec spec;
private static final String KEY = "VHJFTFRGJHGHJDhkhjhd/dhfdh=";
public AESHelper() throws Exception {
byte[] keyBytes = KEY.getBytes("UTF-8");
Arrays.fill(keyBytes, (byte) 0x00);
cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
key = new SecretKeySpec(keyBytes, "AES");
spec = getIV();
}
public AlgorithmParameterSpec getIV() {
final byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x00);
return new IvParameterSpec(iv);
}
public String encrypt(String plainText) throws Exception {
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");
return encryptedText;
}
public String decrypt(String cryptedText) throws Exception {
cipher.init(Cipher.DECRYPT_MODE, key, spec);
byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);
byte[] decrypted = cipher.doFinal(bytes);
String decryptedText = new String(decrypted, "UTF-8");
return decryptedText;
}
}
But it throw javax.crypto.BadPaddingException: Pad Block Corrupted
Upvotes: 3
Views: 129
Reputation: 1207
finally i have found solution for my own question.
For encryption in android you can use https://gist.github.com/m1entus/f70d4d1465b90d9ee024.
This class work same as like https://github.com/dev5tec/FBEncryptor in ios.
Upvotes: 3