简体   繁体   English

我无法解密发送消息AES

[英]I can't decrypt send message AES

I have problem with decrypting message from UDP receive. 我有解密来自UDP接收的消息的问题。

My code: 我的代码:

byte[] receiveData= new byte[1024];

DatagramPacket pakiet = new DatagramPacket(receiveData, receiveData.length);
udp.receive(pakiet);

String wiadomosc = new String(pakiet.getData());
publishProgress(wiadomosc);
String szyfr = main.preferences.getString("SZYFR_TCP", "");
if(!szyfr.equals("1"))
{
    enc = new Encryptor(kluczByte, getCode());
    Log.e("myApp", "jestem 2");
    Log.e("message = ","" + wiadomosc);

    wiadomosc = enc.decrypt(wiadomosc);
    publishProgress(wiadomosc);
}

In the log I saw that variable wiadomosc has data and many bad characters like this: 在日志中我看到变量wiadomosc有数据和许多坏字符,如下所示:

The byte array reciveData has 1024 elements. 字节数组reciveData有1024个元素。 I think that my receive message is shorter, so it have to add elements to 1024. How to remove these characters from the string? 我认为我的接收消息较短,因此必须将元素添加到1024.如何从字符串中删除这些字符? Because I can't decrypt this string because I get a "corrupted pad block" exception. 因为我无法解密此字符串,因为我得到了“损坏的填充块”异常。

I think that you just get the buffer returned when you call pakiet.getData() . 我认为你只是在调用pakiet.getData()时返回缓冲区。 So you could just call pakiet.getLength() to get to the exact amount of data within the buffer. 所以你可以调用pakiet.getLength()来获得缓冲区中的确切数据量。 If you call the decrypt function with the correct offset and length you should have more success. 如果使用正确的偏移量和长度调用decrypt函数,则应该取得更大的成功。

UDP header contains the length of the UDP data. UDP报头包含UDP数据的长度。 So there must be some API in java to extract that length. 因此,java中必须有一些API来提取该长度。 The length is in bytes. 长度以字节为单位。 So initialize you receivedData wih that length. 所以初始化你receivedData这个长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM