I am trying to encrypt a message from android then decrypt it in web.
Firstly, I generate a key using Javascript and store it in my database
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i <10; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
secondaryDatabase.ref().update({
[displayName]:text
})
Secondly, I encrypt the message in Java by retrieving the message in the input field and the key from the database.
message.setText(message);
String key = String.valueOf(dataSnapshot);
encryptedI = AESCrypt.encrypt(key,message);
For simplicity purpose, lets just take it that message is "hello world " and the key is password
String message = "hello world";
String key = "password";
encryptedI = AESCrypt.encrypt(key,message);
Thirdly, i stored the encrypted message in the database.
mRef.child(uid).child("encryptedmessage").setValue(encryptedI);
Lastly, I decrypted the message using the key both retrieved from database with Cryto-js on javascript but it returns a empty string
var decrypted = CryptoJS.AES.decrypt(message,key);
var decryptedvalue=decrypted.toString(CryptoJS.enc.Utf8)
I have checked that the key and encrypted message used is the same, I even decrypted in Java to make sure that the encryption is done correctly. I used https://github.com/scottyab/AESCrypt-Android for encryption in Android while cryto-js for decryption in javascript
The question is why does it returns a empty string and how can i solve it.
Rfc2898DeriveBytes.