将字符串转换为PrivateKey不会在android中提供相同的密钥



我使用以下函数将PrivateKey转换为String,反之亦然:

private static String privateKeyToString(PrivateKey privateKey)
{
byte[] privateKeyByte = privateKey.getEncoded();
String privateKeyString = Base64.encodeToString(privateKeyByte, Base64.NO_WRAP);
return privateKeyString;
}
private static PrivateKey stringToPrivateKey(String privateKeyString)
{
byte[] privateKeyByteServer = Base64.decode(privateKeyString, Base64.NO_WRAP);
// generate the publicKey
KeyFactory keyFactory = null;
try {
keyFactory = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
PrivateKey privateKeyServer = null;
try {
privateKeyServer = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyByteServer));
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return privateKeyServer;
} //To generate RSA Keys
private static Map<String, Object> getRSAKeys() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
Map<String, Object> keys = new HashMap<String, Object>();
keys.put("private", privateKey);
keys.put("public", publicKey);
return keys;
}

现在用CCD_ 1生成的密钥是"1";OpenSSLRSAPrivateKey";而原始私钥是"0";OpenSSLRSAPrivateCRTKey";并且这些键不是相同的。如何在转换后获得相同的密钥?

原始密钥:

OpenSSLRSAPrivateCrtKey{modulus=c44f17124a2dc42553d78ba59664480fdabfbb38f9ade78ffbff23c681f9483c160c9336f466d5f19960ec1449c3f9aea557913b179218db941f60e60ce77b0b15e5a74d8e5eeb23df5081ffdff6f40c69488b8230ecd3fae17f680e9b6a194697a03671ac886d32dc68aafb77a269a167d3ae339d24cdfb5d0a17d9bbbcd037a5b30148d7a1d1281139020b1158df4b343c3ebb2b31ba1c93151801bbfc6e7ad552c18b12e7c868a44689b4a8af5ecea07fa0dfb1a65385647b21fa6383bb8b927044565dfac3154bfc8c7e51c9032d8dab211ba41de78f4061f8683f7bc649646c55dbb14c2ac2c681d6ff72b15b08eea5d46d6c515a208c7e44794dd67f67,publicExponent=10001}

转换后的密钥

OpenSSLRSAPrivateKey{modulus=c44f17124a2dc42553d78ba59664480fdabfbb38f9ade78ffbff23c681f9483c160c9336f466d5f19960ec1449c3f9aea557913b179218db941f60e60ce77b0b15e5a74d8e5eeb23df5081ffdff6f40c69488b8230ecd3fae17f680e9b6a194697a03671ac886d32dc68aafb77a269a167d3ae339d24cdfb5d0a17d9bbbcd037a5b30148d7a1d1281139020b1158df4b343c3ebb2b31ba1c93151801bbfc6e7ad552c18b12e7c868a44689b4a8af5ecea07fa0dfb1a65385647b21fa6383bb8b927044565dfac3154bfc8c7e51c9032d8dab211ba41de78f4061f8683f7bc649646c55dbb14c2ac2c681d6ff72b15b08eea5d46d6c515a208c7e44794dd67f67

使用此方法获取字符串

public String getPrivateKey(String option)
throws NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
IllegalBlockSizeException,
BadPaddingException {
switch (option) {
case "pkcs1-pem":
String pkcs1pem = "-----BEGIN RSA PUBLIC KEY-----n";
pkcs1pem += Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);
pkcs1pem += "-----END RSA PUBLIC KEY-----";
return pkcs1pem;
case "pkcs8-pem":
String pkcs8pem = "-----BEGIN PUBLIC KEY-----n";
pkcs8pem += Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);
pkcs8pem += "-----END PUBLIC KEY-----";
return pkcs8pem;
case "base64":
return Base64.encodeToString(privateKey.getEncoded(), Base64.DEFAULT);
default:
return null;
}
}

并使用此方法获取privateKey

public static PrivateKey stringToPrivate(String private_key)
throws NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
IllegalBlockSizeException,
BadPaddingException {
try {
// Read in the key into a String
StringBuilder pkcs8Lines = new StringBuilder();
BufferedReader rdr = new BufferedReader(new StringReader(private_key));
String line;
while ((line = rdr.readLine()) != null) {
pkcs8Lines.append(line);
}
// Remove the "BEGIN" and "END" lines, as well as any whitespace
String pkcs8Pem = pkcs8Lines.toString();
pkcs8Pem = pkcs8Pem.replace("-----BEGIN PRIVATE KEY-----", "");
pkcs8Pem = pkcs8Pem.replace("-----END PRIVATE KEY-----", "");
pkcs8Pem = pkcs8Pem.replaceAll("\s+", "");
// Base64 decode the result
byte[] pkcs8EncodedBytes = Base64.decode(pkcs8Pem, Base64.DEFAULT);
// extract the private key
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privKey = kf.generatePrivate(keySpec);
return privKey;
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
}
return null;

}

最新更新