#2 AES Encryption and Decryption in React-Native vs .NET 7
Today, one of the most critical steps to take is to send data in a secure way. Because if we do not protect the privacy of sensitive information, then the information can be stolen easily by others. Thereby, It can cause a big problem.
Encryption and Decryption of information can save secrecy. What is the encryption and decryption?
Encryption - is the process of converting a standard message (plaintext) into a meaningless message (Ciphertext).
Decryption - ?is the process of converting a meaningless message (Ciphertext) into its original form (Plaintext).
We use AES encryption (advanced encryption standard) in the article to encrypt and decrypt our sensitive data. This encryption method is one of the best encryption protocols available. You can read more about it from AES Encryption.
In this article, We encrypt data in react-native (client) and send it to .NET 7 API (server) and then decrypt the cipher text to the plain text.
For the react-native side, crypto-js package is used for encryption and decryption. You can check the installation and configuration of crypto-js. In .NET, we do not need an additional package instead .NET uses System.Security.Cryptography for AES encryption.?
Let’s dive into code example:
Firstly, I create encryption.js file which responsible to carry out encrypting and decrypting in the client side (React-Native):
generateRandomIV() - generate a new random 16 bytes iv value every time to encrypt data
领英推荐
encrypt() - responsible to encrypt data
decrypt() - responsible to decrypt data
In the above, we encrypt “password” and call the “decrypt” api? method of server which get encrypted password.
For server side, We create “AesDecryptionHelper” to decrypt data that comes from client side:
Then, we create a simple api controller to get encrypted data sent by client and decrypt it with the help of the “DecryptData” method:
RegisterModel for the controller method:
As a conclusion, we see handling encryption and decryption process for both sides: client and server. By doing that, sensitive data can be transfer over HTTP and HTTPS securely.
Senior Frontend Developer | React, React Native, Javascript, TypeScript, C#, .NET
1 年it was helpful