2013-04-25 30 views
0

我相信我正在做這個權利。我怎麼看解密與RSA文件:RSA解密打印NULL

  1. 讀取該文件的每一行作爲String
  2. 設置cipher.init(Cipher.DECRYPT_MODE, privateKey)
  3. 字符串轉換爲char[]使用Hex.decodeHex(String.toCharArray())
  4. 最後做cipher.doFinal(x)

聽起來來嗎?我正在這樣做,但它不起作用,DecryptedFile.txt只是兩行「null」。

我能夠使用幾乎相同的確切過程進行加密,但顯然與cipher.init(Cipher.ENCRYPT_MODE, publicKey)

這裏是我的代碼

try { 
     BufferedReader inStream = new BufferedReader (new FileReader(cryptoFile)); 

     int k = 0; 

     fileContents.add(inStream.readLine()); 

     while(fileContents.get(k) != null) { 
      k++; 
      fileContents.add(inStream.readLine()); 
     } 

     inStream.close(); 

     try { 

      PrivateKey privateKey = kp.getPrivate(); 
      Cipher cipher = Cipher.getInstance("RSA"); 
      cipher.init(Cipher.DECRYPT_MODE, privateKey); 

      int j = 0; 

      while(fileContents.get(j) != null) { 

       String text = fileContents.get(j); 

       try {       
        x = Hex.decodeHex(text.toCharArray()); 
        y = cipher.doFinal(x); 
       } catch (DecoderException ex) { 
        Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, ex); 
       } 

       try (PrintWriter file = new PrintWriter(
         new BufferedWriter(
         new FileWriter("DecryptedFile.txt", true)))) { 
        file.println(y); 
       } catch (IOException e) { 
        System.err.println("IOERROR: " + e.getMessage() + "\n"); 
       } 

       j++; 
      } 

     } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { 
      Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } catch (FileNotFoundException e) { 
     System.err.println("IOERROR: File NOT Found: " + cryptoFile + "\n"); 
    } catch (IOException e) { 
     System.err.println("IOERROR: " + e.getMessage() + "\n"); 
    } finally { 
     messagePane.setText(messagePane.getText() + "\n\n" 
       + cryptoFile + "is done being decrypted."); 
     messagePane.setText(messagePane.getText() + "\n" 
       + "Decrypted file saved to \'DecryptedFile.txt\'."); 

     cryptoFile = ""; 
     pathTextField.setText(cryptoFile); 
     encryptButton.setEnabled(false); 
     decryptButton.setEnabled(false); 

    } 
+0

RSA實際上是一個緩慢的算法 - 通常你不會用它來加密文件,而是用AES加密文件,然後用RSA加密AES密鑰。 – 2013-04-25 01:26:44

+0

@ Zim-ZamO'Pootertoot我必須爲我班的最終項目編寫我選擇的程序,並且我們被告知必須在其中使用RSA。 – pattmorter 2013-04-25 01:29:46

+0

嘿,在這種情況下,不用擔心 - 只是不要在生產代碼 – 2013-04-25 01:30:22

回答

1

您正在使用從FileContents衍生出的字符串派生的字符數組的密碼 - 這大概是搞砸了它的編碼。相反,從文件讀取一個字節數組並將其用作密碼的輸入。

這是一個好主意,墊輸入必要時 - 使用Cipher.getInstance("RSA/ECB/PKCS1Padding")

始終指定的字符編碼(例如str.getBytes("UTF-8"))將字符串轉換成字節,並且反之亦然時 - 不同的JVM使用不同的默認字符編碼。

+0

如果我告訴你,我一行一行地加密文件,然後保存? – pattmorter 2013-04-25 01:48:43

+0

這取決於你如何保存數據。如果你使用一個字符串,將其轉換爲byte [],對其進行加密並將其保存到一個文件中,那麼您需要讀取與解密函數輸入相同的字節[]。我不確定最好的辦法是做什麼;您可能需要更改輸出功能以便更容易地重新讀取數據,例如將字符串轉換爲byte [],對其進行加密,保存其長度,然後保存字節數組,以便將文件保存爲例如。 120 [120個字節的數組];現在您可以在120中讀取,然後在接下來的120個字節中讀取,然後解密它們。 – 2013-04-25 01:54:16

+0

最好使長度字段固定大小,可能兩個字節應該這樣做,所以它會是[0] [120] [120字節的數據]。不要忘記,字節是用Java簽名的,所以如果你不小心的話,[128] [128字節的數據]將以-127字節的數據讀取。 – 2013-04-25 01:55:32