本文介绍了从字节数组创建PrivateKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从字节数组生成PrivateKey?我得到这个字节数组使用getEncoded()方法,但现在我必须将其转换回PrivateKey。
Is there a way to generate PrivateKey from byte array? I got this byte array using getEncoded() method, but now I have to convert it back to PrivateKey.
谢谢,
Vuk
Thanks,Vuk
推荐答案
我一直在寻找这个答案,终于找到了。 keyBytes是一个最初用getEncoded()创建的字节数组。
I was looking for this answer too and finally found it. keyBytes is a byte array originally created with getEncoded().
//add BouncyCastle as a provider if you want
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
//create a keyfactory - use whichever algorithm and provider
KeyFactory kf = KeyFactory.getInstance("DSA", "BC");
//for private keys use PKCS8EncodedKeySpec; for public keys use X509EncodedKeySpec
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(keyBytes);
PrivateKey pk = kf.generatePrivate(ks);
我从来没有为JavaCard做任何事情,只是从这,看起来你可以使用KeyFactory类。您可能需要下载BouncyCastle库。
I've never done anything for JavaCard but from this post, it looks like you can use the KeyFactory class. You'll probably need to download the BouncyCastle library.
这篇关于从字节数组创建PrivateKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!