Preface
Javascript and what I posted beforePython encryptionas well asGo encryption decryptionDifferent, there is no need to lead so many libraries
Only a few libraries need to be installed, among which the most crypto-js needs to be known
I won't introduce it in detail and go to the official website directly
/package/crypto-js
Install
npm install crypto-js --save-dev
npm install md5 --save-dev
1. Some common built-in function encryption
unescape
The unescape() function decodes strings encoded by escape().
let e = escape("Beginning") (e) // %u59CB%u8B58 let u = unescape(e) (u) // Beginning
URL encoding and decoding
let e = encodeURI("https://The first blog")(e) // https://%E5%A7%8B%E8%AD%98%E7%9A%84%E5%8D%9A%E5%AE%A2 let u = decodeURI(e) (u) // https://The first blog
fromCharCode
Convert Unicode encoding to one character
var n = (65); // A [101,118,97,108].map(item=>{ return (item) }) ['e', 'v', 'a', 'l']
2.Base64
btoa atob
let e = btoa("/zichliang/p/") (e) let u = atob(e) (u)
Reference crypto-js encryption module
var CryptoJS = require('crypto-js') function base64Encode() { var srcs = .(text); var encodeData = .(srcs); return encodeData } function base64Decode() { var srcs = .(encodeData); var decodeData = (.Utf8); return decodeData } var text = "/zichliang/p/" var encodeData = base64Encode() var decodeData = base64Decode() ("Base64 Encoding: ", encodeData) ("Base64 Decoding: ", decodeData) // Base64 encoding: aHR0cHM6Ly93d3cuY25ibG9NCy5jb20vemljaGxpYW5nL3AvMTcyNjU5NjAuaHRtbA==// Base64 decoding: /zichliang/p/
3.MD5
// Reference crypto-js encryption modulevar CryptoJS = require('crypto-js') function MD5Test() { var text = "/zichliang" return CryptoJS.MD5(text).toString() } (MD5Test()) // 50177badb579733de56b628ae57fb972
4.PBKDF2
// Reference crypto-js encryption modulevar CryptoJS = require('crypto-js') function pbkdf2Encrypt() { var text = "/zichliang" var salt = "1234567" // key length 128, 10 repeated operations var encryptedData = CryptoJS.PBKDF2(text, salt, {keySize: 128/32,iterations: 10}); return () } (pbkdf2Encrypt()) // bcda4be78de797d8f5067331b1a70d40
5.SHA1
// Reference crypto-js encryption modulevar CryptoJS = require('crypto-js') function SHA1Encrypt() { var text = "/zichliang" return CryptoJS.SHA1(text).toString(); } (SHA1Encrypt()) // ca481c13d5af7135b69d11ffb0a443a635fbc307
// Reference crypto-js encryption modulevar CryptoJS = require('crypto-js') function HMACEncrypt() { var text = "/zichliang" var key = "secret" return CryptoJS.HmacMD5(text, key).toString(); } (HMACEncrypt())// 20ca7a63f1f4a7047ffd6b722b45319a
// Reference crypto-js encryption modulevar CryptoJS = require('crypto-js') function desEncrypt() { var key = .(desKey), iv = .(desIv), srcs = .(text), // CBC encryption mode, Pkcs7 fill mode encrypted = (srcs, key, { iv: iv, mode: , padding: .Pkcs7 }); return (); } function desDecrypt() { var key = .(desKey), iv = .(desIv), srcs = encryptedData, // CBC encryption mode, Pkcs7 fill mode decrypted = (srcs, key, { iv: iv, mode: , padding: .Pkcs7 }); return (.Utf8); } var text = "/zichliang" // Object to be encryptedvar desKey = "0123456789ABCDEF" // Keyvar desIv = "0123456789ABCDEF" // Initial vector var encryptedData = desEncrypt() var decryptedData = desDecrypt() ("Encrypted string: ", encryptedData) ("Decrypt string: ", decryptedData) // Encrypted string: p+4ovmk1n5YwN3dq5y8VqhngLKW//5MM/qDgtj2SOC6TpJaFgSKEVg==// Decrypt string: /zichliang
8.3DES
// Reference crypto-js encryption modulevar CryptoJS = require('crypto-js') function tripleDesEncrypt() { var key = .(desKey), iv = .(desIv), srcs = .(text), // ECB encryption method, Iso10126 fill method encrypted = (srcs, key, { iv: iv, mode: , padding: .Iso10126 }); return (); } function tripleDesDecrypt() { var key = .(desKey), iv = .(desIv), srcs = encryptedData, // ECB encryption method, Iso10126 fill method decrypted = (srcs, key, { iv: iv, mode: , padding: .Iso10126 }); return (.Utf8); } var text = "/zichliang" // Object to be encryptedvar desKey = "0123456789ABCDEF" // Keyvar desIv = "0123456789ABCDEF" // Offset var encryptedData = tripleDesEncrypt() var decryptedData = tripleDesDecrypt() ("Encrypted string: ", encryptedData) ("Decrypt string: ", decryptedData) // Encrypted string: pl/nNfpIrejwK+/X87VmGZIbS3kOB+IpFcx/97wpR4AO6q9HGjxb4w==// Decrypt string: /zichliang
// Reference crypto-js encryption modulevar CryptoJS = require('crypto-js') function aesEncrypt() { var key = .(aesKey), iv = .(aesIv), srcs = .(text), // CBC encryption method, Pkcs7 filling method encrypted = (srcs, key, { iv: iv, mode: , padding: .Pkcs7 }); return (); } function aesDecrypt() { var key = .(aesKey), iv = .(aesIv), srcs = encryptedData, // CBC encryption method, Pkcs7 filling method decrypted = (srcs, key, { iv: iv, mode: , padding: .Pkcs7 }); return (.Utf8); } var text = "/zichliang" // Object to be encryptedvar aesKey = "0123456789ABCDEF" // Key, 16 timesvar aesIv = "0123456789ABCDEF" // Offset, 16 times var encryptedData = aesEncrypt() var decryptedData = aesDecrypt() ("Encrypted string: ", encryptedData) ("Decrypt string: ", decryptedData) // Encryption string: /q8i+1GN8yfzIb8CaEJfDOfDQ74in+XzQZYBtKF2wkAB6dM1qbBZ3HJVlY+kHDE3// Decrypt string: /zichliang
10.RC4
// Reference crypto-js encryption modulevar CryptoJS = require('crypto-js') function RC4Encrypt() { return CryptoJS.(text, key).toString(); } function RC4Decrypt(){ return CryptoJS.(encryptedData, key).toString(.Utf8); } var text = "/zichliang" var key = "12345678ASDFG" var encryptedData = RC4Encrypt() var decryptedData = RC4Decrypt() ("Encrypted string: ", encryptedData) ("Decrypt string: ", decryptedData) // Encrypted string: U2FsdGVkX19/bT2W57mzjwoF5Fc3Zb4WiyDU+MiNMmHfdJvZeScl0EW9yJWCPiRrsA==// Decrypt string: /zichliang
// Reference crypto-js encryption modulevar CryptoJS = require('crypto-js') function rabbitEncrypt() { return (text, key).toString(); } function rabbitDecrypt() { return (encryptedData, key).toString(.Utf8); } var text = "/zichliang/p/" var key = "1234567ASDFG" var encryptedData = rabbitEncrypt() var decryptedData = rabbitDecrypt() ("Encrypted string: ", encryptedData) ("Decrypt string: ", decryptedData) // Encrypted string: U2FsdGVkX1/pYbHvbNff3/RNpso4yRKIX0XDFta8hoLNxe52K8HSmF+XV8ayYqucTKVPP6AJtGczXS7U9kkxHnw=// Decrypt string: /zichliang/p/
Using node-rsa
Need to install a library
npm install node-rsa
// Reference node-rsa encryption modulevar NodeRSA = require('node-rsa'); function rsaEncrypt() { pubKey = new NodeRSA(publicKey,'pkcs8-public'); var encryptedData = (text, 'base64'); return encryptedData } function rsaDecrypt() { priKey = new NodeRSA(privatekey,'pkcs8-private'); var decryptedData = (encryptedData, 'utf8'); return decryptedData } var key = new NodeRSA({b: 512}); //Generate 512-bit keyvar publicKey = ('pkcs8-public'); //Export the public keyvar privatekey = ('pkcs8-private'); //Export private keyvar text = "/zichliang/p/" var encryptedData = rsaEncrypt() var decryptedData = rsaDecrypt() ("Public Key:\n", publicKey) ("Private Key:\n", privatekey) ("Encrypted string: ", encryptedData) ("Decrypt string: ", decryptedData) /* Public Key: -----BEGIN PUBLIC KEY---- MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAN7JoMDNvvpB/po2OMSeSKsromfP5EyI 0fAz6XDVwqdTUBwwAArLlqIzmVNK0yi4nlbj5eF+O8ZjRkRQ6xKP/CMCAwEAAQ== -----END PUBLIC KEY---- Private key: -----BEGIN PRIVATE KEY---- MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA3smgwM2++kH+mjY4 xJ5IqyuiZ8/kTIjR8DPpcNXCp1NQHDAACsuWojOZU0rTKLieVuPl4X47xmNGRFDr Eo/8IwIDAQABAkEArI0Ps6TnIJ9SmZAbYbWSZPjTvYHXuatSpq8eQ+Vb8Ql003G5 Y2FIoWpQX1jQ9/DsxEZ/1u+71bl08z1eONz2KQIhAPgLZOKanhDDaOn5sO7Y2RM3 TyLS08mCGNGQxEhkEttFAiEA5e7bvnrSNh1lcF/QTxkWPGoXb9kxPljm49CfiTS9 PEcCIDzxX7olTwzDVjWWeZhVgxArmK/vqMVrx3lF3lQC8ncZAiBlpY5nSoybd6tc Xj8MeJ6n3o6112I5mbuYgqXEVhhCCQIgY6vinhOzMF0dX9MNjBm8x1mUCd4XG2TN QQcOik3RIGw= -----END PRIVATE KEY---- Encrypted string: ZolvYwjFqOp1Yldui7rm75mSN5kz7533nc3B3H6xZGQR9v0elhbcjmI9vXaBsgdLNTuyoVk3bfzWfQdeIpvCpcBCTGe1HG9KrSBYDiWJc4vBgVBz8D57/XaS1zjM0kuAJ/ELu4os7XG5lMQbRbFhHXs7zQsIBq6/m2IZdGWx7HjB2jiQBQPMfszdQUOwQA bM5o7lRvUgdMVaZkEWpOTEybmUX4kxBP5CvNtB86oTRUw+U7Ex7QB8lWj33hoKvh70 Decryption string: /zichliang/p/ */
Use the built-in module crypto
const crypto = require('crypto'); const nodeRSA = require('node-rsa'); // Generate a 1024-length key pairconst key = new nodeRSA({b: 1024}); // Export the public keyconst publicKey = ('public'); // Export private keyconst privateKey = ('private'); const secret = '/zichliang/p/' // Use private key to encrypt, public key to decryptconst encrypt = (privateKey, (secret)); const decrypt = (publicKey, encrypt); ('After encrypted:', ('base64')); ('After decryption:', ());
Long encryption
This encryption is really troublesome, and it also needs to be imported.
Post a GitHub address here/wangqinglongDo/github_demo/blob/master/libs/
By the way, the environment needs to be supplemented. And decryption is not very useful. If anyone knows how to decrypt it, I hope to tell me in the comment section.
var encrypt = new JSEncrypt(); var publickKey = "-----BEGIN PUBLIC KEY-----\ MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLFb8qp1vRFvi/qfgi1Wg7Mi8l\ LcpfAc+tgpyD7aFW9QquQVMm/jG1IJZVQ6LsdkI7TiDutMCzOMCBXbdSC9BCIAGA\ L2Sz3cYVlGb1kYSM0ZMcUMIK5eF4Bptke070XHvbi8wArtysJ0l71RHDd786tNbG\ W0hDSw3zAqTErbxFaQIDAQAB\ -----END PUBLIC KEY-----\ " (publickKey); //Set the public key encryption certificatevar data = "/zichliang/p/"; var commonEncodeData = (data); // Ordinary encryption (commonEncodeData) var cnEscapeData = ((data)); //base64 Decrypted encryptionvar encryptData = (cnEscapeData); //Get encrypted data.(encryptData)
The above is the detailed content of the summary of common encryption and decryption methods in JavaScript. For more information about JavaScript encryption and decryption, please pay attention to my other related articles!