<script src="https://cdnjs.cloudflare.com/ajax/libs/jsrsasign/8.0.20/jsrsasign-all-min.js">script>
<script>
// 公钥
let pk="-----BEGIN PUBLIC KEY-----\n" +
"MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgFYiHvXY0EqYUAEuchLaOtpTfjxo\n" +
"l9v6Qt8keUw+LG6YwtpFz3c5r3ZezgjpCEq2EQzAdJzGPylfgQGL2mSr+whczz1i\n" +
"6uXAB7KYVY3DUY2C9LkfhdmWoWHPT0n8Po7mbWauddE7thgGcXHs6ngEq5tr6us6\n" +
"BpHo7rrm59ccjPHTAgMBAAE=\n" +
"-----END PUBLIC KEY-----"
// 私钥
let priK = "-----BEGIN RSA PRIVATE KEY-----\n" +
"MIICWwIBAAKBgFYiHvXY0EqYUAEuchLaOtpTfjxol9v6Qt8keUw+LG6YwtpFz3c5\n" +
"r3ZezgjpCEq2EQzAdJzGPylfgQGL2mSr+whczz1i6uXAB7KYVY3DUY2C9LkfhdmW\n" +
"oWHPT0n8Po7mbWauddE7thgGcXHs6ngEq5tr6us6BpHo7rrm59ccjPHTAgMBAAEC\n" +
"gYA6Ny+jzJnjgRS557F0KomSnNH6dFsHdBuUhCtzq6YOTjOMoZh6pcOBBszM2uwe\n" +
"yvW1H7Lxb1CrXwBrkiHW3yJ4mIy7M/VnO4n1YD6S5yZs+F72EyXxzEbVHSw3MZRg\n" +
"HMQ/fEmjfTCTxAkpDThKI45x0iCWPYrAdJZo5D+jK/ajKQJBAJs7U52eMoIxEuuZ\n" +
"E7kmdiiYS8mlmkLDtxh0coyIYzA/Ia7vq0GzVAgU2Gw5Jbc7Xs8jDUekz63QN90h\n" +
"+28pDtcCQQCOC+VIKOmtqdzNeh3NMv2zAaKpYgTieze8+qk2sbdyKpG5CY5+LKwq\n" +
"z8/m/hCEHrunElDFl1/hXwNrMB3I5sFlAkAPpG7Ml0BntiI983L1WVbWyGH6s+FT\n" +
"kP1GTZWta6DzH6l5Q1VyjWuOVOtqHGgcirMHFHUmHwl+bljyw5XeobiJAkEAh+bd\n" +
"sRJjsGGp/XVuedrlW5Se+gvrvHxDMa1opAmYB8SusyhvikxXVIaK1OgTCTUWOJyP\n" +
"ZtyTQV7UOINKm+Ia+QJADxxw7+BoN4/IAMld7/NhItkNRwE8/sQTes12IlZdvikM\n" +
"l+n5UgpXWz+ZJnX5WhAHytf3oIRU2Jd7Ix3I8gwDTg==\n" +
"-----END RSA PRIVATE KEY-----";
let src = "我是谁 我从哪里来 我要到哪里去";
// 加密、解密套餐 ( 公钥加密,私钥解密 ):
// 加密
let pub = KEYUTIL.getKey(pk);
let enc = KJUR.crypto.Cipher.encrypt(src, pub);
console.log(`公钥加密结果:${enc}`);
// 建议使用使用hextob64u(enc), 它会将+替换成-,/替换成_,去掉尾部补全的=
// console.log(hextob64(enc));
// 解密
let prv = KEYUTIL.getKey(priK);
let dec = KJUR.crypto.Cipher.decrypt(enc, prv);
console.log(`私钥解密结果:${dec}`);
// 签名、验签套餐 ( 私钥签名,公钥验签 ):
// 签名
let signature=new KJUR.crypto.Signature({alg:"SHA1withRSA",prvkeypem:priK});
signature.updateString(src);
// 签名返回结果是16进制字符串,注意转码
let a = signature.sign();
let sign = hextob64(a);
console.log(`私钥签名:${sign}`);
// 验签
let signatureVf = new KJUR.crypto.Signature({alg:"SHA1withRSA",prvkeypem:pk});
signatureVf.updateString(src);
// 验签入参是16进制字符串,注意转码
let b = signatureVf.verify(b64tohex(sign));
console.log(`公钥验签:${b}`);
script>
node
apt update
apt install npm
npm install -g jsrsasign-util
npm install -g jsrsasign
npm install -g crypto-js
npm list -g
npm fund
touch test .js
vi test.js
var jsrsasign=require("jsrsasign");
// 公钥
let pk="-----BEGIN PUBLIC KEY-----\n" +
"MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgFYiHvXY0EqYUAEuchLaOtpTfjxo\n" +
"l9v6Qt8keUw+LG6YwtpFz3c5r3ZezgjpCEq2EQzAdJzGPylfgQGL2mSr+whczz1i\n" +
"6u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let src = "我是谁 我从哪里来 我要到哪里去";
console.log(`${pk}`);
console.log(`${priK}`);
// 加密、解密套餐 ( 公钥加密,私钥解密 ):
// 加密
let pub = jsrsasign.KEYUTIL.getKey(pk);
let enc = jsrsasign.KJUR.crypto.Cipher.encrypt(src, pub);
console.log(`公钥加密结果:${enc}`);
// 建议使用使用hextob64u(enc), 它会将+替换成-,/替换成_,去掉尾部补全的=
// console.log(hextob64(enc));
// 解密
let prv = jsrsasign.KEYUTIL.getKey(priK);
let dec = jsrsasign.KJUR.crypto.Cipher.decrypt(enc, prv);
console.log(`私钥解密结果:${dec}`);
// 签名、验签套餐 ( 私钥签名,公钥验签 ):
// 签名
let signature=new jsrsasign.KJUR.crypto.Signature({alg:"SHA1withRSA",prvkeypem:priK});
signature.updateString(src);
// 签名返回结果是16进制字符串,注意转码
let a = signature.sign();
let sign = jsrsasign.hextob64(a);
console.log(`私钥签名:${sign}`);
// 验签
let signatureVf = new jsrsasign.KJUR.crypto.Signature({alg:"SHA1withRSA",prvkeypem:pk});
signatureVf.updateString(src);
// 验签入参是16进制字符串,注意转码
let b = signatureVf.verify(jsrsasign.b64tohex(sign));
console.log(`公钥验签:${b}`);
node test.js
参考连接
JavaScript的RSA加密库:https://blog.csdn.net/junxuezheng/article/details/109824552
jsrsasign使用笔记:https://www.jianshu.com/p/9cc8ba017c0e/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)