2017-03-15 66 views
0

我試圖解密的服務器的消息 - 我得到的錯誤是使用javax.crypto.BadPaddingException:鑑於最終塊未正確填充 - DES Decrytion

加密技術 - DES。

螺紋--Exception「主」 javax.crypto.BadPaddingException:鑑於最終塊未正確填充

我有一個非常困難的時間試圖解決這個問題, 任何幫助將是理解

class TCPClient { 
public static void main(String argv[]) throws Exception { 
    byte[] sentence, textEncrypted; 
    String modifiedSentence; 
    String password; 
    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 
    Socket clientSocket = new Socket("localhost", 6789); 
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); 
    password = "Passcode"; 
    byte[] salt = new byte[64]; 
    Random rnd = new Random(); 
    rnd.nextBytes(salt); 
    byte[] data = deriveKey(password, salt, 64); 

    // BufferedReader inFromServer = new BufferedReader(new 
    // InputStreamReader(clientSocket.getInputStream())); 
    System.out.println("Enter the Data to be transmisted to server\n"); 
    sentence = inFromUser.readLine().getBytes(); 
    SecretKey desKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(data)); 
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 
    cipher.init(Cipher.ENCRYPT_MODE, desKey); 
    textEncrypted = cipher.doFinal(sentence); 
    outToServer.writeBytes(new String(textEncrypted) + '\n'); 
    clientSocket.close(); 
} 

public static byte[] deriveKey(String password, byte[] salt, int keyLen) { 
    SecretKeyFactory kf = null; 
    try { 
     kf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); 
    } catch (NoSuchAlgorithmException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    KeySpec specs = new PBEKeySpec(password.toCharArray(), salt, 1024, keyLen); 
    SecretKey key = null; 
    try { 
     key = kf.generateSecret(specs); 
    } catch (InvalidKeySpecException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return key.getEncoded(); 
} 
} 

服務器端代碼

class TCPServer { 
public static void main(String argv[]) throws Exception { 
    String password = null; 
    String capitalizedSentence; 
    ServerSocket welcomeSocket = new ServerSocket(6789); 

    while (true) { 
     Socket connectionSocket = welcomeSocket.accept(); 
     BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
     password = "Passcode"; 
     byte[] salt = new byte[64]; 
     Random rnd = new Random(); 
     rnd.nextBytes(salt); 
     byte[] data = deriveKey(password, salt, 64); 
     byte [] EncyptedText = inFromClient.readLine().getBytes(); 
     System.out.println("Received Encrypted message " + EncyptedText); 
     SecretKey desKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(data)); 
     Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 
     cipher.init(Cipher.DECRYPT_MODE, desKey); 
     // Decrypt the text 
     System.out.println("Text Received " + EncyptedText); 
     byte[] textDecrypted = cipher.doFinal(EncyptedText); 
     System.out.println("Text Decryted : " + new String(textDecrypted)); 

    } 
} 

public static byte[] deriveKey(String password, byte[] salt, int keyLen) { 
     SecretKeyFactory kf = null; 
     try { 
      kf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); 
     } catch (NoSuchAlgorithmException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     KeySpec specs = new PBEKeySpec(password.toCharArray(), salt, 1024, keyLen); 
     SecretKey key = null; 
     try { 
      key = kf.generateSecret(specs); 
     } catch (InvalidKeySpecException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return key.getEncoded(); 
} 
} 
+0

只是在這裏頭腦風暴,但你的客戶端鹽是從服務器端不同。這不會導致任何問題嗎? –

+0

我曾嘗試使用鹽,但我最終以相同的錯誤:( –

回答

0

這樣做會丟失數據:

outToServer.writeBytes(new String(textEncrypted) + '\n'); 

除此之外,沒有必要。密文不是現代密碼的真正文本,它是二元的。由於套接字提供二進制InputStream s和OutputStream s,所以根本沒有理由將密文轉換爲字符串。所需要的只是將可能的輸入字符串轉換爲二進制(當然,在客戶端和服務器上都使用相同的編碼,現在首選UTF-8)。

+0

在修改的代碼中,我寫字節,而不是通過TCP發送字符串。唯一的問題是我面臨的是在服務器上派生密鑰,請建議我的方式如何在系統之間共享密鑰 –

+0

嗯,在這種情況下,您可能希望共享一個包含密鑰的二進制文件,我的意思是,沒有任何內容是安全的,所以我認爲這只是測試。SecretKey.getEncoded(), KeyFactory ...但這是另一個問題。爲了安全起見,請使用TLS和PKI方案。 –

相關問題