(1)创建RSA私钥,java端使用
openssl genrsa -out private_key.pem 1024
(2)创建RSA公钥
openssl rsa -in private_key.pem -pubout -out public_key.pem
(3)创建证书请求(按照提示输入信息)
openssl req -new -out cert.csr -key private_key.pem
(4)自签署根证书
openssl x509 -req -in cert.csr -out public_key.der -outform der -signkey private_key.pem -days 3650
(5)用java代码要从这个文件中得到想要的privatekey 可以先用命令(就被这东西卡住了)
私钥转格式,在iOS端使用私钥解密时用这个私钥
openssl pkcs8 -topk8 -inform PEM -outform DER -in private_key.pem -out private_pkcs8_der.key -nocrypt
(6)将 public_key.der 文件转成 base64的字符串
openssl base64 -in public_key.der
MIICZzCCAdACCQDop1LFiuM/hTANBgkqhkiG9w0BAQUFADB4MQswCQYDVQQGEwJD
aDELMAkGA1UECBMCR0QxCzAJBgNVBAcTAlNaMQ0wCwYDVQQKEwRRdWx2MQ0wCwYD
VQQLEwRRdWx2MQ8wDQYDVQQDEwZ4aWFveXUxIDAeBgkqhkiG9w0BCQEWETExODI4
NjkzNTNAcXEuY29tMB4XDTE2MDcxMzA4Mzg0NVoXDTI2MDcxMTA4Mzg0NVoweDEL
MAkGA1UEBhMCQ2gxCzAJBgNVBAgTAkdEMQswCQYDVQQHEwJTWjENMAsGA1UEChME
UXVsdjENMAsGA1UECxMEUXVsdjEPMA0GA1UEAxMGeGlhb3l1MSAwHgYJKoZIhvcN
AQkBFhExMTgyODY5MzUzQHFxLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC
gYEAvN/YKLEmhPWLUEvyWS+jXQeXWVSmBiHfdFgkoSxaz84k1gssxk2spNj+e2l+
J2gWi9phqB0wYjzJj4rm4Bi2E/LP5dG20IrJyem22KX4ENvOyMeRFECdPLhWQGxG
tdWlYan+/hEZzqpXjixuDUFvmk0mf96f2Y/JTmiuB/naDKcCAwEAATANBgkqhkiG
9w0BAQUFAAOBgQB0ERXAmXqXpXu8N/0w93IbxhHs1We5vbMYb0vIEPVQh4E2dXqf
UNsU39l6Q5wZlmWG3g6vWqhVLQOf0vB44UacABMVVu+EjzJuTGaEfQbIlS6Xrz12
VT5hFoSg+oi/vB2rX6ttJSN8WWaGJsa43lkNPfyGJnrgFsJhfqTAegv3gg==
其中第五步我没有用啦,我们后台是用 .net 写的,将 private_key.pem 和 public_key.pem 给后台,
iOS 这边使用 的是 public_key.der 生存的公钥,或者直接使用 public_key.der 文件
终端运行图片
下载请戳RSADemo
参考文章:
http://geniuswxk.iteye.com/blog/2182713