I need an easy solution for encrypt a string in AES from my React Native app, send to the server and the decrypt from there with Java. I found some solutions but I am not able to make it works... Thanks a lot!
1 Answer
For the Javascript/React Native side I've used crypto-js before which should just be a couple lines of code to implement.
npm install crypto-js
Then in your code
import AES from 'crypto-js/aes'
const stringToEncrypt = 'my string';
const secretKey = 'secret key 123';
const encryptedString = AES.encrypt(stringToEncrypt, secretKey).toString();
Then you can send encryptedString to your server, prefereably via https using something like Axios or Fetch
3 Comments
Flavio Spugna
Thx, almost all the examples I found with AES are using a secret key with salt and an Initialization vector. I try to replicate the same then in Java, but the encrypted strings I get are different. In your example, do you know how to read that in Java?
P. Brew
On reflection yes you will need a key to encrypt via AES. You could store this securely on the server and in your React Native code using environment variables to keep it secure. I have updated my answer accordingly. My Java isn't great so I would suggest looking up decryption using Cypher (import javax.crypto.Cipher) docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html
Rahul.S
In my case secret key is placed in a keystore file, how can i read and access key from it in order to use in react native to encrypt my plain text
I found some solutions but I am not able to make it works...please provide more details (e. g. code in React and in Java), otherwise we're guessing. Regardless that - are you trying something where https in not enough?