{
code: 200,
id: 4,
msg: "success",
user: "Sourav"
}
I have a issue like i want to store id and user in Local Storage as Encrypted format.How can i do it using Angular 6?
In one our project, we have used 'crypto-js' library. http://github.com/brix/crypto-js
import * as CryptoJS from 'crypto-js';
encryptData(data) {
try {
return CryptoJS.AES.encrypt(JSON.stringify(data), this.encryptSecretKey).toString();
} catch (e) {
console.log(e);
}
}
decryptData(data) {
try {
const bytes = CryptoJS.AES.decrypt(data, this.encryptSecretKey);
if (bytes.toString()) {
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
}
return data;
} catch (e) {
console.log(e);
}
}
The technical solution for encrypting things on client side is probably to use some third party library. Quoting such libraries leads to opinionated answers and that's not very desirable here.
However, if the use case is to hide some backend data from the user (which seems to be the case when I read your question), it makes no sense to encrypt, since the key would be either stored in JavaScript code or sent through network. In both cases it's impossible to obfuscate.
Some examples of valid use cases for client-side encryption:
Though it's not perfect, window.btoa()
will provide basic base-64
encoding, to avoid everyone reading the user data. This could be your quickest solution. As encryption on the client side is not secured, because everything that comes to the browser can be seen by the end user (Your code or Ajax call, etc), even your encryption key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With