function DecryptString($sourceStr) { $key = base64_encode(pack("H*", $sourceStr)); return Rsa::privDecrypt($key, true); }
$r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey); if ($r) { return $decrypted; } return null; } public function __destruct() { @fclose($this->_privKey); @fclose($this->_pubKey); } } //以下是一个简单的测试demo,如果不需要请删除 $rsa = new Rsa('sslkey'); //sslkey为存放密钥的路径,将已有的密钥文件复制到该路径下,公钥名称为pub.key,私钥名称为priv.key //$rsa->createKey(); //创建一对密钥,如果密钥对已经存在,不需调用 //私钥加密,公钥解密 echo 'source:我是老鳖<br />'; $pre = $rsa->privEncrypt("我是老鳖"); echo 'private encrypted:<br />' . $pre . '<br />'; $pud = $rsa->pubDecrypt($pre); echo 'public decrypted:' . $pud . '<br />'; //公钥加密,私钥解密 echo 'source:干IT的<br />'; $pue = $rsa->pubEncrypt('干IT的'); echo 'public encrypt:<br />' . $pue . '<br />'; $prd = $rsa->privDecrypt($pue); echo 'private decrypt:' . $prd; $p = "BHh8quzSlewEJQ6ZiYOsBRDxUVcSh+fVboVI0YXK2LJNH9v/PRG4VpbmroiJtn9Gp55kVAVHLBgTCwnrd0N2FJE+0M5+n6TzkOul1tSgzo+WZVJWXOFlw0S3m8LMLP0ZtMrfQxj8JtgNbewoFIQ2M3ReYCpLTQ4D1EwxzwT/b88="; $prd = $rsa->privDecrypt($p); echo "???????" . $prd;
* 私匙加密 * @param [type] $data [description] * @return [type] [description] */ public static function encrypt($data) { if (openssl_public_encrypt($data, $encrypted, self::getPublicKey())) { $data = base64_encode($encrypted); } else { throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?'); } return $data; } } //JS->PHP传输RSA加密解密测试 $password = $_POST['password']; echo "Password Source String:<br/>" . $password; echo "<br/>"; $key = base64_encode(pack("H*", $password)); echo "Password Decrypt String:" . Rsa::privDecrypt($key, true); echo "<br/>"; $username = $_POST['username']; echo 'Username Source String:<br/>' . $username . '<br/>'; $key = base64_encode(pack("H*", $username)); echo "Username DecryptString:" . Rsa::privDecrypt($key, true); //echo '<br>'; //PHP->PHP RSA加密解密测试 //$key = Rsa::encrypt('测试中文rsa加密'); //echo $key; //echo '<br/>'; //echo Rsa::privDecrypt($key);
* 私匙加密 * @param [type] $data [description] * @return [type] [description] */ public static function encrypt($data) { if (openssl_public_encrypt($data, $encrypted, self::getPublicKey())) { $data = base64_encode($encrypted); } else { throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?'); } return $data; } } //JS->PHP传输RSA加密解密测试 $password = $_POST['password']; echo "需要解密字符串(password):<br/>" . $password; echo "<br/>"; $key = base64_encode(pack("H*", $password)); echo "解密后原密码:" . Rsa::privDecrypt($key, true); echo "<br/>"; $username = $_POST['username']; echo '需要解密字符串(username):<br/>' . $username . '<br/>'; $key = base64_encode(pack("H*", $username)); echo "解密后原用户名:" . Rsa::privDecrypt($key, true); //echo '<br>'; //PHP->PHP RSA加密解密测试 //$key = Rsa::encrypt('测试中文rsa加密'); //echo $key; //echo '<br/>'; //echo Rsa::privDecrypt($key);