RSA加密解密
RSA加密,解密我使用的是jsencypt,具体的使用参考:"https://github.com/travist/jsencrypt"
加密、签名 简介
数据加密:用公钥加密,只有用私钥解开,因为私钥只有你自己有,所以他保证了数据不能被别人看到
数据签名:用私钥加密,只能用公钥解密,任何人都可以用公钥验证。因为私钥只有你自己有,所以它可以保证数据只能是你发出的,不可能有别人发出,除非你得私钥丢失或被第三方破解出来。
rsa找的插件,直接上代码
<script src="./node_modules/jsencrypt/bin/jsencrypt.min.js"></script>
<script>
let private_key = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALIOAdfeFSskOlvY0aN8XThiZmhEkxhOgZ2WlvSwHoHrDTx+vmb5Rx8dP9C7rb8DWMaveq4bzFOW1eMqcbf6TEM78RdSGF7QleegF5qKD4SUe+BZdjLZx319XFvjCgE61gbI+/geJ66QOSQBJGp6r8+yd0X0rcSdm0uhOF0omZaHAgMBAAECgYA5+7HB+IJwQCX5DwyF7tP0rqamwd4dBkPTa9zQ5iGpMUBoOOYMZpUwnrfPY67bC133peGPnWHWvsH8bjAGN65pkuCHanlpkz5T2vQzaK3jx+lX7o5Ka3zK0VxOESvPGBH6hlpx7V4XpYta3470gI3KttT4zQWaZrv+nnGBjQwVoQJBAO1SxJOYpJ8hGiHnysJWzsolhMhUiOMx/R6OyDY6MY38YXV73hAk8/eT13hhWeerMV0D2udh/4h/zaQ4mgtPWqkCQQDAES9e1wDWLKzBPSVXxHW2/NoXvPcSD/GsqXRTeTorXKZB//dqQMFp8tq8zIQkJ9IrU+Y98YBezPrUC9RPDNWvAkAP/rETBYmq0+9P87Bdsr4TL74oRlJHhbn6kkL6LKfkWusyoNuRmPv8ZrFyApWtoIMeMsFRMPCpAXwZolr3giypAkBpsctQtJ64PfN/j8eaCfoBjhnykEGixfVn4ASs3vx82kp/7X1ZEnOde1CWLzHUt7k3sL72fYz4HnM196CJM/XvAkEAg0Q5Nr3WthCJEQ3giXPGmI4QuULTOp1i14PDczUdMYKk19zs+xOxBS9652vFSPLXH5rff4KUy8VrG5N+ln/zEQ=='
let PUBLICK_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyDgHX3hUrJDpb2NGjfF04YmZoRJMYToGdlpb0sB6B6w08fr5m+UcfHT/Qu62/A1jGr3quG8xTltXjKnG3+kxDO/EXUhhe0JXnoBeaig+ElHvgWXYy2cd9fVxb4woBOtYGyPv4HieukDkkASRqeq/PsndF9K3EnZtLoThdKJmWhwIDAQAB'
let str = '11'; // 将要加密的内容
// 加密
let jsEncryp = new JSEncrypt(); // 初始化实例
jsEncryp.setPublicKey(PUBLICK_KEY); // 设置私钥
let data = jsEncryp.encrypt(str); // 加密
console.log(data,'加签') // RDEiVJFZCNN7sD4CuwQvTs7FXDKUZ/JCFtFwjL4xl3VwkfjA0Lf1zAFs1t90XK9p4tbIm9vig4HM80m1b0p3knWrCBOfTZPzC0vSqidY9yPWgqQifGh5Xy+OoewwQe3BDh+IcLCcgbz12god2dAYorcPX7tI2yqs/EPaMQhHbc0=
// 解密
jsEncryp.setPublicKey(private_key) // 设置公钥
let uncrypted = jsEncryp.decrypt(data); // 解密
console.log(uncrypted) // 11
</script>
RSA加签验签
需要注意的是:区分RSA私钥的类型,有pkcs1和pkcs8。pkcs8格式的私钥主要用于Java中。
-
pkcs1格式:
-
-----BEGIN RSA PRIVATE KEY-----
-
-----END RSA PRIVATE KEY------
-
-
pkcs8格式:
- -----BEGIN PRIVATE KEY-----
- -----END PRIVATE KEY----
加签验签必须要有前缀后缀否则会报错
let privateKey = '-----BEGIN PRIVATE KEY-----MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANG/4GW4WIt+vkRpMR + u6aXfH1mMvxFila4C0jMUyfvCZdWIDj4rsNM80EKOHoZYnS1w0kBzIZXeqCAFdLLOX0D4NGpiCtcn / shlI2 / cUUFgLyVDbLdace2y9WKVCH8ohOrw + 3wOFMoBZk2CWKHOYu + 0akkz7NCl0g0MZy9prpNJAgMBAAECgYBa7Bsg3QV9Cqb9zr + dpVvhLFRRpEJbDeomcVCfFf7spHsyUfUinBvdpbn9b8 / pmtYfkY2CpKXFaqxknRjWPcFqBce2jpgh5hJEfPXA1bqfYaSuGJVVER2t1TfbqvBNYdE56KjpZXkQx3dRg / V8M0JYpEz8uymxWDYWmPERk5 + DdQJBAPykfg + mu1NJdqnq5wNE8mAx0uBVg4gUCmSCATrICxvc8aDHQAT7FKNzphVaoaC299nWAuI0QkqhdZ96siHPVKcCQQDUiXWD2PPvtFT9yNw5cjKTfmj3gr7etuRsvX + ovWZusV21W1DSLD4GyAfzl8ju0EHdwSZ7L9Crs4Ryk6uHdqaPAkBgKnTZnEudrk7 + 6q04b1wbBZwRTDue5pH2JA43w8GGv5kx6 + YQf0oh1XmYk0EGnd7GClJarC23nrHZHKkFeZftAkAUz3TLOSYBdYXQ56bwlQbwvDLo8UthIUVShGeIjSsHcaR8SnmqapyUK2fj6RvKjme0n + H1s + PZvBUe5uinte1vAkEAm7FdzA5NrFw7hT7NNozsTjFdpaeIyWp1A2pc9X + Gh8Ynq3syZQj7Xi704l / 4yB5dInzQ6a + fH9BDvJOJ1B8Uxg ==-----END PRIVATE KEY-----';
let publicKey = '-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDRv+BluFiLfr5EaTEfruml3x9ZjL8RYpWuAtIzFMn7wmXViA4 + K7DTPNBCjh6GWJ0tcNJAcyGV3qggBXSyzl9A + DRqYgrXJ / 7IZSNv3FFBYC8lQ2y3WnHtsvVilQh / KITq8Pt8DhTKAWZNglihzmLvtGpJM + zQpdINDGcvaa6TSQIDAQAB-----END PUBLIC KEY-----';
// 加签
let verify_rsa = new RSA.RSAKey()
verify_rsa = RSA.KEYUTIL.getKey(privateKey);
let str = '123'
let Sig = verify_rsa.signStringWithSHA1('signData', str);
Sig = RSA.hex2b64(Sig)
// 验签
verify_rsa = RSA.KEYUTIL.getKey(publicKey)
Sig = RSA.b64tohex(Sig)
let flag = verify_rsa.verifyString("signData", Sig)
console.log(flag) // true