私の非常に簡単なプログラムで問題を理解するのに助けになる人がいますか?DES鍵を生成してJavaのソケットを通過させる
サーバーでメッセージを出力するとき、ネットワーク経由で送信されたメッセージと同じメッセージが表示されますが、メッセージは表示されません。ここで
は、クライアントのために私のコードです:
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());
}
}
そして、ここでは、サーバのための私のコードです:サーバー側の
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Š?
コンソールクライアント側の出力:
ここで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)));
}
}
}
は、私は、サーバー側で取得していますエラーです:
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)
クライアント側にエラーはありません。
'私は、ネットワークを介して送信された同じメッセージを見たいのですが、私はあなたのでone.'得ていないのです何も見ない?スタックトレースなし、メッセージなし?あなたはあなたの鍵を使わずに試しましたか? – Nathan
@Nathan、更新。 – ohidano
@Nathan、私はまだ問題を理解することはできません。お願い助けて。 – ohidano