当前位置:   article > 正文

Javascript 加密解密方法_js 加密解密

js 加密解密

Javascript 和 我之前发的 python加密
以及 go加密
解密不一样 不需要导那么多的库
只需要安装几个库 其中需要了解最多的 crypto-js

具体就不多介绍了直接上官网crypto-js - npm

安装

  1. npm install crypto-js --save-dev
  2. npm install md5 --save-dev

一些常见的built-in 函数加密

unescape

unescape() 函数可对通过 escape() 编码的字符串进行解码。

  1. let e = escape("始識")
  2. console.log(e) // %u59CB%u8B58
  3. let u = unescape(e)
  4. console.log(u) // 始識

URL编码与解码

  1. let e = encodeURI("https://始識的博客")
  2. console.log(e) // https://%E5%A7%8B%E8%AD%98%E7%9A%84%E5%8D%9A%E5%AE%A2
  3. let u = decodeURI(e)
  4. console.log(u) // https://始識的博客

fromCharCode

将 Unicode 编码转为一个字符

  1. var n = String.fromCharCode(65);
  2. // A
  3. [101,118,97,108].map(item=>{
  4. return String.fromCharCode(item)
  5. })
  6.  ['e', 'v', 'a', 'l']

Base64

btoa atob

  1. let e = btoa("https://www.cnblogs.com/zichliang/p/17265960.html")
  2. console.log(e) // // https://%E5%A7%8B%E8%AD%98%E7%9A%84%E5%8D%9A%E5%AE%A2
  3. let u = atob(e)
  4. console.log(u) // https://www.cnblogs.com/zichliang/p/17265960.html

引用 crypto-js 加密模块

  1. var CryptoJS = require('crypto-js')
  2. function base64Encode() {
  3. var srcs = CryptoJS.enc.Utf8.parse(text);
  4. var encodeData = CryptoJS.enc.Base64.stringify(srcs);
  5. return encodeData
  6. }
  7. function base64Decode() {
  8. var srcs = CryptoJS.enc.Base64.parse(encodeData);
  9. var decodeData = srcs.toString(CryptoJS.enc.Utf8);
  10. return decodeData
  11. }
  12. var text = "https://www.cnblogs.com/zichliang/p/17265960.html"
  13. var encodeData = base64Encode()
  14. var decodeData = base64Decode()
  15. console.log("Base64 编码: ", encodeData)
  16. console.log("Base64 解码: ", decodeData)
  17. // Base64 编码: aHR0cHM6Ly93d3cuY25ibG9ncy5jb20vemljaGxpYW5nL3AvMTcyNjU5NjAuaHRtbA==
  18. // Base64 解码: https://www.cnblogs.com/zichliang/p/17265960.html

MD5

  1. // 引用 crypto-js 加密模块
  2. var CryptoJS = require('crypto-js')
  3. function MD5Test() {
  4. var text = "https://www.cnblogs.com/zichliang"
  5. return CryptoJS.MD5(text).toString()
  6. }
  7. console.log(MD5Test()) // 50177badb579733de56b628ae57fb972

PBKDF2

  1. // 引用 crypto-js 加密模块
  2. var CryptoJS = require('crypto-js')
  3. function pbkdf2Encrypt() {
  4. var text = "https://www.cnblogs.com/zichliang"
  5. var salt = "1234567"
  6. // key 长度 128,10 次重复运算
  7. var encryptedData = CryptoJS.PBKDF2(text, salt, {keySize: 128/32,iterations: 10});
  8. return encryptedData.toString()
  9. }
  10. console.log(pbkdf2Encrypt()) // bcda4be78de797d8f5067331b1a70d40

SHA1

  1. // 引用 crypto-js 加密模块
  2. var CryptoJS = require('crypto-js')
  3. function SHA1Encrypt() {
  4. var text = "https://www.cnblogs.com/zichliang"
  5. return CryptoJS.SHA1(text).toString();
  6. }
  7. console.log(SHA1Encrypt()) // ca481c13d5af7135b69d11ffb0a443a635fbc307

HMAC

  1. // 引用 crypto-js 加密模块
  2. var CryptoJS = require('crypto-js')
  3. function HMACEncrypt() {
  4. var text = "https://www.cnblogs.com/zichliang"
  5. var key = "secret"
  6. return CryptoJS.HmacMD5(text, key).toString();
  7. }
  8. console.log(HMACEncrypt())// 20ca7a63f1f4a7047ffd6b722b45319a

