我有一个关于客户端服务器输出无法正常工作的小错误。 问题是从解密后,从我的服务器发送到客户端的输出并不总是相同。 在这种情况下,得到的解密是相同的,只有一定的长度。
下面是输出,后面是源代码:
Server
Starting the socket server at port:9876
Waiting for clients...
GxModP = 2819481724922694722202990425920759863750647735434635820227489190230872167846391528548356160689879969739097933352014135386311977602583588682340466075235457
encrypted = Y:!T¤]®ð]áfyLkåª hÛÒ2{ø2/Z°ÿFär8 icÂÂñýÜå¼Níxv_Ö3?»>@×âÑÛm4ÁônûZùGÃêa¼/d
×Q¨_¬ù6øY¾½¡pOY÷?x¼ù>öMÀ|c´áúê±ôpZþÏÈy9½4S
decrypted = 2819481724922694722202990425920759863750647735434635820227489190230872167846391528548356160689879969739097933352014135386311977602583588682340466075235457
Client
Attempting to connect to localhost:9876
Connection Established
User input : Y:!T¤]®ð]áfyLkåª hÛÒ2{ø2/Z°ÿFär8 icÂÂñýÜå¼Níxv_Ö3?»>@×âÑÛm4ÁônûZùGÃêa¼/d
Gx mod P : 281948172492269472220299042592075986375064773543463582022748919023087216784639152854835616068987
我在这里做的是将加密放入客户端输入并解密加密的消息以便到达GxModP。但是,似乎GxModP的值只有一定长度才相同。 谁能告诉我,我犯了什么错误导致这个?
我在下面附上了源代码以供参考
private static String algorithm = "RC4";
public static byte[] encryptRC4(String toEncrypt, String key) throws Exception {
// create a binary key from the argument key (seed)
SecureRandom sr = new SecureRandom(key.getBytes("ISO-8859-1"));
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init(sr);
SecretKey sk = kg.generateKey();
// create an instance of cipher
Cipher cipher = Cipher.getInstance(algorithm);
// initialize the
cipher with the key
cipher.init(Cipher.ENCRYPT_MODE, sk);
// enctypt!
byte[] encrypted = cipher.doFinal(toEncrypt.getBytes("ISO-8859-1"));
return encrypted;
}
public static String decryptRC4(byte[] toDecrypt, String key, int length) throws Exception {
// create a binary key from the argument key (seed)
SecureRandom sr = new SecureRandom(key.getBytes("ISO-8859-1"));
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
kg.init(sr);
SecretKey sk = kg.generateKey();
// do the decryption with that key
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, sk);
byte[] decrypted = cipher.doFinal(toDecrypt, 0, length);
return new String(decrypted, "ISO-8859-1");
}
服务器代码:
private void sendKey(Socket client) throws IOException {
//calculate GxModP
//encrypt + send over.
String gXmodP = generateGxModP(g, p);
String temp = "";
System.out.println("GxModP = "+gXmodP);
try
{
encryptedKey = EncryptionScheme.encryptRC4(gXmodP, password);
}catch(Exception e){}
String encrypted = new String(encryptedKey, "ISO-8859-1");
byte[] temp2 = encrypted.getBytes("ISO-8859-1");
System.out.println("encrypted = "+encrypted);
try
{
temp = EncryptionScheme.decryptRC4(temp2, password, temp2.length);
}catch(Exception e){};
System.out.println("decrypted = " + temp);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
writer.write(encrypted);
writer.flush();
writer.close();
}
客户代码
public void readResponse() throws IOException
{
String userInput;
BufferedReader stdIn = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
String gXmodP = "";
/*
System.out.println("Response from server:");
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);*/
//instead of reading response i will decrypt and print out
userInput = stdIn.readLine();
System.out.println("User input : " + userInput);
byte [] toDecrypt = userInput.getBytes("ISO-8859-1");
try
{
gXmodP = EncryptionScheme.decryptRC4(toDecrypt, password, toDecrypt.length);
}catch(Exception e){};
System.out.println("Gx mod P : " + gXmodP);
}
答案 0 :(得分:0)
您处理加密输出的二进制结果的方式容易出错。您不应该直接将byte []转换为String并将其作为字符传输(使用类似Base64的编码或完全粘贴到二进制传输)。
在您的具体示例中,问题来自加密结果包含二进制中的换行符/ \ n。因此,客户端上的readLine();
会在那里剪切二进制字符串,因此您的解密文本也会被删除。否则,您的加密代码运行得非常好。
但我不会评论使用RC-4。