- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我已经设置了一个服务器,它通过下面提到的 java 函数监听加密的字节数组。早些时候我使用java(android)来构建我的应用程序,因此使用相同的java函数很容易,但我无法弄清楚该函数的dart等效( flutter )是什么,它将字符串作为输入并返回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 服务器。
提前谢谢你
最佳答案
您配置 Java 密码的方式默认为电子密码本 (ECB) 模式,不应该在真正的密码系统中使用。您还依赖于默认填充 - PKCS5。
使用 pointycastle
包,导入以下内容:
import 'package:pointycastle/api.dart';
import 'package:pointycastle/block/aes_fast.dart';
import 'package:pointycastle/paddings/pkcs7.dart';
import 'package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart';
import 'package:pointycastle/block/modes/ecb.dart';
Dart 中的近似等价物是:
List<int> encrypt2(String text) {
// key must be 16 or 32 bytes - not sure how "mykey" could work
// key should really be binary, not a String! Better to use a KDF.
Uint8List key = Uint8List.fromList(
utf8.encode('0123456789abcdef'),
);
PaddedBlockCipher cipher = PaddedBlockCipherImpl(
PKCS7Padding(), // Java defaults to PKCS5 which is equivalent
ECBBlockCipher(AESFastEngine()), // Very weak mode - don't use this in the real world
);
cipher.init(
true,
PaddedBlockCipherParameters<CipherParameters, CipherParameters>(
KeyParameter(key),
null,
),
);
return cipher.process(utf8.encode(text)); // this isn't the same as .toBytes, except for ASCII
}
关于java - java中是否有以下Aes加密函数的Dart等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55857845/
我有一个相当冗长的命令行程序,它需要用户输入参数,然后使用这些参数进行处理。我想做的是将程序拆分为交互式和非交互式。我试图这样做,并打算让非交互式程序“调用”交互式程序并使用结果(参数),基于这些参数
谁能解释为什么我们在构造函数的参数中使用大括号。 class Cars { String carName; bool isAuto; // create the constructor
我想知道是否有Dart函数,例如PHP的 strrev()。如果没有,您能否请向我展示任何源代码如何自行实现? 谢谢。 最佳答案 我还没有在API中找到一个新的Dart用户(截至今天下午)。但是,以任
我有一个组件,我想根据一个 bool 值绑定(bind)一个不同的 css 类。我的组件代码中有以下内容: bindCssClass(div, "open", this, "task.isOpen")
我一直在研究如何在dart中播放.wav文件,并且已经能够通过dart:html库中定义的AudioElement成功完成此操作。但是,我对使用dart:web_audio库感兴趣,并且遇到了所有这些
Dart 是否旨在实现许多与 Haxe 相同的功能,以便能够针对其他语言和运行时?它是语言和工具的既定目标还是已知目标? 最佳答案 不。Dart 专门针对 web 应用程序开发(更具体地说,客户端 w
我正在尝试让 dart 正常工作。作为编辑器,我想使用 emacs dart-mode。我有两个关于在没有 dart 编辑器的情况下使用 dart 的问题: 1) 我是否需要手动调用 dart2js,
我正在使用列表来创建墨水瓶按钮。我想将列表放在一个单独的 dart 文件中,并将该文件导入到我使用列表的文件中。我不知道如何导入列表。 https://pastebin.com/mf0kvsGu 我制
我正在编写一个 flutter 应用程序,它通过蓝牙 (FlutterBlue) 向设备发送命令。该设备控制一些 LED。通信总体上运行良好,但是:在用户界面上,我有一个控制光强度的 slider 。
我正在使用来自 Get started: command-line and server apps | Dart 的说明.运行 dart --version 时,我得到: Dart VM versio
var c 返回 3 但 10/7=1.4285,其余为 0.4285,operator % 有错误? void main() { var a = 10; var b = 7; var c
如文档中所述: The const keyword isn’t just for declaring constant variables. You can also use it to create
在 Dart 中,我如何最好地编写相当于(不可变/值/非对象) 的代码输出或引用参数 ? 例如在 C#-ish 中,我可能会编码: function void example() { int re
Dart 支持的多重继承机制有哪些? 最佳答案 不,Dart 不支持多重实现继承。 Dart 具有接口(interface),并且与大多数其他类似语言一样,它具有多个接口(interface)继承。
我正在寻找有关如何制作可以采用位置子参数的 web 组件的资源。就像是: {{value}} // this could be any uneditable element {{value}
使用polymer.dart 的Dart 应用程序的pubspec.yaml 文件如下所示(来自Polymer.dart Code Lab): name: polymer_and_dart descr
今天我决定开始学习 Dart 语言,我从 Tour 开始,那里有一个例子: // These work in a const string. const aConstNum = 0; const aC
我正在查找字符串类和其他一些资源,试图了解如何格式化字符串。首先,我试图将数字填充到字符串中,但不是精度。 例子: int a = 0, b = 5, c = 15, d = 46; String
我知道的大部分是Javascript。我相信“列表”是 Dart 最接近数组的东西,但它们在技术上是一样的吗?将它们视为数组是错误的吗?同样, map 会被视为对象吗?我意识到可能存在差异,但进行这种
现在,白色背景刺痛了我的眼睛,因为这是一个looong的黑夜。是否可以从 Dart 编辑器 (v 0.1) 更改主题(更改为更暗的主题)?选择背景颜色和语法高亮怎么样? 最佳答案 是的,这是可能的,我
我是一名优秀的程序员,十分优秀!