DES

  1. // 引用 crypto-js 加密模块
  2. var CryptoJS = require('crypto-js')
  3. function desEncrypt() {
  4. var key = CryptoJS.enc.Utf8.parse(desKey),
  5. iv = CryptoJS.enc.Utf8.parse(desIv),
  6. srcs = CryptoJS.enc.Utf8.parse(text),
  7. // CBC 加密模式,Pkcs7 填充方式
  8. encrypted = CryptoJS.DES.encrypt(srcs, key, {
  9. iv: iv,
  10. mode: CryptoJS.mode.CBC,
  11. padding: CryptoJS.pad.Pkcs7
  12. });
  13. return encrypted.toString();
  14. }
  15. function desDecrypt() {
  16. var key = CryptoJS.enc.Utf8.parse(desKey),
  17. iv = CryptoJS.enc.Utf8.parse(desIv),
  18. srcs = encryptedData,
  19. // CBC 加密模式,Pkcs7 填充方式
  20. decrypted = CryptoJS.DES.decrypt(srcs, key, {
  21. iv: iv,
  22. mode: CryptoJS.mode.CBC,
  23. padding: CryptoJS.pad.Pkcs7
  24. });
  25. return decrypted.toString(CryptoJS.enc.Utf8);
  26. }
  27. var text = "https://www.cnblogs.com/zichliang" // 待加密对象
  28. var desKey = "0123456789ABCDEF" // 密钥
  29. var desIv = "0123456789ABCDEF" // 初始向量
  30. var encryptedData = desEncrypt()
  31. var decryptedData = desDecrypt()
  32. console.log("加密字符串: ", encryptedData)
  33. console.log("解密字符串: ", decryptedData)
  34. // 加密字符串: p+4ovmk1n5YwN3dq5y8VqhngLKW//5MM/qDgtj2SOC6TpJaFgSKEVg==
  35. // 解密字符串: https://www.cnblogs.com/zichliang

3DES

  1. // 引用 crypto-js 加密模块
  2. var CryptoJS = require('crypto-js')
  3. function tripleDesEncrypt() {
  4. var key = CryptoJS.enc.Utf8.parse(desKey),
  5. iv = CryptoJS.enc.Utf8.parse(desIv),
  6. srcs = CryptoJS.enc.Utf8.parse(text),
  7. // ECB 加密方式,Iso10126 填充方式
  8. encrypted = CryptoJS.TripleDES.encrypt(srcs, key, {
  9. iv: iv,
  10. mode: CryptoJS.mode.ECB,
  11. padding: CryptoJS.pad.Iso10126
  12. });
  13. return encrypted.toString();
  14. }
  15. function tripleDesDecrypt() {
  16. var key = CryptoJS.enc.Utf8.parse(desKey),
  17. iv = CryptoJS.enc.Utf8.parse(desIv),
  18. srcs = encryptedData,
  19. // ECB 加密方式,Iso10126 填充方式
  20. decrypted = CryptoJS.TripleDES.decrypt(srcs, key, {
  21. iv: iv,
  22. mode: CryptoJS.mode.ECB,
  23. padding: CryptoJS.pad.Iso10126
  24. });
  25. return decrypted.toString(CryptoJS.enc.Utf8);
  26. }
  27. var text = "https://www.cnblogs.com/zichliang" // 待加密对象
  28. var desKey = "0123456789ABCDEF" // 密钥
  29. var desIv = "0123456789ABCDEF" // 偏移量
  30. var encryptedData = tripleDesEncrypt()
  31. var decryptedData = tripleDesDecrypt()
  32. console.log("加密字符串: ", encryptedData)
  33. console.log("解密字符串: ", decryptedData)
  34. // 加密字符串: pl/nNfpIrejwK+/X87VmGZIbS3kOB+IpFcx/97wpR4AO6q9HGjxb4w==
  35. // 解密字符串: https://www.cnblogs.com/zichliang

