加密传输的数据,采用RSA算法,RSA是一种非对称算法,算法需要一对密钥,使用其中一个加密,需要使用另外一个才能解密。有可私钥可以直接生产公钥。
iOS SDK并不支持私钥加密,公钥解密。可以通过OPENSSL第三方库解决。
使用openssl命令行生成密钥对
openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem
根据提示,填入私钥的密码(代码中需要用到),签名证书的组织名、邮件等信息之后,就会生成包含有公钥的证书文件public_key.der合私钥文件private_key.pem。
public_key.der文件用于分发到iOS客户端进行公钥加解密,而private_key.pem文件留在服务器端供php使用。当然,为了在服务器端进行加解密测试,那么我们还可以生成一个服务器端PHP使用的pem公钥文件:
openssl rsa -in private_key.pem -pubout -out public_key.pem
上面这个命令就会根据输入的私钥文件生成pem格式的公钥文件了。从这里也可以看到,根据私钥,我们是可以生成相对应的公钥的,这也就是为什么我们要把公钥放在客户端,而不是私钥放在客户端的原因了。
服务器端PHP的加解密测试代码
<?php
class RSAEncryptTest {
const PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,C222ABDFB89D4349
qStrPtGxMrB24Q/yHwNhVpq5CjPKThCTPRnicXWYm1mAxBG2iR5T5E05pROzsPur
YhFr+PUb+C6WI9pYLYbqax1k59CZG63YP2SLAvYftgSaAjmvuZzlvMnBXA/yqioL
Fab1f4vNavZTBiOrXFsO7ryygAJub9GLx2GEr/RoJgnvHbqz2yknF0EmylsWA1LM
8EypQZQC9eg40W0khhTYgfc488jdam3hCsFvGiELDb/NBEryRa1adVIaREpSbA24
tP2/NQYjW45M+g1PFDqeDRGmPNjeH3z6T1fIbiJIFu2d7SsMUT2PZhFbllapL9nD
jL4x4MfLb00lstvmTNAqK75pIkuEUKr9t+v4hzZjgWW45QNtMGVFHJj36gZzvhdV
YOQP0fk57/UrnDlBijKFyuK7FC/rhyH2IZ+oLfWlBcRnlaPZC3UTrOxyxgJnc5JL
wpdz/bF4beUh8e9paR0liywrNKGIkIvCnGqwPlnaqHW225uKObU6MFcL58cQ8Cx1
3NQ0ulJnUGgWIScC3oYRR+r7atXNbdYkK16dmcav9ay4d2nJ/a2qY/sbgXzjPJve
8eX3oHnQvtyfhUAzVasLelBDoNexNlsAqgIbSYAo0hIm2FtZ0Y+WGAzgMU2G9O0i
NAU8zv8xXExMnMslCFVVIfFCA/cM84Z5t5NYnJchpOYomtvaaApLuKjDQhsIFi+1
W/RvPWOxuhbJkzFPmLuZy2KtcdPTMceJ8qDYeNSgzLZ1RZgj2EvRVGyBHUJrocrv
LZTsUTkkBCeQoCbcKKstwSDpl9mlAMro8b78elIAPYkxgZpD/EjOOw==
-----END RSA PRIVATE KEY-----
";
const PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDXZaD7aXaL6WOGFqfu8M1C55z2
d8gylu2y6jRdyc9wPt14tmkKofJE9vosD4mCRAArw0yS633g0b0Ln9f1IVFg2ZE1
5kBASpQf42D08Jb/L93GHPBUhvFBUy8pUAKwqa6braW/4ZMNj40f5PGhqkyH0VIV
03aeS8yd1lPPMtX1XwIDAQAB
-----END PUBLIC KEY-----";
private static $private_key;
private static $public_key;
public static function private_encrypt($str){
self::setup_key();
if(openssl_private_encrypt($str, $encrypted, self::$private_key))
return $encrypted;
}
public static function private_decrypt($str){
self::setup_key();
if(openssl_private_decrypt($str, $decrypted, self::$private_key))
return $decrypted;
}
public static function public_decrypt($str){
self::setup_key();
if(openssl_public_decrypt($str, $decrypted, self::$public_key))
return $decrypted;
}
public static function public_encrypt($str){
self::setup_key();
if(openssl_public_encrypt($str, $encrypted, self::$public_key))
return $encrypted;
}
private static function setup_key(){
if (!self::$private_key){
// 这里的test就是在生成证书的时候设置的私钥密码
self::$private_key = openssl_pkey_get_private(self::PRIVATE_KEY, "1234");
}
if (!self::$public_key)
self::$public_key = openssl_pkey_get_public(self::PUBLIC_KEY);
}
}
//公钥加密字符串 xxx
$public_encrypt=RSAEncryptTest::public_encrypt("xxx");
echo "public_encrypt:\n";
echo base64_encode($private_encrypt) ;
echo "\n";
//私钥解密
$private_decrypt=RSAEncryptTest::private_decrypt($public_encrypt);
const PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,C222ABDFB89D4349
qStrPtGxMrB24Q/yHwNhVpq5CjPKThCTPRnicXWYm1mAxBG2iR5T5E05pROzsPur
YhFr+PUb+C6WI9pYLYbqax1k59CZG63YP2SLAvYftgSaAjmvuZzlvMnBXA/yqioL
Fab1f4vNavZTBiOrXFsO7ryygAJub9GLx2GEr/RoJgnvHbqz2yknF0EmylsWA1LM
8EypQZQC9eg40W0khhTYgfc488jdam3hCsFvGiELDb/NBEryRa1adVIaREpSbA24
tP2/NQYjW45M+g1PFDqeDRGmPNjeH3z6T1fIbiJIFu2d7SsMUT2PZhFbllapL9nD
jL4x4MfLb00lstvmTNAqK75pIkuEUKr9t+v4hzZjgWW45QNtMGVFHJj36gZzvhdV
YOQP0fk57/UrnDlBijKFyuK7FC/rhyH2IZ+oLfWlBcRnlaPZC3UTrOxyxgJnc5JL
wpdz/bF4beUh8e9paR0liywrNKGIkIvCnGqwPlnaqHW225uKObU6MFcL58cQ8Cx1
3NQ0ulJnUGgWIScC3oYRR+r7atXNbdYkK16dmcav9ay4d2nJ/a2qY/sbgXzjPJve
8eX3oHnQvtyfhUAzVasLelBDoNexNlsAqgIbSYAo0hIm2FtZ0Y+WGAzgMU2G9O0i
NAU8zv8xXExMnMslCFVVIfFCA/cM84Z5t5NYnJchpOYomtvaaApLuKjDQhsIFi+1
W/RvPWOxuhbJkzFPmLuZy2KtcdPTMceJ8qDYeNSgzLZ1RZgj2EvRVGyBHUJrocrv
LZTsUTkkBCeQoCbcKKstwSDpl9mlAMro8b78elIAPYkxgZpD/EjOOw==
-----END RSA PRIVATE KEY-----
";
const PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDXZaD7aXaL6WOGFqfu8M1C55z2
d8gylu2y6jRdyc9wPt14tmkKofJE9vosD4mCRAArw0yS633g0b0Ln9f1IVFg2ZE1
5kBASpQf42D08Jb/L93GHPBUhvFBUy8pUAKwqa6braW/4ZMNj40f5PGhqkyH0VIV
03aeS8yd1lPPMtX1XwIDAQAB
-----END PUBLIC KEY-----";
private static $private_key;
private static $public_key;
public static function private_encrypt($str){
self::setup_key();
if(openssl_private_encrypt($str, $encrypted, self::$private_key))
return $encrypted;
}
public static function private_decrypt($str){
self::setup_key();
if(openssl_private_decrypt($str, $decrypted, self::$private_key))
return $decrypted;
}
public static function public_decrypt($str){
self::setup_key();
if(openssl_public_decrypt($str, $decrypted, self::$public_key))
return $decrypted;
}
public static function public_encrypt($str){
self::setup_key();
if(openssl_public_encrypt($str, $encrypted, self::$public_key))
return $encrypted;
}
private static function setup_key(){
if (!self::$private_key){
// 这里的test就是在生成证书的时候设置的私钥密码
self::$private_key = openssl_pkey_get_private(self::PRIVATE_KEY, "1234");
}
if (!self::$public_key)
self::$public_key = openssl_pkey_get_public(self::PUBLIC_KEY);
}
}
//公钥加密字符串 xxx
$public_encrypt=RSAEncryptTest::public_encrypt("xxx");
echo "public_encrypt:\n";
echo base64_encode($private_encrypt) ;
echo "\n";
//私钥解密
$private_decrypt=RSAEncryptTest::private_decrypt($public_encrypt);
echo "private_decrypt:\n";
echo$private_decrypt;
echo "\n";
iOS端
参考链接:
http://blog.yorkgu.me/2011/10/27/rsa-in-ios-using-publick-key-generated-by-openssl/