在Java中生成DES密钥并通过套接字传递
有人能帮我在我的简易程序中解决这个问题吗 在服务器上输出消息时,我希望看到通过网络发送的相同消息,但我没有收到 以下是我的客户代码:在Java中生成DES密钥并通过套接字传递,java,sockets,security,encryption,stream,Java,Sockets,Security,Encryption,Stream,有人能帮我在我的简易程序中解决这个问题吗 在服务器上输出消息时,我希望看到通过网络发送的相同消息,但我没有收到 以下是我的客户代码: package encryption; import java.io.*; import java.net.*; import java.security.*; import java.util.*; import javax.crypto.*; public class CipherClient { public static void main(S
package encryption;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.*;
import javax.crypto.*;
public class CipherClient
{
public static void main(String[] args) throws Exception
{
String message = "The quick brown fox jumps over the lazy dog.";
String host = "localhost";
int port = 7999;
Socket s = new Socket(host, port);
// -Generate a DES key.
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());
Key key = generator.generateKey();
// -Store it in a file.
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("KeyFile.xx"));
out.writeObject(key);
out.close();
// -Use the key to encrypt the message above and send it over socket s to the server.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cipherOut = new CipherOutputStream(s.getOutputStream(), cipher);
System.out.println(message.getBytes().length);
cipherOut.write(message.getBytes());
}
}
public class CipherClient
{
public static void main(String[] args) throws Exception
{
// -Generate a DES key.
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());
Key key = generator.generateKey();
// -Store it in a file.
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("KeyFile.xx"));
out.writeObject(key);
out.close();
String message = "The quick brown fox jumps over the lazy dog.";
System.out.println("Message converted from Bytes = " + new String(message.getBytes()));
System.out.println("Length = " + message.getBytes().length);
String host = "localhost";
int port = 7999;
Socket s = new Socket(host, port);
// -Use the key to encrypt the message above and send it over socket s to the server.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cipherOut = new CipherOutputStream(s.getOutputStream(), cipher);
cipherOut.write(message.getBytes());
cipherOut.close();
s.close();
}
}
这是我的服务器代码:
package encryption;
import java.io.*;
import java.net.*;
import java.security.*;
import javax.crypto.*;
public class CipherServer
{
public static void main(String[] args) throws Exception
{
int port = 7999;
ServerSocket server = new ServerSocket(port);
Socket s = server.accept();
// -Read the key from the file generated by the client.
ObjectInputStream in = new ObjectInputStream(new FileInputStream("KeyFile.xx"));
Key key = (Key)in.readObject();
System.out.println(key.getClass().getName());
in.close();
// -Use the key to decrypt the incoming message from socket s.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
CipherInputStream cipherIn = new CipherInputStream(s.getInputStream(), cipher);
byte[] stringInBytes = new byte[44];
cipherIn.read(stringInBytes);
String string = new String(stringInBytes);
// -Print out the decrypt String to see if it matches the orignal message.
System.out.println(string);
}
}
服务器端的控制台输出:
javax.crypto.spec.SecretKeySpec
}#ùÂ?°ô0íÿ| r|XÌ\?ñwŽ³{Í@nŠ?
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.readInt(Unknown Source)
at encryption.CipherServer.main(CipherServer.java:26)
客户端上的控制台输出:
44
以下是我为客户机编写的新代码:
package encryption;
import java.io.*;
import java.net.*;
import java.security.*;
import javax.crypto.*;
public class CipherClient
{
public static void main(String[] args) throws Exception
{
// -Generate a DES key.
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());
Key key = generator.generateKey();
// -Store it in a file.
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("KeyFile.xx"));
out.writeObject(key);
out.close();
// -Connect to a server.
String message = "The quick brown fox jumps over the lazy dog.";
String host = "localhost";
int port = 7999;
Socket s = new Socket(host, port);
// -Use the key to encrypt the message above and send it over socket s to the server.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = cipher.doFinal(message.getBytes());
DataOutputStream dOut = new DataOutputStream(s.getOutputStream());
dOut.writeInt(encVal.length); // write length of the message
dOut.write(encVal); // write the message
}
}
以下是我的服务器新代码:
package encryption;
import java.io.*;
import java.net.*;
import java.security.*;
import javax.crypto.*;
public class CipherServer
{
public static void main(String[] args) throws Exception
{
int port = 7999;
ServerSocket server = new ServerSocket(port);
Socket s = server.accept();
// -Read the key from the file generated by the client.
ObjectInputStream in = new ObjectInputStream(new FileInputStream("KeyFile.xx"));
Key key = (Key)in.readObject();
in.close();
// -Use the key to decrypt the incoming message from socket s.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
DataInputStream dIn = new DataInputStream(s.getInputStream());
int length = dIn.readInt(); // read length of incoming message
if(length>0)
{
byte[] messageInBytes = new byte[length];
dIn.readFully(messageInBytes, 0, messageInBytes.length); // read the message
// -Print out the decrypt String to see if it matches the orignal message.
System.out.println(new String(cipher.doFinal(messageInBytes)));
}
}
}
以下是我在服务器端遇到的错误:
javax.crypto.spec.SecretKeySpec
}#ùÂ?°ô0íÿ| r|XÌ\?ñwŽ³{Í@nŠ?
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.readInt(Unknown Source)
at encryption.CipherServer.main(CipherServer.java:26)
客户端没有错误。我认为客户端和服务器之间存在着从密钥文件读写的竞争。至少当我运行你的代码时它对我是这样的 在创建套接字之前,尝试将密钥生成和写入密钥文件。这样,当客户机创建套接字时,服务器接受它,当它转到文件时,它确实会得到一个有效的文件 它适用于我,除了写入文件位之外,与您的代码几乎没有区别 客户:
package encryption;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.*;
import javax.crypto.*;
public class CipherClient
{
public static void main(String[] args) throws Exception
{
String message = "The quick brown fox jumps over the lazy dog.";
String host = "localhost";
int port = 7999;
Socket s = new Socket(host, port);
// -Generate a DES key.
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());
Key key = generator.generateKey();
// -Store it in a file.
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("KeyFile.xx"));
out.writeObject(key);
out.close();
// -Use the key to encrypt the message above and send it over socket s to the server.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cipherOut = new CipherOutputStream(s.getOutputStream(), cipher);
System.out.println(message.getBytes().length);
cipherOut.write(message.getBytes());
}
}
public class CipherClient
{
public static void main(String[] args) throws Exception
{
// -Generate a DES key.
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());
Key key = generator.generateKey();
// -Store it in a file.
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("KeyFile.xx"));
out.writeObject(key);
out.close();
String message = "The quick brown fox jumps over the lazy dog.";
System.out.println("Message converted from Bytes = " + new String(message.getBytes()));
System.out.println("Length = " + message.getBytes().length);
String host = "localhost";
int port = 7999;
Socket s = new Socket(host, port);
// -Use the key to encrypt the message above and send it over socket s to the server.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cipherOut = new CipherOutputStream(s.getOutputStream(), cipher);
cipherOut.write(message.getBytes());
cipherOut.close();
s.close();
}
}
和服务器:
public class CipherServer
{
public static void main(String[] args) throws Exception
{
int port = 7999;
ServerSocket server = new ServerSocket(port);
Socket s = server.accept();
// -Read the key from the file generated by the client.
ObjectInputStream in = new ObjectInputStream(new FileInputStream("KeyFile.xx"));
Key key = (Key)in.readObject();
in.close();
// -Use the key to decrypt the incoming message from socket s.
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
CipherInputStream cipherIn = new CipherInputStream(s.getInputStream(), cipher);
byte[] array = new byte[44];
cipherIn.read(array);
cipherIn.close();
s.close();
String message = new String(array);
System.out.println(message);
}
}
我希望看到通过网络发送的相同消息,但我没有收到。你什么都没看见?没有堆栈跟踪,没有消息?你试过不用钥匙吗?@Nathan,更新了。@Nathan,我还是不知道问题出在哪里。帮助我,请。让我试试,非常感谢您的关注。仍然得到相同的输出吗?以下是我在服务器端得到的结果:javax.crypto.spec.SecretKeySpec异常位于线程主java.net.SocketException:Connection reset at java.net.SocketInputStream.readUnknown Source at java.net.SocketInputStream.readUnknown Source atjava.net.SocketInputStream.readUnknown Source at javax.crypto.CipherInputStream.getMoreDataCipherInputStream.java:114 at javax.crypto.CipherInputStream.readCipherInputStream.java:239 at javax.crypto.CipherInputStream.readCipherInputStream.java:215 at encryption.CipherServer.mainCipherServer.java:27是否先运行服务器?查看问题我稍微调整了一下。