gpt4 book ai didi

java - 将消息发送到 IP 地址后进行 AES 解密

转载 作者:行者123 更新时间:2023-12-01 11:29:28 25 4
gpt4 key购买 nike

所以我正在制作一个应用程序,将安全消息发送到指定的 IP 地址。我使用 AES 来加密消息,这部分效果很好。我可以对消息进行加密,也可以在发送消息之前对其进行解密。但是,当我尝试解密从服务器收到的消息时,我无法解密它。它以加密形式显示。

我收到此错误“java.lang.NumberFormatException:无效的int:“ne””,我认为这可能与字符编码或其他内容有关?通过网络发送字符串时是否会以任何方式更改?

以下是可能与该问题相关的片段。

public static String encrypt(String seed, String cleartext)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}

public static String decrypt(String seed, String encrypted)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}


public static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
16).byteValue();
return result;
}

public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}

这是我对要显示的消息进行解密的地方。

while (!goOut) {
if (dataInputStream.available() > 0) {
incoming = dataInputStream.readUTF();

if(encrypt == true) {
try{
msgLog += AESHelper.decrypt(seed,incoming);
} catch (Exception e) {
e.printStackTrace();
}

} else msgLog += incoming;



MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
chatMsg.setText(msgLog);
}
});

这正在加密消息:

OnClickListener buttonEncryptOnClickListener = new OnClickListener()   {
public void onClick(View v) {
if (chatClientThread == null) {
return;
}
if (editTextSay.getText().toString().equals("")) {
return;
}

if(!editTextSay.getText().toString().equals("")){

String message = editTextSay.getText().toString();
encrypt = true;
int secLvl = Integer.parseInt(editTextSecurity.getText().toString());

String encryptedMsg;
try {
encryptedMsg = AESHelper.encrypt(seed, message);
textEncryptedmsg.setText(encryptedMsg);
textEncryptedmsg.setVisibility(View.VISIBLE);

} catch (Exception e) {
e.printStackTrace();

}

}

}
};

正在发送消息:

OnClickListener buttonSendOnClickListener = new OnClickListener() {

@Override
public void onClick(View v) {
if (editTextSay.getText().toString().equals("")) {
return;
}

if(chatClientThread==null){
return;
}

if (encrypt == true){

chatClientThread.sendMsg(textEncryptedmsg.getText().toString() + "\n");
} else {
chatClientThread.sendMsg(editTextSay.getText().toString() + "\n");
}

editTextSay.setText("");
textEncryptedmsg.setText("");
textDecryptedmsg.setText("");
encrypt = false;
incomingmsg.setVisibility(View.VISIBLE);
}

};

最佳答案

我假设您以十六进制编码的形式发送密文。 DataInputStream#readUTF() 读取 single Unicode character from the stream 。由于您要发送十六进制字符,这意味着可以从两个这样的 Unicode 字符构造单个密文字节。

问题是 AES 在 block 上运行。尝试单独解密每个“半”字节是行不通的。

您需要将解密方法重写为

  • 使用流式解密或
  • 一次性读取整个密文并解密。

如果您想尝试流式解密,那么您基本上有两个选择:

<小时/>

这是一个在尝试解密之前阅读整个内容的(伪代码)示例:

StringBuilder incoming = new StringBuilder();
while (!goOut && ) {
incoming.append(dataInputStream.readUTF());
}
if(encrypt == true) {
try{
msgLog += AESHelper.decrypt(seed, incoming.toString());
} catch (Exception e) {
e.printStackTrace();
}
} else msgLog += incoming;

关于java - 将消息发送到 IP 地址后进行 AES 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com