2

こんにちは、暗号化されたチャット システムに問題があります。

私はそれを暗号化することによってネットワーク経由で暗号化キーを安全に取得しようとしています...チャットサーバーは暗号化されたキーを送信し、クライアントは暗号化された後に暗号化を返信しますサーバーは暗号化したキーで暗号化を解除します。 ..これは、二重に暗号化された文字列を復号化するのに問題があるようです..私のコードを見てください..

このコードを実行すると(サーバーとクライアントを別々のウィンドウで実行するだけで機能します)、最初にサーバーウィンドウの同期をクリックします(まだ両方の方法で機能させる必要があります)

次に、クライアントウィンドウで同期します....コマンドラインを読むと、私の問題が表示されます!

私が得る主なエラーはこれです

最終ブロックが適切にパディングされていない場合、助けはありますか??

パディングで復号化の問題を修正する必要があるだけですか? 暗号化/復号化にパディングを入れない方法 (その暗号化クラスは一番下にあります)

SwingChatServer.java

   import java.awt.*;
import java.net.*;              
import java.awt.event.*;   
import javax.swing.*;     
import javax.swing.event.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.spec.*;
import java.util.UUID;
import chat.*;
public class SwingChatServer extends SwingChatGUI
{
PrintWriter out;
BufferedReader in;
BufferedReader stdin;
String inputLine, outputLine, collect;
public ButtonHandler bHandler = new ButtonHandler();
public ButtonHandler bH = new ButtonHandler();
public String rgk;
public String stk;
public String lls;
public int sen;   
public SwingChatServer (String title)
{
  super (title);
  bHandler = new ButtonHandler ();
  sendButton.addActionListener (bHandler);
  synco.addActionListener (bH);
  keymaker();
}
private class ButtonHandler implements ActionListener
{
  public void actionPerformed (ActionEvent event)
  {

  if(event.getSource()==sendButton)
  {
   outputLine = txArea.getText ();
   System.out.println ("Server > " + outputLine);
try {

    DesEncrypter encrypter = new DesEncrypter(rgk);
    String encrypted = encrypter.encrypt(outputLine);
System.out.println("" + encrypted);
out.println (encrypted);

} catch (Exception e) {
   //out.println (outputLine);
}
  }
  if(event.getSource()==synco)
  {
   System.out.println("YOUR PARTNER WISHES TO MAKE THIS A PRIVATE MATTER, CLICK SYNC TO ENCRYPT ALL MESSAGES");
   stk = rgk;
   System.out.println("this is the key which will encrypt the new key ---> " + stk);
   keymaker();
   System.out.println("this is the key which will be encrypted ---> " + rgk);
  try {
   DesEncrypter encrypter = new DesEncrypter(stk);
           String encrypted = encrypter.encrypt(rgk);
   System.out.println("this is how the key looks encrypted ---> " + encrypted);
   out.println (encrypted);
   out.println("test");
   sen = 1;
  } catch (Exception e) {
   System.out.println("error");
  }

  }

         }
}

public void run () throws IOException
{
  ServerSocket serverSocket = null;

  try
            {
                 serverSocket = new ServerSocket (4444);
            }
            catch (IOException e)
            {
                 System.err.println ("Could not listen on port: 4444.");
                 System.exit (1);
            }
            Socket clientSocket = null;
            try
            {
                 clientSocket = serverSocket.accept ();
            }
            catch (IOException e)
            {
                 System.err.println ("Accept failed.");
                 System.exit(1);
            }
         out = new PrintWriter (clientSocket.getOutputStream (), true);
         in = new BufferedReader (new InputStreamReader (clientSocket.getInputStream ()));
         //stdin = new BufferedReader (new InputStreamReader (System.in));
            out.println ("Welcome to the Chat Server\n");
            while ((inputLine = in.readLine ()) != null)
            {

  lls = inputLine;
  if (sen == 1)
  {
  System.out.println("this should be the encrypted, encryption ---> " + lls);

  try {
   System.out.println("test..1..." + inputLine + " and now for stk " + stk);  
   DesEncrypter encrypters = new DesEncrypter(stk);
   System.out.println("peeka BOO");
   String decrypteds = encrypters.decrypt(inputLine);
   System.out.println(decrypteds);
   //sen = 0;
   //stk = null;
   } catch (Exception e) {
    System.out.println(e.getMessage()); 
   }
  } else {

  System.out.println ("Server < " + inputLine);
try {
   DesEncrypter encrypter = new DesEncrypter(rgk);

   String decrypted = encrypter.decrypt(inputLine);
   System.out.println("" + decrypted);
   rxArea.setText (decrypted);
   } catch (Exception e) {
   }
  collect = (collect +" \n"+ inputLine);
                 rxArea.setText (collect);

            }
}
            out.close();
            in.close();
            clientSocket.close();
            serverSocket.close();
    }
    public static void main(String[] args) //throws IOException
    {

  SwingChatServer f = new SwingChatServer ("Chat Server Program");

  f.pack ();
  f.setVisible(true);
  try
  {
   f.run ();
  }
  catch (IOException e)
  {
   System.err.println("Couldn't get I/O for the connection.");
   System.exit(1);
  }
    }
public void keymaker()
{

String uuid = UUID.randomUUID().toString();
rgk = uuid;
}
}

