Asymmetric encryption algorithm (public key and private key)

An asymmetric encryption algorithm requires two keys for encryption and decryption. The two keys are a public key (public key, referred to as public key) and a private key (private key, referred to as private key).

Public Key and Private Key are a key pair (that is, a public key and a private key) obtained through an algorithm. The public key is the public part of the key pair, and the private key is non- The public part. The public key is usually used to encrypt session keys, verify digital signatures, or encrypt data that can be decrypted with the corresponding private key. The key pair obtained through this algorithm can be guaranteed to be unique in the world. When using this key pair, if one key is used to encrypt a piece of data, the other key must be used to decrypt it. For example, if you use a public key to encrypt data, you must use the private key to decrypt it. If you use the private key to encrypt data, you must also use the public key to decrypt it, otherwise the decryption will not succeed. (Here cited Baidu Encyclopedia entries explanation)

Let's talk about how to use it.
First introduced in the page jsencrypt file (you can use CDN acceleration)
Then you can use the key generation tool to generate a public key and a private key (recommended encryption and decryption tool )
the full story (ps: a public key and a private key can use the key generation tool To generate):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jsencrypt/2.3.1/jsencrypt.js"></script>
</head>
<body>
    <script>
        var public_key = "-----BEGIN PUBLIC KEY-----" +
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD3OhpxaOoSpObJ3CXh31T1kE5I"+
"b5DfCUB59vX7fxliFDEIknR1nkAk+YQltVF/RIcaoT8ni/NGBUJxp19nojpLUes2"+
"kqaVo5H8F7WOw2okHwtCGGEb+MMol11qCKG1nGl7cZtEu+2T/O1A4wYzvg6XUsXJ"+
"+HPnc2/15D410bK94QIDAQAB" + 
"-----END PUBLIC KEY-----";
        var private_key = "-----BEGIN PRIVATE KEY-----" +
"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPc6GnFo6hKk5snc" +
"JeHfVPWQTkhvkN8JQHn29ft/GWIUMQiSdHWeQCT5hCW1UX9EhxqhPyeL80YFQnGn" +
"X2eiOktR6zaSppWjkfwXtY7DaiQfC0IYYRv4wyiXXWoIobWcaXtxm0S77ZP87UDj" +
"BjO+DpdSxcn4c+dzb/XkPjXRsr3hAgMBAAECgYB3/nKRMwvRaso9laWpYN16psTo" +
"MYZOSs5JpE01+TWXvhcigvsVj26ww46ZQs1AiUFN8o+VoT69cJYUVdlfkoTZz7nc" +
"a8aH/8CVjEANjD8DGmzOJJIFpWXP0wMYta09SdCGKnKbmmlDX1MHAauA7oZy81gy" +
"8Di0DTfTLv3oW4YCBQJBAP5OfbZOorq7sPElk+W/oJhn5kf5Ym5QZc2KlpOKSTw4" +
"uUIBoA//PbdpVTzz9UJSQF/D6sgLBCeDnd6OGg368pcCQQD434s1Bnbc45xTEC/R" +
"zorB8eD3QHDIfrXfaLTy+KPPaGUP9QUu+m9YMQwVsYSW7UzRhKqPQjsgHFbO9eXx" +
"nvpHAkEArK7z4lWnXpGQ508TDNGA5by3vGv1kN77IgVXljwy2rfR5KZh4Dr7142p" +
"Gci25CAiImVRCiGg4owXKUbpHlEnZQJBAMsvpzW5e13ILp7aPlBq8np5ghQbdnka" +
"il5F3EXqncL+FlS61GqQaEt2b8leT0PxpaABUkb36dHDkVQTCpMEGo0CQERzIpg1" +
"F1uePVCsDQcRJ3TZ6CKeRsWzJUmuz10s+v8wIKzN1Dxtyok4qEDAemR81LJPsldw" +
"9SocEoKudNJ4NLA=" +
"-----END PRIVATE KEY-----";

        // 采用公钥进行加密
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(public_key);
        var encrypted = encrypt.encrypt('世上无难事,只怕有心人'); // 加密
        console.log(encrypted);

        // 采用私钥进行解密
        var decrypt = new JSEncrypt();
        decrypt.setPrivateKey(private_key);
        var uncrypted = decrypt.decrypt(encrypted); // 解密
        console.log(uncrypted)
    </script>
</body>
</html>

Guess you like

Origin blog.csdn.net/qq_43248623/article/details/108419464