1. 程式人生 > >Android RSA公鑰加密

Android RSA公鑰加密

1、openssl生成公司鑰對 
openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem 

生成私鑰:private_key.pem 
公鑰:public_key.der 

2、將private_key.pem轉換成PKCS8Padding格式 
openssl pkcs8 -topk8 -inform PEM -in private_key.pem -outform PEM -nocrypt 
返回一串字元如: 
-----BEGIN PRIVATE KEY----- 
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKwerXN6yQc0hOyP 

aqQkTA0kANym5cLgoTUvCk3b3DDn2fQvfXjLqPx/lzYDRIQljO9wqOEL8bpTas6C 
ymN7Ioq/a0uNpYOimp7inlfYiwr/3ju1ZBtg7w9WDZUMivlPuEATbsj5Z0ZUMeyk 
7Mi2vW2hGx0rROVbiXpx7u6bu54BAgMBAAECgYEApAB/KV+/mPv0BCwLYuerB4Jc 
x+LHXs8HDFQML2/k+TySMtflaGo2VJGGlVK7wGa/SY9Be14UMIQ3axq4md3EZHuy 
ibnFfPJHHfSubGxpg+IRUvtuUExTpZ9GT4y6depEWGllmR4CHZVt100tkOYAeDsl 

suE3UmYHTHr20tqJ/BkCQQDdc1+qIV/VM+S49yYoFWvAt/bF80HyNMJJZLwcwzP2 
NC/i8QS8AqtbkayLTsKGMc+Naw2UE7a7hOH4UwWmFb9rAkEAxvkOJsSuGv9FK7nV 
k83NT5NURdAXTNlu4AYmTIdCA4XW8+I0BxoPmuNhqbZuus8i0D4LWREb4F2YNiqM 
2onPQwJARO6U6LBa1FLC75B/IBsK34JkHloKPQqPL6lbpNheZFT6UvoTLzLlhCtk 
OjjZ9Eus/6hDIcTBLL3AWTv+liRO4QJAMFlSo/8E45CGCWMFz/2hVyXQw4oWL1li 

hNNZuRzg4KREIE0mIVem1RdNowSukXxF1F5POZxbrsBvfTIHug7kqwJABumKLTAs 
tRKj4mXmH9tyUFCOrwRDqY/XIC9NLu+l7X1WfdgbSi++OQ0qa2LV6dHQhHNdEYmt 
sh2JnjcySwHROg== 
-----END PRIVATE KEY----- 
取出-----BEGIN PRIVATE KEY-----和-----END PRIVATE KEY-----之間的字元儲存,做為解密的私鑰串。 

3、將生成的公鑰DER檔案BASE64編碼 
openssl base64 -in public_key.der 
生成的字串做為IOS的公鑰 

4、從已有的私鑰中,匯出android使用的公鑰 
openssl rsa -in private_key.pem -pubout -out rsa_public_key.pem