SwingChatClient.java

import java.awt.*;
import java.net.*;              
import java.awt.event.*;   
import javax.swing.*;     
import javax.swing.event.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.spec.*;
import java.util.UUID;
import chat.*;
public class SwingChatClient extends SwingChatGUI
{
         static Socket socket = null;
         static PrintWriter out = null;
         static BufferedReader in = null;
         public ButtonHandler bHandler, bH;
public String rgk;
public String stk;
public String lls;
public int sen;

public SwingChatClient (String title)
{
  super (title);
  bHandler = new ButtonHandler();
  bH = new ButtonHandler();
  sendButton.addActionListener( bHandler );
  synco.addActionListener( bH );
  keymaker();
}
private class ButtonHandler implements ActionListener
{
  public void actionPerformed (ActionEvent event)
  {
  if (event.getSource()==sendButton)
  {
            String outputLine;
          outputLine = txArea.getText ();
          System.out.println ("Client > " + outputLine);
   out.println (outputLine);
  }
  if (event.getSource()==synco)
  {
   System.out.println("YOUR PARTNER WISHES TO MAKE THIS A PRIVATE MATTER, CLICK SYNC TO ENCRYPT ALL MESSAGES");
   stk = rgk;
   System.out.println("this is the key which will encrypt the new key " + stk);


  try {
   DesEncrypter encrypter = new DesEncrypter(stk);
           String encrypted = encrypter.encrypt(lls);
   System.out.println("" + encrypted);
   out.println (encrypted);
   sen = 1;
  } catch (Exception e) {
   System.out.println("error");
  }
  }
  }
}
public void run () throws IOException
{
  try
  {
   socket = new Socket ("localhost", 4444);
   out = new PrintWriter (socket.getOutputStream (), true);
   in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
  }
  catch (UnknownHostException e)
  {
   System.err.println ("Don't know about host: .");
   System.exit(1);
  }
  catch (IOException e)
  {
   System.err.println ("Couldn't get I/O for the connection to: .");
   System.exit (1);
  }
  String fromServer;
  while ((fromServer = in.readLine ()) != null)
  {
   System.out.println ("this should be encrypted " + fromServer);
   lls = fromServer;

   if (sen == 1)
   {

   try {
    DesEncrypter encrypter = new DesEncrypter(stk);
    String decrypted = encrypter.decrypt(fromServer);
    rgk = decrypted;
    sen = 0;
    stk = null;
    System.out.println("hello???");
   } catch (Exception e) {
   }

   } else {
   try {
   DesEncrypter encrypter = new DesEncrypter(rgk);

   String decrypted = encrypter.decrypt(fromServer);
   System.out.println("hmm..." + decrypted);
   rxArea.setText (decrypted);
   } catch (Exception e) {
   }
   if (fromServer.equals ("Bye.")) break;
  }
}
  out.close();
  in.close();
  socket.close();
}
public static void main(String[] args)
{
   SwingChatClient f = new SwingChatClient ("Chat Client Program");

  f.pack ();
  f.setVisible(true);
  try
  {
   f.run ();
  }
  catch (IOException e)
  {
   System.err.println("Couldn't get I/O for the connection to:");
   System.exit(1);
  }
}
public void keymaker()
{

String uuid = UUID.randomUUID().toString();
rgk = uuid;
}
}

SwingChatGUI.java

import java.awt.*;               
import java.awt.event.*;   
import javax.swing.*;     
import javax.swing.event.*;
public class SwingChatGUI extends JFrame
{
public JButton sendButton, synco;
public JTextArea txArea, rxArea;

  public Container container;

public JPanel n1, s1;


public SwingChatGUI (String title)
{
  super (title);

            container = getContentPane();                       
            container.setLayout( new FlowLayout() );

  txArea = new JTextArea (6, 40);

  rxArea = new JTextArea (6, 40);

  sendButton = new JButton ("Send");
  synco = new JButton ("sync");

  container.add (rxArea);
  container.add (txArea);
  container.add (sendButton);
  container.add (synco);
}

public static void main (String[] args)
{
  Frame f = new SwingChatGUI ("Chat Program");
  f.pack ();
  f.setVisible(true);
}
}

これがパッケージ チャットになりました。*

DesEncrypter.java

package chat;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DesEncrypter {
  Cipher ecipher;
  Cipher dcipher;
  byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35,
          (byte) 0xE3, (byte) 0x03 };
  public DesEncrypter(String passPhrase) throws Exception {
    int iterationCount = 2;
    KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
    SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
    ecipher = Cipher.getInstance(key.getAlgorithm());
    dcipher = Cipher.getInstance(key.getAlgorithm());
    AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
    ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
    dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
  }
  public String encrypt(String str) throws Exception {
    return new BASE64Encoder().encode(ecipher.doFinal(str.getBytes()));
  }
  public String decrypt(String str) throws Exception {
    return new String(dcipher.doFinal(new BASE64Decoder().decodeBuffer(str)));
  }
}
4

1 に答える 1

1

問題がSTRINGを送信していたことを誰もが知っているように、暗号化されたデータを文字列に変換することはできません.

于 2013-03-21T18:14:21.717 に答える