Encrypt your Local Storage data!
Tags: javascript, webdev, beginners, frontend
Published: 2023-06-17 15:53:52

![Encrypt Local Storage in the Browser](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uf9dmx7tcrfgsdip7ebt.png)

Before going further I would suggest you first check out my article on the [prototypes](https://hashnode.com/post/ckx8z1jjn08nsucs1e9526lkh). Yes, we will be using the prototype concept to add our custom encryption functions to the local storage API. Let's see how we can achieve this.

The localStorage function is available globally on the browser's window object. And if you have ever noticed, this function has the type Storage.

![example](https://cdn.hashnode.com/res/hashnode/image/upload/v1684003241483/fc045ec5-3a03-4e77-bdbe-35e3a485a801.png)

If you check the function definition you will notice that this Storage is an interface.

![example](https://cdn.hashnode.com/res/hashnode/image/upload/v1684003461831/807e75da-1d2b-4199-8a54-f93afd02ffab.png)

Now, we got to know that the localStorage has a type of Storage interface, we can manipulate the prototype object of the Storage interface and add our encryption/decryption custom functions to it.

## Adding custom functions in the Storage Interface

We will use the `crypto-js` library for encrypting/decrypting our data. We will be using a node package manager (npm) to download this library so make sure your project is initialised by npm first.

Now install the library using the command `npm install crypto-js`. It will be added to the node\_modules folder. We will be adding two functions named `encrypt` and `decrypt` which will handle the encryption and decryption of data.

![example](https://cdn.hashnode.com/res/hashnode/image/upload/v1684004859570/4e474c22-ef3e-4334-8054-c0c8bb913a5d.png)

Here we have taken a secret key which is passed to the functions of the crypto-js library to encrypt and decrypt the data. This secret key can be anything but it should be stored in a safe place for security.

Now, we will be adding our two custom functions to the prototype object of the Storage interface.

![example](https://cdn.hashnode.com/res/hashnode/image/upload/v1684005878654/f5a5ad01-8b8b-42b7-94ad-61ba69678f54.png)

`setEncryptedItem` and `getDecryptedItem` is added to the Storage interface and will be available to our native function `localStorage` once our code will be executed.

Note that, before using our added custom functions, we have to first store it in the prototype of the Storage interface and therefore as per our code we are required to call `manipulateLocalStorage` function first.

Our whole code will look like the following -

```javascript
import * as CryptoJS from 'crypto-js';

function storageEncryption() {
    /**
  * secret key should be stored in a safe place. This is only for a demo purpose.
  */
    let _key = "secret_key"

    function encrypt(txt) {
        return CryptoJS.AES.encrypt(txt, _key).toString();
    }

    function decrypt(txtToDecrypt) {
        return CryptoJS.AES.decrypt(txtToDecrypt, _key).toString(CryptoJS.enc.Utf8);
    }

    function manipulateLocalStorage() {
        Storage.prototype.setEncryptedItem = (key, value) => {
            localStorage.setItem(key, encrypt(value));
        };

        Storage.prototype.getDecryptedItem = (key) => {
            let data = localStorage.getItem(key) || "";
            return decrypt(data) || null;
        }
    }
 /**
  * First call this function to add our custom functions to the Storage interface
  * 
  */
    manipulateLocalStorage();
    /**
     * you can use the setEncryptedItem and getDecryptedItem functions
     * to encrypt and decrypt the data
     * */ 

    localStorage.setEncryptedItem("token", "12345");
    const token = localStorage.getDecryptedItem("token");
    console.log(token);
}
storageEncryption();

```

Let me show you how our data got stored in the browser's local storage.

![example](https://cdn.hashnode.com/res/hashnode/image/upload/v1684006944982/42dd698f-44a8-4a87-92e0-a6a78780e900.png)

You can see our token `12345` is unreadable. Now let's check the decrypted value that we have printed in our console.

![example](https://cdn.hashnode.com/res/hashnode/image/upload/v1684007069308/44b84f7f-2ae4-40d3-8d57-99ddbbebc41a.png)

Yes! it's our decrypted token. 😃