标签:
通过http://www.cnblogs.com/smismile/p/3789874.html这篇博文生成所需的公钥字符串和私钥
1. 前端加密写法,所需js文件
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script src="./Barrett.js"></script> <script src="./BigInt.js"></script> <script src="./RSA.js"></script> <script> $(function(){ var rsa_n = "AA6349CF6ADECA06C7A9CAADB43D47BE3D53BB93B843DF7FE8265F6D32C6A0F1CF3F3536E9C21D74117907FE00ED23C2B52140BA5D030FCE76F25040978BAF842E022C99C6A27FE63B2FD3DAAE2C7FBDF0BC0CEB3DBACAD405ACC9E692E13D60421B63624149E5BE4F95D1A701190135053EDCA316C836D1760639854BC421EF"; $("#submit").click(function(){ setMaxDigits(131); var key = new RSAKeyPair("10001", ‘‘, rsa_n); var password = $("#password").val(); password = encryptedString(key, password); $("#password").val(password); $("#login").submit(); }); }); </script>
2. 后端php解密
/** * 公钥加密 * * @param string 明文 * @param string 证书文件(.crt) * @return string 密文(base64编码) */ function publickey_encodeing($sourcestr, $fileName) { $key_content = file_get_contents($fileName); $pubkeyid = openssl_get_publickey($key_content); if (openssl_public_encrypt($sourcestr, $crypttext, $pubkeyid)) { return base64_encode("".$crypttext); } } /** * 私钥解密 * * @param string 密文(二进制格式且base64编码) * @param string 密钥文件(.pem / .key) * @param string 密文是否来源于JS的RSA加密 * @return string 明文 */ function privatekey_decodeing($crypttext, $fileName, $fromjs = FALSE) { $key_content = file_get_contents($fileName); $prikeyid = openssl_get_privatekey($key_content); $crypttext = base64_decode($crypttext); $padding = $fromjs ? OPENSSL_NO_PADDING : OPENSSL_PKCS1_PADDING; if (openssl_private_decrypt($crypttext, $sourcestr, $prikeyid, $padding)) { return $fromjs ? rtrim(strrev($sourcestr), "/0") : "".$sourcestr; } return ; } // 以上是加密和解密函数,以下是解密过程 $txt_en = base64_encode(pack("H*", $_POST[‘password‘])); $file = ‘./ssl/rsa_private_key.pem‘; $txt_de = privatekey_decodeing($txt_en, $file, TRUE); echo ‘解密:‘. $txt_de;
标签:
原文地址:http://www.cnblogs.com/smismile/p/4600790.html