2012-12-04 4 views
1

다른 StackOverflow 사용자가 작성한 RSA 암호화/암호 해독에 대한 설명이 나왔습니다. 코드를 구현했습니다. 지금까지는 암호화에 문제가 없습니다. 난 해독을 구현하고 지금까지 어떤 오류도 발생하지 않았다.Java에서 파일 쓰기/저장 문제가 발생했습니다.

그러나 암호 해독의 OutputStream.write 기능은 가정 된 해독 된 파일을 디렉토리에 저장하거나 쓰지 않습니다. 오류없이 실행되지만 암호화와 달리 파일을 반환하지 않습니다. 암호화 된 파일을 해독하는 것과 동일한 방법을 사용하지만 파일을 반환하지 않습니다. 오류없이 실행됩니다.

다음은 나의 암호 해독 코드입니다. 암호화/암호 해독을 위해 다른 클래스를 사용합니다. 또한 예제와 달리 두 클래스 모두에서 loadkey 함수를 사용합니다. 내가 참조로 사용되는 대답

링크 : Issues in RSA encryption in Java 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 
    } 
} 
+1

사이드 노트 : 당신의'loadKey' 메소드는 아무것도하지 않는'try/catch'를 가지고 있습니다. 최소한 catch 문 안에 예외를 기록하십시오. 또한,'decrypt' 메쏘드에서'CipherInputStream'을 닫지 않습니다. –

+0

또한 출력 스트림에 쓸 때마다 새 스레드를 시작하는 이유는 무엇입니까? –

답변

1

나는 거기에 새 스레드의 시작에 의해 던져진 예외가 있었고이 잡혔다 같은데요 loadkey() 메서드를 사용하지만 닫히지 않은 파일의 출력을 짧게 잘라내는 것은 아닙니다.

관련 문제