如何从编码数组再次生成KeyPair或Key对象?

时间:2014-11-11 16:04:25

标签: java encryption

我是新用的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;
}

0 个答案:

没有答案