How To Encrypt And Decrypt Data In ReactJs

In this article, we will learn how to encrypt and decrypt data in reactjs.

-we will use the crypto-js package to encrypt and decrypt data. the crypto-js library is used to secure our information from malicious users.

A well-known and reliable encryption technique for encrypting data is called Advanced Encryption Standard (AES).  Additionally, Crypto-js offers the ability to deeply encrypt and decode objects.

Let’s build a React application.

–First of all, we have create react application.

–Then create an App.js file inside the src directory.

-After creating the App.js application, Install the required module using the below command.

npm install crypto-js

-Now open your app.js file and add the below code:

import React from 'react';
import CryptoJS from 'crypto-js';

export default function App() {
  let encryptData = CryptoJS.AES.encrypt(
    'this is your text!!',
    'myKey'
  ).toString();
  let bytes = CryptoJS.AES.decrypt(encryptData, 'myKey');
  let decryptData = bytes.toString(CryptoJS.enc.Utf8);
  return (
    <>
      <p>Encrypted Text:</p>
      {encryptData}
      <p>Decrypted Text:</p>
      {decryptData}
    </>
  );
}

-In order to verify that the secret key is real, it encrypts our data in encrypt data, which we then decrypt into a server file.

output:

-These three encryption standards are supported by Crypto-JS. Depending on the size of the key you enter, it will choose the variant. Additionally, Crypto-JS supports the hashing algorithms MD, SHA-1, and SHA-2.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories