PHPSeclib使用心得:RSA加密和AES加密

1.RSA

新建密钥对:

$rsa = new RSA();
$result = $rsa->createKey();
echo $result["privatekey"]    //私钥
$result["publickey"]    //公钥

加密:

$rsa = new RSA();
$rsa->loadKey($key);

$rsa->setEncryptionMode(RSA::ENCRYPTION_NONE);    //选择加密的模式,可选模式参考官方文档

echo base64_encode($rsa->encrypt($msg));    //需要对结果进行base64转码,否则为乱码

解密:

$rsa = new RSA();
$rsa->loadKey($key);

$rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1);

echo $rsa->decrypt(base64_decode($msg));    //同理,解码时,需要先进行base64解码

目前笔者测试能和PHPSeclib的 RSA配合的javascript包为: https://github.com/rzcoder/node-rsa

2.AES

加密:

$aes = new AES(MCRYPT_MODE_ECB);
$aes->setKey(self::KEY);
$aes->setIV(self::VECTOR);
echo base64_encode($aes->encrypt($msg));

解密:

$aes = new AES(MCRYPT_MODE_ECB);
$aes->setKey(self::KEY);
$aes->setIV(self::VECTOR);
echo $aes->decrypt(base64_decode($msg));

 

你可能感兴趣的:(PHPSeclib使用心得:RSA加密和AES加密)