-2
即时通讯工作在Java客户端服务器aes加密服务器加密和客户端解密。但是,我有填充错误可能为字节[]和字符串..我不能解决它..任何人都可以告诉我要改变什么来解决加密和解密问题?当使用填充密码解密时,输入长度的建议必须是16的倍数
SERVER
public void run(){
try {
String key1 = "1234567812345678";
byte[] key2 = key1.getBytes();
SecretKeySpec secret = new SecretKeySpec(key2, "AES");
String msg = "Singapore Malaysia Japan India";
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] encrypted = cipher.doFinal(msg.getBytes());
in = new DataInputStream(incoming.getInputStream());
out = new DataOutputStream(incoming.getOutputStream());
boolean done = false;
String str="";
out.writeUTF("Connected!\n");
out.flush();
while (!done){
out.writeUTF(">");
out.flush();
str = in.readUTF();
System.out.println(in+":"+str);
if (str == null)
done = true;
else{
System.out.println("Sending Ciphertext : " + new String(encrypted));
out.writeUTF(new String(encrypted));
out.flush();
CLIENT
String str = "";
String str2 = "";
DataOutputStream out;
DataInputStream in;
try {
Socket t = new Socket("127.0.0.1", 9003);
in = new DataInputStream(t.getInputStream());
out = new DataOutputStream(t.getOutputStream());
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
boolean more = true;
System.out.println(in.readUTF());
while (more) {
str = in.readUTF();
System.out.print(str);
str2 = br.readLine();
out.writeUTF(str2);
out.flush();
str = in.readUTF();
System.out.println("Encrypted Info: " + str);
try {
String key1 = "1234567812345678";
byte[] key2 = key1.getBytes();
SecretKeySpec secret = new SecretKeySpec(key2, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret);
byte[] decrypted = cipher.doFinal(str.getBytes());
System.out.println("Decrypted Info: " + new String(decrypted));
}
您之前发布过相同的问题,在我要发表评论时已将其删除。你的逻辑看起来是正确的,但是如果新的String(加密的)和客户端的str通过记录它们都是相同的,你可以检查服务器。在通过套接字 – 2014-08-31 10:18:40
写入和读取DataOutputStream时,想要查看是否有任何数据丢失请不要将stackoverflow用作您的私人调试服务。这些问题 - 基本上出了什么问题? - 不会对其他人有任何帮助。首先缩小问题空间;没有客户端/服务器,你会得到相同的错误。 – 2014-08-31 10:43:43