简体   繁体   中英

Issue with writing/saving file in Java

I came across an explanation on RSA encryption/decryption written by another StackOverflow user and I implemented the code. So far, there is no problem with the encryption. I implemented the decryption as well and didn't get any error so far.

However, the OutputStream.write function of the decryption is not saving/writing the supposed decrypted file in the directory. It runs without errors, but unlike the encryption, it does not return any file. I use the same method in writing the encrypted file as the decryption, but its not returning any file. It runs without error as well.

Below is my decryption code. Please note I use different classes for encryption/decryption. Also unlike the example, I use the loadkey function in both the two classes.

Link to the answer I used as reference: Issues in RSA encryption in Java class

My decryption class:

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
  }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM