直接开始
1、生成公钥和私钥
打开ssl
openssl
生成私钥
genrsa -out rsa_private_key.pem 1024
转换为pkcs8格式
pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt
将生成的PRIVATE KEY复制出来备用
生成公钥
rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
然后目录下将有2个文件
2、测试效果
首先使用RSA第三方库,将其RSA.h
RSA.m
2个文件拖入自己的项目
注意:这里使用的私钥是刚刚转换格式后复制出来的,而不是文件里的
加解密成功
服务端处理
我这里使用的是php
代码如下
class RSA {
private static $PRIVATE_KEY = '-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALICiOzQ7D42qbYZ
RVoMEy2JCg0O4KCWpFqF7tTyuRFZc4o//wHtcAexmP5CkXnd++Vn97IFxUiN+/EC
+fVZIuz6yhdqZC4+kiosuFbA9lD9G1zCCfui6AyH9+KkUw7wRrR3DsdBreD9JCCL
vKJok1D1G3eg3oZVNtBDjeWgL5bXAgMBAAECgYEAmCeWhmWmKR9sPNyo7Ok5U0i5
sKOw2VMqNL4agSbfjbbaDafZ/vnHMqxZS6v20SYvc81EaYSJNxU17rJOmaqRMHSp
PKjnoewiw2tyhVtT9WL2YkFukJSB3mE6hl/1rgfU0zgVLBjp2Jrgm66qSY0OmBmP
kVkVtqYsmPxyPaUPhlECQQDnRmk8g5rw0iuJII9/UP04GwM99p8gtuk5hTrnc0yP
uXVJ1zeYlnWVej3nYI0XjUILgJs1sNcjSYUVTY+ogHELAkEAxQpZ3ze/iGgnJ8P5
rpsdWvq2wyDQtWae5DOJaMlbfcb87ST4QeI+5jyfyW5tFLxwp3OjMM5bMMdbkqp+
3Npo5QJBALj9XA3YYuW/5qzfy4gV+wWWrcgxbWUYiYLF67DYS+QmfweaS1EVAGaZ
Ccz6MMEm7y4OJ5gRpHQISZgZmxA39LMCQEX9UkUOuVMLo/TlENhrsraxXZ/cFSaK
SCBJCiMVexEnUIov2psUcUE5AYz8U3TNJrrkQXBBrHScK7PiKePrFN0CQCyMULeW
3lMhSRTs/xC0OaWx/5zGGrpd+sRW/hutQoBkBRZkRFG6kLn2yAY8lCjnkYLziVfT
sgwnxJMso3vdqs0=
-----END PRIVATE KEY-----';
private static $PUBLIC_KEY = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyAojs0Ow+Nqm2GUVaDBMtiQoN
DuCglqRahe7U8rkRWXOKP/8B7XAHsZj+QpF53fvlZ/eyBcVIjfvxAvn1WSLs+soX
amQuPpIqLLhWwPZQ/Rtcwgn7ougMh/fipFMO8Ea0dw7HQa3g/SQgi7yiaJNQ9Rt3
oN6GVTbQQ43loC+W1wIDAQAB
-----END PUBLIC KEY-----
';
/**
* 获取私钥
* @return bool|resource
*/
private static function getPrivateKey() {
$privKey = self::$PRIVATE_KEY;
return openssl_pkey_get_private($privKey);
}
/**
* 获取公钥
* @return bool|resource
*/
private static function getPublicKey() {
$publicKey = self::$PUBLIC_KEY;
return openssl_pkey_get_public($publicKey);
}
/**
* 私钥加密
* @param string $data
* @return null|string
*/
public static function privEncrypt($data = '') {
if (!is_string($data)) {
return null;
}
return openssl_private_encrypt($data, $encrypted, self::getPrivateKey()) ? base64_encode($encrypted) : null;
}
/**
* 公钥加密
* @param string $data
* @return null|string
*/
public static function publicEncrypt($data = '') {
if (!is_string($data)) {
return null;
}
return openssl_public_encrypt($data, $encrypted, self::getPublicKey()) ? base64_encode($encrypted) : null;
}
/**
* 私钥解密
* @param string $encrypted
* @return null
*/
public static function privDecrypt($encrypted = '') {
if (!is_string($encrypted)) {
return null;
}
return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::getPrivateKey())) ? $decrypted : null;
}
/**
* 公钥解密
* @param string $encrypted
* @return null
*/
public static function publicDecrypt($encrypted = '') {
if (!is_string($encrypted)) {
return null;
}
return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, self::getPublicKey())) ? $decrypted : null;
}
}
测试(这里的key是我上面用iOS公钥加密生成的)
运行代码解密成功