<!DOCTYPE html>
<html>
<head>
<title>rsa加密</title>
</head>
<body>
rsa加密解密
</body>
<!--引入jsencrypt.js-->
<script src="https://cdn.bootcss.com/jsencrypt/3.0.0-beta.1/jsencrypt.js"></script>
<script type="text/javascript">
//公钥
var PUBLIC_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCiR6jIjY9Enn44KBlHRk1fZzcYvGYJSeMsiDqOz8sF5NPtsy8S49LaiEF0NZXpt76ZYMHsj+SgIlC5e8M/DmBW5ATTaUSPUAybX03Q0Kyn5pcFT/KODSiaS21CgKDC7zyLb7FVs4zL9G+Hm4zmZU3Zq59xvu7KoUlk+RXDyoz5hQIDAQAB';
//私钥
var PRIVATE_KEY = 'MIICWwIBAAKBgQCiR6jIjY9Enn44KBlHRk1fZzcYvGYJSeMsiDqOz8sF5NPtsy8S49LaiEF0NZXpt76ZYMHsj+SgIlC5e8M/DmBW5ATTaUSPUAybX03Q0Kyn5pcFT/KODSiaS21CgKDC7zyLb7FVs4zL9G+Hm4zmZU3Zq59xvu7KoUlk+RXDyoz5hQIDAQABAoGAel/JgvtlN5zBl/19GoeEX1ZJcSjZVVXXr0RHzFwcQKSPwGdoLzmITu2DVmv2UdoTLDyP4KGeCbf24fjXgpyHvqsmN87V90uD84J4GXI6rqJVp1KH+oJh/21Q4gbQ0A78sQ6Bod7RgnjpumvE2xvEv/4ypx4pekqE/XvcVjuhpz0CQQDN6ezIFDvcANP0FCrvBq4gLUAZ1KSVJ9/cSd3bUGG3bvJfkWQ4IRst8IHzsTe7PTdXPfloGMY6YpaCC8rp6Nq3AkEAycCxEtFtkXuKzO1I+13FyJEJUnHShQHVI/hLPU+jwYOomMOWX8yMryocLPokJ0q1SI/VoGIuFyolcrRIzgkBowJAdNlYOrnwtgnqPUnsizOZQD0tcPsQYmv8Rv/L3BajPPrCo6TX4Fo5GDqxI97fJ2u3qdRLYd0dMCieDDIw9cfjxQJAbwGJrDv02FYsQaLRRBUNTNSYV3dvCGXAu0DTTRGznTbundcHtwAZ7Ar0Y77iA4h0hE83kD5U88PpxNnzMQH9lwJABsTCiK8uGQpg9PwFxpZuwHURWU+JzJ+Mt52CItmzIZyljkpMATf3hFKnki4FathFJeVOMQrv8a2uinSnZ0/6MQ==';
//使用公钥加密
var encrypt = new JSEncrypt();
//encrypt.setPrivateKey('-----BEGIN RSA PRIVATE KEY-----'+PRIVATE_KEY+'-----END RSA PRIVATE KEY-----');
encrypt.setPublicKey('-----BEGIN PUBLIC KEY-----' + PUBLIC_KEY + '-----END PUBLIC KEY-----');
var str = "hello tom summer";
var encrypted = encrypt.encrypt(str);
console.log('加密前数据:%o', str);
console.log('加密后数据:%o', encrypted);
//使用私钥解密
var decrypt = new JSEncrypt();
//decrypt.setPublicKey('-----BEGIN PUBLIC KEY-----' + PUBLIC_KEY + '-----END PUBLIC KEY-----');
decrypt.setPrivateKey('-----BEGIN RSA PRIVATE KEY-----'+PRIVATE_KEY+'-----END RSA PRIVATE KEY-----');
var uncrypted = decrypt.decrypt(encrypted);
console.log('解密后数据:%o', uncrypted);
</script>
</html>
网友评论