【发布时间】:2019-04-25 21:25:08
【问题描述】:
我已经设置了一个服务器,它通过下面提到的 java 函数监听加密的字节数组。早些时候我使用java(android)来构建我的应用程序,因此使用相同的java函数很容易,但我无法弄清楚该函数的dart等效(颤振)是什么,它将字符串作为输入并返回AES加密字节数组作为输出,我可以将其写入 tcp 套接字。
我也非常感谢帮助我了解如何将生成的字节数组写入服务器,然后读取类似的响应并通过 dart (flutter) 解密它
我已经成功地编写了简单的字符串并通过 dart 接收简单的字符串作为输入和输出到 tcp 服务器,但不能对加密的字节数组做同样的事情。在 java 中,我使用 DataOutputStream 像这样将响应发送到服务器
DataOutputStream dOut = newDataOutputStream(socket.getOutputStream());
byte[] s2 = Encrypt3.encrypt2(myString);
dOut.writeInt(s2.length); // write length of the message
dOut.write(s2);
这是我用于aes加密的java函数
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Encrypt3 {
public static String key = "mykey";
public static byte[] encrypt2(String text ){
String encrypt ="";
try{
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
return encrypted ;
}catch(Exception e){
System.out.println(e);
}
return null ;
}
public static String decrypt2(byte[] encrypted2){
String decrypt ="";
try{
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
decrypt = new String(cipher.doFinal(encrypted2));
}catch(Exception e){
System.out.println(e);
}
return decrypt ;
}
}
当输入字符串作为输入时,我希望在 dart 中生成等效的字节数组,然后将字节数组写入 tcp 服务器。
提前谢谢你
【问题讨论】:
-
我正在使用相同的加密和解密功能。所以,请分享您在颤振中创建的用于加密解密的类和用于转换数据的 api 函数
标签: java flutter serversocket tcpsocket