I want to encrypted data in ionic, send it to server by a POST request and decrypted on server side. And also received encrypted data from server and decrypted on the app.
The one I'm currently testing is AES256, but it seems that the encrypted data is different. Which means that it isn't encrypted or decrypted the correct way once that it gives 2 different values. DO you know some way to make it possible?
encrypt(secureKey, secureIV, data) {
this.platform.ready().then(() => {
cordova.plugins.AES256.encrypt(secureKey, secureIV, data,
(encrypedData) => {
console.log('Encrypted Data----', encrypedData);
this.decrypt(this.secureKey, this.secureIV, encrypedData);
}, (error) => {
console.log('Error----', error);
});
});
}
decrypt(secureKey, secureIV, encryptedData) {
this.platform.ready().then(() => {
cordova.plugins.AES256.decrypt(secureKey, secureIV, encryptedData,
(decryptedData) => {
console.log('Decrypted Data----', decryptedData);
}, (error) => {
console.log('Error----', error);
});
});
}
Using this methods on ionic to encrypt and decrypt strings of code. But The value of those on android device is different from the server running on java. How can I make it, to run the same processes for encrypting and decrypting data on the device as on the server?
UPDATE / SOLVED
So, I ignore the previous plugin of AES 256 of cordova, and install CryptoJS libraryCryptoJS library.
this was the result:
var key = await CryptoJS.enc.Latin1.parse("12345678910123456789012345678901");
var iv = await CryptoJS.enc.Latin1.parse("1234567891123456");
console.log(key + " ; " + iv)
var encrypted = await CryptoJS.AES.encrypt("test", key, { iv: iv },
{
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs5
});
console.log("encrypted -> " + encrypted.ciphertext.toString(CryptoJS.enc.Base64))
I had to use .Latin1.parse in order to generate the same value in TypeScript/JavaScript as in java. More info, here