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)));
}
}
請在此處創建一個小型,自包含的測試用例,其中只是加密問題。你可能會在這個過程中發現你的問題,如果沒有,你會在這裏得到更多的幫助,如果你發佈。 – bmargulies
異常堆棧在哪裏?除非您詳細描述您的問題,否則您無法期待答案。 –
對不起,我不明白?即時通訊不是很好,尤其是錯誤的東西大聲笑我可以編程,但如果出了什麼問題我有點卡住 – TheHidden