我是新用的java.security。*
我使用此代码创建了一个密钥对:
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
SecureRandom random = new SecureRandom();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
generator.initialize(256, random);
KeyPair pair = generator.generateKeyPair();
byte[] public = pair.getPublic().getEncoded();
byte[] private = pair.getPrivate().getEncoded();
现在,如何仅使用编码数组再次创建KeyPair对象?那可能吗?我意识到这将是:
public KeyPair getKeyPairFromEncoded(byte[] public, byte[] private) {
KeyPair result = new KeyPair(null, null);
result.setPublicFromEncoded(public);
result.setPrivateFromEncoded(private);
return result;
}
如果可能的话,另一种解决方案是能够获得Key对象:
public Key getKeyFromEncoded(byte[] encoded) {
Key result = new Key(); //<== I know... This is not possible
result.setKeyFromEncoded(encoded);
return result;
}