我正在尝试通过使用PHPSECLIB(http://phpseclib.sourceforge.net/)来实现RSA 我认识到的PHP页面效果很好,它生成了一个公共密钥和一个私有密钥,并且对明文进行了很好的加密和解密。 关键是$ ciphertex由奇怪的符号组成,无法正确保存到我的MYSQL数据库内的varchar列中。 这是代码
<?php
include 'Crypt/RSA.php';
include 'Crypt/Rijndael.php';
$rsa = new Crypt_RSA();
$keys = $rsa->createKey(2048);
$rsa = new Crypt_RSA();
$rsa->loadKey($keys['publickey']); // public key
echo $keys['publickey'];
echo "\n -----\n";
echo $keys['privatekey'];
echo "\n -----\n";
$plaintext = 'My text';
echo $plaintext;
$ciphertext = $rsa->encrypt($plaintext);
echo $ciphertext;
$rsa = new Crypt_RSA();
$rsa->loadKey($keys['privatekey']); // private key
echo "\n -----\n";
echo $rsa->decrypt($ciphertext); ?>
这是我的私钥示例
-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEA0z6+jhrOkoxwuHL8KtieekjKytpk/ZGck47bX4qB79v0Hhvw o1080mWYzrtSmeB4+Od6CktUxAwuNTUL2rGck577A6nVYtasdJgzf8zCf+KGocOX qjB8sdryGbKgUmFTDu1KPKQh9msyXsEpCFcVeDZQcfTEcJ/9kSRjgLD8GJZc7HLS br8+uJ8wyyC1E76taMYeBFcVAr4mo/Zyv4hML6oPYMDMaouFFQKB30KfPwRsI4iw CXP/RsgxIN2a8xmB5XAt36GOJNyuCPEzUA/NBYcGmiil1B4HzG3f7MKhg0q4bzrV dp03RJZ5OOkt4n49Tjz1IOWXVPkQRngC9+/UIQIDAQABAoIBAFDGmU71aTAYi7G8 JjisBE12MDVJNXzQhTLXWYM7D1yMmtg4jteo+2zodHn4nyk/8IIc4Um2bH9auQCX fCTv7ZN1Iz7PXk0hEWi0Gl0wCt+j2x9YD1KAn+3SbujsqOlXVc9LZSuzB1mWu/gH nI0X35nMCw7ILaVzSB6L75ByHLO5eXMxRwWS5yGtk57QfF+QDAzBs2enVV4MWM/f 9cddzDT/ScUaPF5+R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w0BAQEFAAOCAQ8AMIIBCgKCAQEA0z6+jhrOkoxwuHL8Ktie ekjKytpk/ZGck47bX4qB79v0Hhvwo1080mWYzrtSmeB4+Od6CktUxAwuNTUL2rGc k577A6nVYtasdJgzf8zCf+KGocOXqjB8sdryGbKgUmFTDu1KPKQh9msyXsEpCFcV eDZQcfTEcJ/9kSRjgLD8GJZc7HLSbr8+uJ8wyyC1E76taMYeBFcVAr4mo/Zyv4hM L6oPYMDMaouFFQKB30KfPwRsI4iwCXP/RsgxIN2a8xmB5XAt36GOJNyuCPEzUA/N BYcGmiil1B4HzG3f7MKhg0q4bzrVdp03RJZ5OOkt4n49Tjz1IOWXVPkQRngC9+/U IQIDAQAB -----END PUBLIC KEY-----
这是密文的示例
)�y�?���d�CE�ʙ/H�"�8��p#�0�$UI��hb� �x���q��Ck,�a�%�1���cX��Y��r��i"�iG�)�ćv��ӕȝu��0��܈ ��=��N}���bU�T��c*����:�,;�l'���� �!�W�;]B�K�A)�?�/{�'���s#�7�bC�1K䥯��X]H0Y�����٪�U�����a�,��.���0v$ _��YS���3��\