AES

  1. // 引用 crypto-js 加密模块
  2. var CryptoJS = require('crypto-js')
  3. function aesEncrypt() {
  4. var key = CryptoJS.enc.Utf8.parse(aesKey),
  5. iv = CryptoJS.enc.Utf8.parse(aesIv),
  6. srcs = CryptoJS.enc.Utf8.parse(text),
  7. // CBC 加密方式,Pkcs7 填充方式
  8. encrypted = CryptoJS.AES.encrypt(srcs, key, {
  9. iv: iv,
  10. mode: CryptoJS.mode.CBC,
  11. padding: CryptoJS.pad.Pkcs7
  12. });
  13. return encrypted.toString();
  14. }
  15. function aesDecrypt() {
  16. var key = CryptoJS.enc.Utf8.parse(aesKey),
  17. iv = CryptoJS.enc.Utf8.parse(aesIv),
  18. srcs = encryptedData,
  19. // CBC 加密方式,Pkcs7 填充方式
  20. decrypted = CryptoJS.AES.decrypt(srcs, key, {
  21. iv: iv,
  22. mode: CryptoJS.mode.CBC,
  23. padding: CryptoJS.pad.Pkcs7
  24. });
  25. return decrypted.toString(CryptoJS.enc.Utf8);
  26. }
  27. var text = "https://www.cnblogs.com/zichliang" // 待加密对象
  28. var aesKey = "0123456789ABCDEF" // 密钥,16 倍数
  29. var aesIv = "0123456789ABCDEF" // 偏移量,16 倍数
  30. var encryptedData = aesEncrypt()
  31. var decryptedData = aesDecrypt()
  32. console.log("加密字符串: ", encryptedData)
  33. console.log("解密字符串: ", decryptedData)
  34. // 加密字符串: /q8i+1GN8yfzIb8CaEJfDOfDQ74in+XzQZYBtKF2wkAB6dM1qbBZ3HJVlY+kHDE3
  35. // 解密字符串: https://www.cnblogs.com/zichliang

RC4

  1. // 引用 crypto-js 加密模块
  2. var CryptoJS = require('crypto-js')
  3. function RC4Encrypt() {
  4. return CryptoJS.RC4.encrypt(text, key).toString();
  5. }
  6. function RC4Decrypt(){
  7. return CryptoJS.RC4.decrypt(encryptedData, key).toString(CryptoJS.enc.Utf8);
  8. }
  9. var text = "https://www.cnblogs.com/zichliang"
  10. var key = "12345678ASDFG"
  11. var encryptedData = RC4Encrypt()
  12. var decryptedData = RC4Decrypt()
  13. console.log("加密字符串: ", encryptedData)
  14. console.log("解密字符串: ", decryptedData)
  15. // 加密字符串: U2FsdGVkX19/bT2W57mzjwoF5Fc3Zb4WiyDU+MiNMmHfdJvZeScl0EW9yJWCPiRrsA==
  16. // 解密字符串: https://www.cnblogs.com/zichliang

Rabbit

  1. // 引用 crypto-js 加密模块
  2. var CryptoJS = require('crypto-js')
  3. function rabbitEncrypt() {
  4. return CryptoJS.Rabbit.encrypt(text, key).toString();
  5. }
  6. function rabbitDecrypt() {
  7. return CryptoJS.Rabbit.decrypt(encryptedData, key).toString(CryptoJS.enc.Utf8);
  8. }
  9. var text = "https://www.cnblogs.com/zichliang/p/16653303.html"
  10. var key = "1234567ASDFG"
  11. var encryptedData = rabbitEncrypt()
  12. var decryptedData = rabbitDecrypt()
  13. console.log("加密字符串: ", encryptedData)
  14. console.log("解密字符串: ", decryptedData)
  15. // 加密字符串: U2FsdGVkX1/pYbHvbNff3/RNpso4yRKIX0XDFta8hoLNxe52K8HSmF+XV8ayYqucTKVPP6AJtGczXS7U9kkxHnw=
  16. // 解密字符串: https://www.cnblogs.com/zichliang/p/16653303.html

RSA

使用 node-rsa

需要安装一个库

