I am working on a function that requires Aes encrypted ciphertext (AES / CBC / PKCS5padding) to be sent from the client to the server from ASP.Net in the backend.
I have a server side decryption function as shown below:
public static string Decrypt(string inputBase64, string passphrase = null) { byte[] key, iv = new byte[0]; byte[] base64data = Convert.FromBase64String(inputBase64); byte[] passphrasedata = RawBytesFromString(passphrase); byte[] currentHash = new byte[0]; SHA256Managed hash = new SHA256Managed(); currentHash = hash.ComputeHash(passphrasedata); return DecryptStringFromBytes(base64data, currentHash, null); } static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV) {
I want to implement an alternative to angularjs for the following Android code:
public static String Encrypt(String input, String passphrase) { if (input.equalsIgnoreCase("") || passphrase.equalsIgnoreCase("")) return ""; else { byte[] key, iv; byte[] passphrasedata = null; try { passphrasedata = passphrase.getBytes("UTF-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } byte[] currentHash = new byte[0]; MessageDigest md = null; try { md = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } currentHash = md.digest(passphrasedata); iv = new byte[16]; return Base64.encodeToString(EncryptStringToBytes(input, currentHash, iv), Base64.NO_WRAP); } } static byte[] EncryptStringToBytes(String plainText, byte[] Key, byte[] IV) { if (plainText == null || plainText.length() <= 0) { Log.e("error", "plain text empty"); } if (Key == null || Key.length <= 0) { Log.e("error", "key is empty"); } if (IV == null || IV.length <= 0) { Log.e("error", "IV key empty"); } byte[] encrypted; try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec myKey = new SecretKeySpec(Key, "AES"); IvParameterSpec IVKey = new IvParameterSpec(IV); cipher.init(Cipher.ENCRYPT_MODE, myKey, IVKey); encrypted = cipher.doFinal(plainText.getBytes("UTF-8")); return encrypted; } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
Android code above is working fine. I want to implement the same encryption logic in AngularJs.
I have included the CryptoJS library for calculating SHA-256 and AES encryption. Here is the code I implemented.
var password = '12345678'; var passwordHash = CryptoJS.SHA256(password).toString(CryptoJS.enc.Latin1); var iv = CryptoJS.enc.Hex.parse('0000000000000000'); var cipher = CryptoJS.AES.encrypt(plaintext,passwordHash,{ iv: iv, mode: CryptoJS.mode.CBC, keySize: 256/32, padding: CryptoJS.pad.Pkcs7 }); cipherText = cipher.ciphertext.toString(CryptoJS.enc.Base64);
The problem is that the encoded string cannot be decrypted to its previous form. I think there is some inconsistency in the encryption logic on the client side and the decryption logic on the server side.
When I pass the CryptoJS encrypted cipher to the java decryption function, it shows errors:
javax.crypto.IllegalBlockSizeException: input length must be a multiple of 16 when decrypting with augmented encryption
or sometimes:
javax.crypto.BadPaddingException: this final block is not inserted correctly