Use the JS method to do a simple local data encryption and decryption

Look at the code first, just copy and use, (you will see the following effect)

Describe the picture

var name = '123' //实现可以加解密汉字的window方法
console.log(name + '<==将要被加密')
let namejiami = window.btoa(window.encodeURIComponent(name))
//账号加密
console.log(namejiami + '<==加密信息')
let namejiemi = window.decodeURIComponent(window.atob(namejiami))
//账号解密
console.log(namejiemi + '<==解密信息')

btoa and atob implement an encryption and decryption method (the essence of which is base64 encoding), but they cannot handle Chinese characters, so we can use the decodeURIComponent method to do the processing.

Note that this approach is only a simple process and does not expose the information to the user in plaintext. If it is to defend against attacks, it is not recommended to use

The above is the native encryption and decryption method for windows. This is just a way of thinking. You can also use the methods provided by ES6 such as 16code to implement a simple encryption as local storage, which slightly improves security.
You can leave a message for other questions about js or where you don't understand this method, and I will reply and help you solve it as soon as possible.

Guess you like

Origin blog.csdn.net/weixin_47821281/article/details/108993490