2012-10-27 49 views
2

您好我有一些麻烦我的我的加密聊天系统麻烦我的Java加密

我试图在网络上通过加密安全地得到一个加密密钥...聊天服务器发送加密的密钥和客户端在加密之后将加密信息发送回服务器,然后使用加密的密钥对其进行解密......这是卡住的地方,它似乎无法解密双重加密的字符串....看看在我的代码..

如果你运行这个代码(只运行服务器和客户端在单独的窗口,它将工作),并首先点击服务器窗口上的同步(我还没有得到它的工作)

然后在客户端窗口上同步....阅读命令行,您将看到我的问题!

主要错误我得到的是这种

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

我只是需要与填充修复解密问题?如何我把没有填充到加密/解密(即加密类是在底部)

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

请在此处创建一个小型,自包含的测试用例,其中只是加密问题。你可能会在这个过程中发现你的问题,如果没有,你会在这里得到更多的帮助,如果你发布。 – bmargulies

+0

异常堆栈在哪里?除非您详细描述您的问题,否则您无法期待答案。 –

+0

对不起,我不明白?即时通讯不是很好,尤其是错误的东西大声笑我可以编程,但如果出了什么问题我有点卡住 – TheHidden

回答

1

只是让每个人都知道这个问题被发送STRING您无法打开加密的数据到它发送的字节串...因此,如果任何人使用该代码发送加密数据并将其作为字节数组接收