C++調用openssl庫生成的祕鑰對,經過傳輸傳出來的只有祕鑰的內容,沒有祕鑰的格式。而咱們在調用openssl庫加密解密時,傳入的祕鑰是須要包含格式的。C++調用openssl庫須要的格式爲pkcs#1, java默認的格式爲pkcs#8。html
下面的代碼,僅僅是添加收尾標識,並不是對密匙內容作轉換。java
//pkcs#1格式的私鑰 64位分行 + 首尾標誌 std::string formatPrivateKeyPKCS1(std::string priKey ) { std::string strPrivateKey = priKey; { //語句塊做用:讀取內存裏生成的祕鑰對,再從內存生成rsa int nPrivateKeyLen = strPrivateKey.size(); for(int i = 64; i < nPrivateKeyLen; i+=64) { if(strPrivateKey[i] != '\n') { strPrivateKey.insert(i, "\n"); } i++; } strPrivateKey.insert(0, "-----BEGIN RSA PRIVATE KEY-----\n"); strPrivateKey.append("\n-----END RSA PRIVATE KEY-----\n"); } return strPrivateKey; } //pkcs#1格式的公鑰 64位分行 + 首尾標誌 std::string formatPublicKeyPKCS1(std::string pubKey ) { std::string strPublicKey = pubKey; { //語句塊做用:讀取內存裏生成的祕鑰對,再從內存生成rsa int nPublicKeyLen = strPublicKey.size(); for(int i = 64; i < nPublicKeyLen; i+=64) { if(strPublicKey[i] != '\n') { strPublicKey.insert(i, "\n"); } i++; } strPublicKey.insert(0, "-----BEGIN RSA PUBLIC KEY-----\n"); strPublicKey.append("\n-----END RSA PUBLIC KEY-----\n"); } return strPublicKey; }
附1:rsa密匙對生成web
附 2:rsa祕鑰對在線格式轉換app