2

嗨,我的加密聊天系统有问题

我正在尝试通过加密通过网络安全地获取加密密钥......聊天服务器发送加密密钥,客户端在加密后将加密发送回服务器,然后使用它加密的密钥对其进行解密。 ..这是卡住的地方,它似乎无法解密双重加密的字符串..看看我的代码..

如果您运行此代码(只需在单独的窗口中运行服务器和客户端,它将起作用)并首先单击服务器窗口上的 SYNC(我还没有让它双向工作)

然后在客户端窗口同步....阅读命令行你会看到我的问题!

我得到的主要错误是这个

鉴于最终块没有正确填充任何帮助?

我只需要用填充解决解密问题吗?如何在加密/解密中不添加填充(加密类位于底部)

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

只是让每个人都知道问题是发送字符串,您不能将加密数据转换为字符串,它必须作为字节发送......所以如果有人使用代码发送加密数据并将其作为字节数组接收

于 2013-03-21T18:14:21.717 回答