I managed to solve the problem. Decryption now works fine. Using the following code
String plainPassword = ""; try { SecretKeySpec key = new SecretKeySpec("hcxilkqbbhczfeultgbskdmaunivmfuo".getBytes("US-ASCII"), "AES"); IvParameterSpec iv = new IvParameterSpec("ryojvlzmdalyglrj".getBytes("US_ASCII")); Cipher cipher = Cipher.getInsta nce("AES/CBC/PKCS7Padding"); cipher.init(Cipher.DECRYPT_MODE, key, iv); byte[] encoded = cipher.doFinal(Base64.decodeBase64(encryptedPassword.getBytes())); plainPassword = new String(encoded); } catch (Exception ex) { Log.d("Decryption Error", ex.toString()); } return plainPassword;
Now the problem is encryption. I used the same code, except that I changed the cipher from decryption mode to encryption mode, but for some reason, when I print an encrypted string, it just prints a load of garbage that doesn't look like the string that C # creates. Below is the code for encryption
public String encrypt(String plainPasword) { String password = ""; try { SecretKeySpec key = new SecretKeySpec("hcxilkqbbhczfeultgbskdmaunivmfuo".getBytes("US-ASCII"), "AES"); IvParameterSpec iv = new IvParameterSpec("ryojvlzmdalyglrj".getBytes("US_ASCII")); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, iv); byte[] encoded = cipher.doFinal(plainPasword.getBytes()); password = new String(encoded); } catch (Exception ex) { Log.d("Encryption Error", ex.toString()); } return password; }
Something is wrong with this, I can not figure it out. Thanks
source share