java生成的公钥与私钥key转成openssl的rsa算法中的公钥与私钥key

说明:

1.java生成的公私钥格式为 pkcs8, 而openssl默认生成的公私钥格式为 pkcs1,两者的密钥实际上是不能直接互用的

2.java采用的rsa默认补齐方式是pkcs1, 因此互用的时候需要将openssl中的补齐方式设置为RSA_PKCS1_PADDING采用openssl的rsa算法实现公私钥加解密,这里由于项目需求,公私钥是使用java生成的,因此需要在密钥的首行和最后一行添加标记,两者对比如下:

--------java private key-------(本行不存在)
MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAKNQ3j+Ov9THQTMbNw+MwLOGRDwR
qkiH0QffSLBh1Nv6JHBp7yC07b+Nj0lMiBOjoMpk98W3RTdDuRcJ/ojM0OMPZWNiOdvrtJ7I/Haw
DiryMLqWR5gBCC0g+ODs2Cxr1ig5idNaX0bHy9ufSZpFd378OZLBVuOEBtQ7lnFhp/DxAgMBAAEC
gYEAiOBLFPvAHUZOh9XEZPzzuMLMR4gKrrVd5PonguTLsP5KMgknCMN7C3NKZwWEeulF5ruOyh/b
HAFfjEatFNyMKJnUhHwShvvSqncbZMBy9xmaA/FF0BUD285Vb+PhuPnL678KhEcKD3k99A+ym4qW
K9abeiJypAfvnleopBFV+c0CQQDcdUrMdCVCdUIxenIjz67Qp6672iBC2Ni5psow2SZZ9FTC31Z5
7jDowqrvsABbY7XbV8g1LUX/U6JLylXiz7MrAkEAvaU16YtUCdVe4s6saNLnQCfi2ykVdn5ovy3R
ZSn1gzQhy+//GFAhtUTjWO46zBicuNbJ1bNcaiF7DF4evU2OUwJBAJC14WqBlu+f3tpxoLjHG95V
I4qlrOWUR/bdd/1GnTkQ/A6K4c3Zv3saRXBeXG2g7v/TCFV3qWnncTJE18mxQs8CQQCPVxNDvaK8
eADrD2lzJY3SiRNHsWrekDcd4US24RsHm1y8J185gj+oIjRwoEfiweubLgWKN7JvKXU26OD1Fmyv
AkBR63yGMoIMo4AxLnkcr16j9WLBgQ/NXakPHcO2iMtFUsix3SNh96AkxZ+Fs87Ys70RuJOTtA/S
llTOdaKm5PVqpc

--------openssl private key-------(本行不存在)
-----BEGIN RSA PRIVATE KEY-----(存在)
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKIpKYdHzzjEaugD55bBNlV+iJ60
w+C2fecv2ptenw7lUqDRiXTQsMXUiEBPKB5MLtJhLgB9yiph+lIN/lv+7GcPHaEjLwvh0ZnwXiQ2
sbL9sCzvU9r/2otJJ+2NrEf0PA4wEocrSiEv3at7oD3PCVps8nht3fzuPYfCxWhkBXeXAgMBAAEC
gYAm/FZrm6wKLc618RpgeBsj+sPFxBfDkpw6VJrt95Wt8xwmZbnyR6n4JdyL+XvfrNkk4xA4NgXA
osWXrJ2WaHdU/J0C1NnVzwMSISojR3+w5nsY5xyfdhPCNthlWnlmYTXJNOaQUGVO3oR1q0be+drt
uQUV6yvsw/JDftskprqToQJBAOvObGbnz36YgdClJCVHZj3vFDlblAOiOFzIPq0hx72FpYcj/Ho3
3o5bAfZnHPMvOhbMaeY1xpm423f78qV2A+MCQQCwDDWM0uxvTA2XbpQGtVupZbqh/iZQ2eMYp8FJ
t4aa/brUesXZurqsHWcrM6HrWp7gA6DzYeSSy6cCeVV6dlO9AkBaW7JMSH3JO/Valz1fj5mNGAT+
6amxLsM8QntJ6ozYgdLpExAXYTtF/3ekDOUaBrnSrKfT0D13AFARDRlw7+UTAkAO7hokhUbT8Yk9
jH01THAnn58CWgF53eCqmJ+F1vCteFM97eOVoYZvTlcPjONwiFDtAqHFwkmWQXs6dHvRVPOpAkEA
jalQvMpQM7Ej3s2oK386LVrH/La0hZZ6Fn4bgCJ26YxB3AGN5J8uHLjU5oBUMxDefc0OraDdyStJ
pAh2vYzETw==
-----END RSA PRIVATE KEY-----


下面分别用c与java语言实现了将一个字符串转化成对应的公钥与私结构体:


openssl的rsa算法中的公钥与私钥key获取 :




java取得publickey与privatekey :




参考网址:

java与openssl的rsa算法互用



你可能感兴趣的:(java生成的公钥与私钥key转成openssl的rsa算法中的公钥与私钥key)