最簡單的RSA加密demo,初學者的保姆級即抄即用!

MC811-MM118發表於2021-12-10

可能你第一次用RSA,那麼直接複製使用吧!不用謝我,我是淺糖!

/**
     * RSA加密
     * @throws \Exception
     */
    public function fingerprintUnlock($eb64_cry)
    {
        header("Content-type: text/html; charset=utf-8");
        define('RSA_PUBLIC', '-----BEGIN PUBLIC KEY-----g/TQIDAQAB
-----END PUBLIC KEY-----');
        define('RSA_PRIVATE','-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMBDr/ejmU/zfV+f
CZ9iv8ABzpBW+ixut7FX4f7SIk+R/HDRYmNgCbgHHciGrofJ46kVcQYStoyKJJmE
drCVO6************************************************AaDxX6geVT
E+ihtOLlsp0j3jX8UpqsWH+2WD9NAgMBAAECgYBYcosQDEXqPuRtSdd+aEo4BVzG
kAy0xhZhUkGYHYlFO657JAf0GWk/LJ7p8fN2akz+Kuv81D0AeEiQCx0i7PQvp6na
WBcRib7qoKyK3cUJmtMt6ovpmufLKd9+fgvQwEYcucoMUU+tvvhlERBEBJ54YsTU
3cVtrY+fRQ********************************CWwp+RHPF83fjVTu+9eAfK
N7YkcOAg459+ud+UxvsgyZh2JIFHtyOusNDYiQaX7PsCQQDbuce4BVJbhLAOUW+T
KNLn/CEgd4UCVg9wDjY6NnJGYV75zCPmEDQMWVz56KraEsDA/GIPfAiBSgcLWhK6
ekJXAkAxIHjF81jgqDj4UEUvxuRqUoYKgsPLPmxcF0QBlhcN9k5zlthefzgMkblo
DHP7PXyZAEgPRlAm9jf3BZY/qPAjAkEAxBGuz14oVLQ9qjkvGCE4unw5T1B351LG
ada0uxN0u6sN5lgsvJI+77oahoMBWqGbUVqlYm3q5Pwa9x1EUxTf9wJBAM8WJEn+
6oa0KipqEiezWX/zUy8KhAR+jv02YVOx0KE0eWDlie0yyV5h8PQ1WBuVvEutHw6q
SvZ1NRs+uqnJ3b4=
-----END PRIVATE KEY-----');
        //公鑰加密
        $public_key = openssl_pkey_get_public(RSA_PUBLIC);
        if(!$public_key){
            die('公鑰不可用');
        }
        //第一個引數是待加密的資料只能是string,第二個引數是加密後的資料,第三個引數是openssl_pkey_get_public返回的資源型別,第四個引數是填充方式
        $return_en = openssl_public_encrypt("好好學習,太逆天上", $crypted, $public_key);
        if(!$return_en){
            return('加密失敗,請檢查RSA祕鑰');
        }
        $eb64_cry = base64_encode($crypted);
        echo "公鑰加密資料:".$eb64_cry;
        echo "<hr>";
        //私鑰解密
        $private_key = openssl_pkey_get_private(RSA_PRIVATE);
        if(!$private_key){
//            die('私鑰不可用');
            return false;
        }
        $return_de = openssl_private_decrypt(base64_decode($eb64_cry), $decrypted, $private_key);
        if(!$return_de){
            return('解密失敗,請檢查RSA祕鑰');
            return false;
        }
        return $decrypted;
        echo "私鑰解密資料:".$decrypted;
        echo "<hr>";
        echo "<hr/>";
        //私鑰加密
        $private_key = openssl_pkey_get_private(RSA_PRIVATE);
        if(!$private_key){
            die('私鑰不可用');
        }
        $return_en = openssl_private_encrypt("hello world222222", $crypted, $private_key);
        if(!$return_en){
            return('加密失敗,請檢查RSA祕鑰');
        }
        $eb64_cry = base64_encode($crypted);
        echo "私鑰加密資料".$eb64_cry;
        echo "<hr>";
        //公鑰解密
        $public_key = openssl_pkey_get_public(RSA_PUBLIC);
        if(!$public_key){
            die('公鑰不可用');
        }
        $return_de = openssl_public_decrypt(base64_decode($eb64_cry), $decrypted, $public_key);
        if(!$return_de){
            return('解密失敗,請檢查RSA祕鑰');
        }
        echo "公鑰解密資料:".$decrypted;
        echo "<hr>";
    }
`

還看不懂,請看這裡:qv4zhbe2wa.feishu.cn/docs/doccn3az...

本作品採用《CC 協議》,轉載必須註明作者和本文連結
談笑有鴻儒,往來無白丁!

相關文章