我是Java编程的初学者。我的代码对从文本文件中提取的数据进行加密,然后使用RSA算法将其存储在另一个文件中。我想通过使用KeyStore类(http://download.oracle.com/javase/1,5.0/docs/api/java/security/KeyStore.html)及其嵌套类-KeyStore来保护我的私钥密码。 PrivateKeyEntry(http://download.oracle.com/javase/1,5.0/docs/api/java/security/KeyStore.PrivateKeyEntry.html)。
KeyStore.PrivateKeyEntry的构造函数需要使用Certificate []数组,但我不确定如何生成此Certificate []数组。
到目前为止,我将附加我的代码以及问题:
这是加密代码。
public class Fileencrypt {
public static void main(String args[]) throws IOException, InvalidKeyException, java.security.InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, KeyStoreException, CertificateException, CertificateEncodingException, IllegalStateException, NoSuchProviderException, SignatureException, UnrecoverableKeyException{
try{
byte[] plainData;
byte[] encryptedData = null;
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
try {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
FileOutputStream fos = new FileOutputStream("C:\\Output\\Publickey.txt");
fos.write(x509EncodedKeySpec.getEncoded());
fos.close();
// Store Private Key.
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = …Run Code Online (Sandbox Code Playgroud)