My id is stored in sessionStorage and I want to encrypt my id. how can I encrypt in Vue 3 and best way to secure my id?
sessionStorage.setItem('id', error.response.data.user.id)
this is my id and I want this to store in sessionStorage in encryption and then get decrypted id.
You can use cryptoJS to achieve the encryption and decryption.
Demo :
var id = document.getElementById("userId"),
save = document.getElementById("save"),
read = document.getElementById("read");
// Manage Save event
save.addEventListener("click", function(e){
window.sessionStorage["userId"] = CryptoJS.AES.encrypt(id.value, 'secret key');
}, true);
// Manage Read Event
read.addEventListener("click", function(e) {
let decryptedId = CryptoJS.AES.decrypt(window.sessionStorage["userId"].toString(), 'secret key');
document.getElementById('showId').innerHTML = decryptedId.toString(CryptoJS.enc.Utf8);
}, true);
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<input type="text" id="userId" />
<input type="button" value="Save" id="save" />
<input type="button" value="Read" id="read" />
<p id="showId"></p>
Due to code snippet limitations its not able to accessing window object but you can take the reference.