本文主要是介绍js 加解密 jsencrypt(非对称加密 rsa),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这是一个非对称加密的库,可以进行 rsa 加解密
使用方法
安装
npm install jsencrypt --save
jsencrypt rsa 加解密
let rsaStr = "这就是一个RSA加密的测试";let jsencryptObj = new jsencrypt();jsencryptObj.getKey(); //这个方法用来生成一个密钥对let publicKey = jsencryptObj.getPublicKey(); //得到公钥let privateKey = jsencryptObj.getPrivateKey(); //得到私钥let result1 = jsencryptObj.encrypt(rsaStr); //加密var st = jsencryptObj.decrypt(result1); //解密
上面是最基本的用法, 但是我们通常是 从前端接收到后端的数据,然后再去解密的,这里我们用 php 做后端加密数据,再用js 作前端来解密数据
这里我们首先产生一个公,私钥对
公钥字串
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDNW0UTqFuKE4rA
NeUkPC5GhB8XcrcepL3jFLhK7YfD/spropSxqCH6PQ+VchIsCAGAtrKVyABjsGbU
yWa5ww90Z0FHo4UR2WnucvAlzmS2ENXSCif7W5Tj3lUddM9ZURwz3lWTA7QZLoIf
4AfR0zPJq0XhYzaAOdNoFm5AoZ27zSD3uBBL4vOXEfwZcOP6fQLrUkwSnKPj08BK
RRmwie3pJ2pU7eBX9NkS9oWU5u74LzJ+u4W6pdYGZo/TjTCyPF44Udkx+Rp9+BUg
Wd23rcVF2VmdWsZsgcIugjH/8z1sYbF0GdGe9CFM2CydxpzE91vqHEm7PaeaBWbe
JxsOdowdAgMBAAECggEAW04waWwav0qvD4UnC7RiUuIdvNw5hTt0XknxIwd3rPJV
aAQSnucFrHCHxEfCwqcQm/0fbC9P1QMyxXuC3CdZcMUUJlkTKnuDeQzLr9OTvcvA
djIj0sPGcIaDS6SLKMhChOFfJQ2pfuTJ4IhIkceZKBqp6GunaN3NHGOUw9HYAw6G
XrRvUXr4KSXwqAF83ufSKzV7jrsVu6Pn/59svwrJdYZp3jZ6eq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OCAQ8AMIIBCgKCAQEAzVtFE6hbihOKwDXlJDwu
RoQfF3K3HqS94xS4Su2Hw/7Ka6KUsagh+j0PlXISLAgBgLaylcgAY7Bm1MlmucMP
dGdBR6OFEdlp7nLwJc5kthDV0gon+1uU495VHXTPWVEcM95VkwO0GS6CH+AH0dMz
yatF4WM2gDnTaBZuQKGdu80g97gQS+LzlxH8GXDj+n0C61JMEpyj49PASkUZsInt
6SdqVO3gV/TZEvaFlObu+C8yfruFuqXWBmaP040wsjxeOFHZMfkaffgVIFndt63F
RdlZnVrGbIHCLoIx//M9bGGxdBnRnvQhTNgsncacxPdb6hxJuz2nmgVm3icbDnaM
HQIDAQAB
-----END PUBLIC KEY-----
在php中利用公钥来加密 (注意一下,公钥字符串的定义,是使用的 长字符定义的方式)
php rsa 加密很简单
$str="这是一个后端加密,前端解密的实验,RSA算法";//这里得到了私钥的密钥$private_key = <<<"eto"
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDNW0UTqFuKE4rA
NeUkPC5GhB8XcrcepL3jFLhK7YfD/spropSxqCH6PQ+VchIsCAGAtrKVyABjsGbU
yWa5ww90Z0FHo4UR2WnucvAlzmS2ENXSCif7W5Tj3lUddM9ZURwz3lWTA7QZLoIf
4AfR0zPJq0XhYzaAOdNoFm5AoZ27zSD3uBBL4vOXEfwZcOP6fQLrUkwSnKPj08BK
RRmwie3pJ2pU7eBX9NkS9oWU5u74LzJ+u4W6pdYGZo/TjTCyPF44Udkx+Rp9+BUg
Wd23rcVF2VmdWsZsgcIugjH/8z1sYbF0GdGe9CFM2CydxpzE91vqHEm7PaeaBWbe
JxsOdowdAgMBAAECggEAW04waWwav0qvD4UnC7RiUuIdvNw5hTt0XknxIwd3rPJV
aAQSnucFrHCHxEfCwqcQm/0fbC9P1QMyxXuC3CdZcMUUJlkTKnuDeQzLr9OTvcvA
djIj0sPGcIaDS6SLKMhChOFfJQ2pfuTJ4IhIkceZ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qU0MM28=
-----END PRIVATE KEY-----
eto;openssl_private_encrypt($str,$result,$private_key);//参数中的 $result 就是 rsa 的加密结果dump($result);//加密结果是一个二进制的数据,所以我们以base64一下传给前端echo base64_encode($result);
//结果 : CcNW2lFUQHYAhz60CHpwPbCXlqpZYQIK0gJZWeE5IPJG9Hw8e3arJ3j9PqsLrDyCRub5pOJQQqrOpMIyOZCP05BUcJRpBi+DR6Z7MrJuqw7B+283mZO/bPllhj6SILz7BD17zodmzw9E+QZG/DdLouOGVKHiirU9STFRgD3DiN93Rj8JzK70gNCnWrEtrCfHA+NdagLCdUEITxf3DL574ZlUXrSIvCj/Rf48/gXWypxG333Pp/IswHtBL6UOuiKGyY//t2aTqCgWRT8qy3nvWAXfH4WgePe+irPYxwOQkXh90oSiRDT2gLhnLV3ZWSSMqaAfCgKNKsCF+uGdONwMiw==
js 进行解密
把php中的结果字符串得到
解密失败, 有太神帮我看一下 问题在哪
这篇关于js 加解密 jsencrypt(非对称加密 rsa)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!