1

我遇到了另一个 StackOverflow 用户编写的关于 RSA 加密/解密的解释,我实现了代码。到目前为止,加密没有问题。我也实现了解密,到目前为止没有收到任何错误。

但是,OutputStream.write解密的功能不是在目录中保存/写入假定的解密文件。它运行没有错误,但与加密不同的是,它不返回任何文件。我使用与解密相同的方法来编写加密文件,但它不返回任何文件。它也可以正常运行。

下面是我的解密代码。请注意,我使用不同的类进行加密/解密。同样与示例不同的是,我loadkey在两个类中都使用了该函数。

链接到我用作参考的答案:Java 类中的 RSA 加密问题

我的解密类:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  try {
    String key=jTextField2.getText();
    String cleartextFile = "C:\\keys\\naan.docx";
    String ciphertextFile =  jTextField1.getText();
    loadKey(new File(key), "C:\\keys\\private.key");
    decrypt(new File(ciphertextFile), new File("cleartextFile"));

  } catch (GeneralSecurityException ex) {
    Logger.getLogger(OpenFileDec.class.getName()).log(Level.SEVERE, null, ex);
  } catch (IOException ex) {
    Logger.getLogger(OpenFileDec.class.getName()).log(Level.SEVERE, null, ex);
  }

} 
private PrivateKey readPrivateKeyFromFile(String keyFileName) throws Exception {
  InputStream in = new FileInputStream(keyFileName);
  ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
  try {
    BigInteger m = (BigInteger) oin.readObject();
    BigInteger e = (BigInteger) oin.readObject();
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PrivateKey pubKey = fact.generatePrivate(keySpec);
    return pubKey;
  } catch (Exception e) {
    throw new RuntimeException("Spurious serialisation error", e);
  } finally {
    oin.close();
  }
}

public void loadKey(File in, String privateKeyFile) throws GeneralSecurityException, IOException {
  try {
    // read private key to be used to decrypt the AES key
    byte[] encodedKey = new byte[(int)privateKeyFile.length()];
    new FileInputStream(privateKeyFile).read(encodedKey);

    // create private key
    //PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey);
    //KeyFactory kf = KeyFactory.getInstance("RSA");
    //PrivateKey pk = kf.generatePrivate(privateKeySpec);
    PrivateKey pk = this.readPrivateKeyFromFile(privateKeyFile);

    // read AES key
    pkCipher.init(Cipher.DECRYPT_MODE, pk);
    aesKey = new byte[AES_Key_Size/8];
    CipherInputStream is = new CipherInputStream(new FileInputStream(in), pkCipher);
    is.read(aesKey);
    aeskeySpec = new SecretKeySpec(aesKey, "AES");     
  } catch (Exception e) {

  }      
} 


public void decrypt(File in, File out) throws IOException, InvalidKeyException {
  aesCipher.init(Cipher.DECRYPT_MODE, aeskeySpec);

  CipherInputStream is = new CipherInputStream(new FileInputStream(in), aesCipher);
  FileOutputStream os = new FileOutputStream(out);

  copy(is, os);
  os.close();
}

private void copy(InputStream is, OutputStream os) throws IOException {
  int i;
  byte[] b = new byte[1024];
  while((i=is.read(b))!=-1) {
    os.write(b, 0, i);
    new Thread(new thread1()).start(); //Start the thread
  }
}
4

1 回答 1

1

我猜新线程的启动引发了一个异常,它被您的 loadkey() 方法捕获,但不是在将输出缩短到从未关闭的文件之前。

于 2012-12-04T14:41:32.327 回答