2017-03-15 2 views
0

は、私は、サーバー上のメッセージを解読しようとしています - DES - 私が得たもの誤差が使用javax.crypto.BadPaddingException:最後のブロックきちんと埋められない考える - DES Decrytion

暗号化技術です。任意のヘルプはなりますが与えられ、最終的なブロックが正しく

を埋めていない私は、この問題を解決しようとする非常に困難な時間を過ごしています、 :スレッド「メイン」javax.crypto.BadPaddingExceptionで

--Exception理解

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'); 

それ以外に必要はありません。暗号文は現代の暗号のテキストではなく、バイナリです。ソケットはバイナリのInputStreamOutputStreamを提供するので、暗号文を文字列に変換する理由もありません。可能な入力文字列をバイナリに変換するだけです(クライアントとサーバーの両方で同じエンコーディングを使用します - もちろんUTF-8が現在好まれています)。

+0

変更されたコードでは、TCPで文字列を送信する代わりにバイトを書きます。どのように私はシステム間で鍵を共有することができますか? –

+0

この場合、鍵を含むバイナリファイルを共有したいと思うかもしれません。 KeyFactory ...しかしそれは別の問題です。TLSとPKIスキームを使用して安全にするには、 –

関連する問題