npm install node-rsa

  1. // 引用 node-rsa 加密模块
  2. var NodeRSA = require('node-rsa');
  3. function rsaEncrypt() {
  4. pubKey = new NodeRSA(publicKey,'pkcs8-public');
  5. var encryptedData = pubKey.encrypt(text, 'base64');
  6. return encryptedData
  7. }
  8. function rsaDecrypt() {
  9. priKey = new NodeRSA(privatekey,'pkcs8-private');
  10. var decryptedData = priKey.decrypt(encryptedData, 'utf8');
  11. return decryptedData
  12. }
  13. var key = new NodeRSA({b: 512}); //生成512位秘钥
  14. var publicKey = key.exportKey('pkcs8-public'); //导出公钥
  15. var privatekey = key.exportKey('pkcs8-private'); //导出私钥
  16. var text = "https://www.cnblogs.com/zichliang/p/16653303.html"
  17. var encryptedData = rsaEncrypt()
  18. var decryptedData = rsaDecrypt()
  19. console.log("公钥:\n", publicKey)
  20. console.log("私钥:\n", privatekey)
  21. console.log("加密字符串: ", encryptedData)
  22. console.log("解密字符串: ", decryptedData)
  23. /*
  24. 公钥:
  25. -----BEGIN PUBLIC KEY-----
  26. MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAN7JoMDNvvpB/po2OMSeSKsromfP5EyI
  27. 0fAz6XDVwqdTUBwwAArLlqIzmVNK0yi4nlbj5eF+O8ZjRkRQ6xKP/CMCAwEAAQ==
  28. -----END PUBLIC KEY-----
  29. 私钥:
  30. -----BEGIN PRIVATE KEY-----
  31. MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA3smgwM2++kH+mjY4
  32. xJ5IqyuiZ8/kTIjR8DPpcNXCp1NQHDAACsuWojOZU0rTKLieVuPl4X47xmNGRFDr
  33. Eo/8IwIDAQABAkEArI0Ps6TnIJ9SmZAbYbWSZPjTvYHXuatSpq8eQ+Vb8Ql003G5
  34. Y2FIoWpQX1jQ9/DsxEZ/1u+71bl08z1eONz2KQIhAPgLZOKanhDDaOn5sO7Y2RM3
  35. TyLS08mCGNGQxEhkEttFAiEA5e7bvnrSNh1lcF/QTxkWPGoXb9kxPljm49CfiTS9
  36. PEcCIDzxX7olTwzDVjWWeZhVgxArmK/vqMVrx3lF3lQC8ncZAiBlpY5nSoybd6tc
  37. Xj8MeJ6n3o6112I5mbuYgqXEVhhCCQIgY6vinhOzMF0dX9MNjBm8x1mUCd4XG2TN
  38. QQcOik3RIGw=
  39. -----END PRIVATE KEY-----
  40. 加密字符串: ZolvYwjFqOp1Yldui7rm75mSN5kz7533nc3B3H6xZGQR9v0elhbcjmI9vXaBsgdLNTuyoVk3bfzWfQdeIpvCpcBCTGe1HG9KrSBYDiWJc4vBgVBz8D57/XaS1zjM0kuAJ/ELu4os7XG5lMQbRbFhHXs7zQsIBq6/m2IZdGWx7HjB2jiQBQPMfszdQUOwQA
  41. bM5o7lRvUgdMVaZkEWpOTEybmUX4kxBP5CvNtB86oTRUw+U7Ex7QB8lWj33hoKvh70
  42. 解密字符串: https://www.cnblogs.com/zichliang/p/16653303.html
  43. */

使用自带模块crypto:

  1. const crypto = require('crypto');
  2. const nodeRSA = require('node-rsa');
  3. // 生成一个1024长度的密钥对
  4. const key = new nodeRSA({b: 1024});
  5. // 导出公钥
  6. const publicKey = key.exportKey('public');
  7. // 导出私钥
  8. const privateKey = key.exportKey('private');
  9. const secret = 'https://www.cnblogs.com/zichliang/p/16653303.html'
  10. // 使用私钥加密,公钥解密
  11. const encrypt = crypto.privateEncrypt(privateKey, Buffer.from(secret));
  12. const decrypt = crypto.publicDecrypt(publicKey, encrypt);
  13. console.log('加密后:', encrypt.toString('base64'));
  14. console.log('解密后:', decrypt.toString());

RSA 长加密

这个加密是真的麻烦 ,而且还需要导入jsencrypt.min.js
这里贴上 GitHub地址 github_demo/jsencrypt.min.js at master · wangqinglongDo/github_demo · GitHub
对了 还需要补环境 而且解密也不是很好用,如果有大佬知道如何解密的 希望在评论区告诉我

  1. var encrypt = new JSEncrypt();
  2. var publickKey = "-----BEGIN PUBLIC KEY-----\
  3. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLFb8qp1vRFvi/qfgi1Wg7Mi8l\
  4. LcpfAc+tgpyD7aFW9QquQVMm/jG1IJZVQ6LsdkI7TiDutMCzOMCBXbdSC9BCIAGA\
  5. L2Sz3cYVlGb1kYSM0ZMcUMIK5eF4Bptke070XHvbi8wArtysJ0l71RHDd786tNbG\
  6. W0hDSw3zAqTErbxFaQIDAQAB\
  7. -----END PUBLIC KEY-----\
  8. "
  9. encrypt.setPublicKey(publickKey); //设置公钥加密证书
  10. var data = "https://www.cnblogs.com/zichliang/p/17265960.html";
  11. var commonEncodeData = encrypt.encryptLong(data); // 普通的加密
  12. console.log(commonEncodeData)
  13. var cnEscapeData = window.btoa(window.encodeURIComponent(data)); //base64 解密后的加密
  14. var encryptData = encrypt.encryptLong(cnEscapeData); //获取加密后数据。
  15. console.log(encryptData)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/765059
推荐阅读
相关标签
  

闽ICP备14008679号