我正在进行加密聊天,我几乎拥有它,但发送信息时出现问题......不断添加2个字符,我不知道为什么......当我尝试删除两个不存在的字符时给我一个“必须以 0 开头”的错误...
如果有人可以帮助我,那就太好了...目前只有服务器加密数据,客户端解密(节省时间)...请举例说明:)
这是chatServer.java
import java.awt.*;
import java.math.BigInteger;
import java.net.*;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.awt.*;
import java.awt.event.*;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class chatServer extends SwingChatGUI
{
PrintWriter out;
BufferedReader in;
BufferedReader stdin;
String inputLine, outputLine;
public ButtonHandler bHandler = new ButtonHandler();
int stage = 1;
private static BigInteger l;
private static BigInteger m;
PublicKey pubKey;
public chatServer (String title)
{
super (title);
bHandler = new ButtonHandler ();
sendButton.addActionListener (bHandler);
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
outputLine = txArea.getText ();
System.out.println ("Server > " + outputLine);
encrypt();
out.println (outputLine);
}
}
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)
{
System.out.println ("Server < " + inputLine);
rxArea.setText (inputLine);
if (stage == 1)
{
String[] parts = inputLine.split("!e! ", 2);
String string1 = parts[0];
String string2 = parts[1];
String j = string1.replace("!m! ", "");
m = new BigInteger(j);
l = new BigInteger(string2);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, l);
KeyFactory fact;
try {
fact = KeyFactory.getInstance("RSA");
pubKey = fact.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stage++;
}
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
public void encrypt() {
byte[] src = outputLine.getBytes();
Cipher cipher;
try {
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherData = cipher.doFinal(src);
outputLine = new String(cipherData);
int count = outputLine.length();
System.out.println("bytes: " + count);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) //throws IOException
{
chatServer f = new chatServer ("Chat Server Program");
f.pack ();
f.show ();
try
{
f.run ();
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: to host.");
System.exit(1);
}
}
}
这是chatClient.java
import java.awt.*;
import java.awt.event.*;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
public class chatClient extends SwingChatGUI
{
/**
*
*/
private static final long serialVersionUID = 1L;
static Socket socket = null;
static PrintWriter out = null;
static BufferedReader in = null;
public ButtonHandler bHandler;
static PublicKey publicKey;
static PrivateKey privateKey;
String fromServer;
int stage = 0;
public chatClient (String title)
{
super (title);
bHandler = new ButtonHandler();
sendButton.addActionListener( bHandler );
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String outputLine;
outputLine = txArea.getText ();
System.out.println ("Client > " + outputLine);
out.println (outputLine);
}
}
public void decrypt() {
Cipher cipher1;
try {
//fromServer = fromServer.substring(0, fromServer.length() - 2);
int count = fromServer.length();
System.out.println("the length is " +count);
byte[] src = fromServer.getBytes();
cipher1 = Cipher.getInstance("RSA");
cipher1.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cipherData1 = cipher1.doFinal(src);
System.out.println("encrypted?" + new String(cipherData1));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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: to host.");
System.exit(1);
}
catch (IOException e)
{
System.err.println ("Couldn't get I/O for the connection to: to host.");
System.exit (1);
}
kp();
CharArrayWriter os = new CharArrayWriter();
char[] buf = new char[4096];
int read;
while ((read=in.read(buf, 0, 4096)) > 0) {
System.out.println("x");
os.write(buf, 0, read);
fromServer = os.toString();
if (stage > 2)
{
decrypt();
}
stage++;
System.out.println ("Client < " + fromServer);
rxArea.setText (fromServer);
os.reset();
if (fromServer.equals ("Bye.")) break;
}
out.close();
in.close();
socket.close();
}
public static void main(String[] args)
{
chatClient f = new chatClient ("Chat Client Program");
f.pack ();
f.show ();
try
{
f.run ();
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: to host.");
System.exit(1);
}
}
public static void kp() {
KeyPairGenerator kpg;
try {
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
publicKey = kp.getPublic();
privateKey = kp.getPrivate();
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub;
pub = fact.getKeySpec(kp.getPublic(),
RSAPublicKeySpec.class);
out.println("!m! " + pub.getModulus() + "!e! " + pub.getPublicExponent());
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是 GUI ... SwingChatGUI.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
@SuppressWarnings("unused")
public class SwingChatGUI extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public JButton sendButton;
public static JTextArea txArea;
public static JTextArea 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");
container.add (rxArea);
container.add (txArea);
container.add (sendButton);
}
public static void main (String[] args)
{
Frame f = new SwingChatGUI ("Chat Program");
f.pack ();
f.setVisible(true);
}
}