嗨,我的 android 应用程序有问题。我编写了使用 tcp 和 udp 的客户端。数据由 AES 加密。但是当我尝试接收数据报时,我的应用程序返回异常。这是我的代码:
protected Void doInBackground(Void... params)
{
try
{
udp = new DatagramSocket(2500);
boolean flagaBYE = true;
String w = "";
while(flagaBYE)
{
byte[] receiveData= new byte[1024];
DatagramPacket pakiet = new DatagramPacket(receiveData, receiveData.length);
udp.receive(pakiet);
String wiadomosc = new String(pakiet.getData(),"utf-8");
publishProgress(wiadomosc);
String szyfr = main.preferences.getString("SZYFR_TCP", "");
if(!szyfr.equals("1"))
{
Encryptor enc = new Encryptor(getKeyBytes(key), getCode());
wiadomosc = enc.decrypt(wiadomosc);
}
我的例外是垫块损坏我生成这样的密钥:
String key = "tojestkluczwlasnie";
然后将字符串解析为 byte[]
public static byte[] getKeyBytes(String key) throws UnsupportedEncodingException{
byte[] keyBytes= new byte[16];
byte[] parameterKeyBytes= key.getBytes("UTF-8");
System.arraycopy(parameterKeyBytes, 0, keyBytes, 0, Math.min(parameterKeyBytes.length, keyBytes.length));
return keyBytes;
}