encryption - 如何在颤振中加密用户名和密码
问题描述
我试图在颤振中实现电话号码和密码加密。在尝试加密 jsonbody "( var rBody = jsonEncode({ 'Request': encryptor.encrypt(requestBody.toString())});" 然后运行应用程序后仍然无法将请求传输到我的远程服务器(这要求所有请求都用AES加密)。有这方面经验的人可以告诉我最好的方法吗?这样密码和电话就被有效地加密了。
import 'dart:async';
import 'dart:convert';
import 'package:encrypt/encrypt.dart';
import 'package:http/http.dart' as http;
Future<http.Response> post() async {
var url = 'http:xxxxxxxpostRequest';
String password = "xxxxxxx";//url password
String username = "xxxxx";//access username
var bytes = utf8.encode("$username:$password");
var credentials = base64.encode(bytes);
var headers = {
"Content-Type": "application/json",
"Authorization": "Basic $credentials"
};
var requestBody = jsonEncode({ 'phone': _phone, 'pin': _pass});
final key = "";// encryption key
final iv= "";
final encryptor=new Encrypter(new Salsa20(key, iv));
var rBody = jsonEncode({ 'Request': encryptor.encrypt(requestBody.toString())});
http.Response response = await http.post(
url, body: rBody, headers: headers);
var responseJson = json.decode(response.body);
print(Utf8Codec().decode(response.bodyBytes));
print("Body: " + responseJson);
}
//这是我的控制台响应
E/flutter (24909): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] 未处理的异常:E/flutter (24909): 'ParametersWithIV' 类型不是'ParametersWithIV' 类型的子类型 E/
解决方案
该encrypt
软件包维护得不好,因此请使用该pointy castle
软件包。(使用pointycastle: ^1.0.0-rc3
。)
您的问题不清楚您将如何:
- 从提供的字符串中获取密钥材料
- 将明文转换为字节
- 将密文转换回您可以包含在 json 中的内容
它们可能以十六进制或 base64 编码。您的服务器团队应该能够指定他们想要的内容。
这是在 AES/CBC/PKCS7 中加密的示例代码。
import 'dart:convert';
import 'dart:typed_data';
import 'package:pointycastle/api.dart';
import 'package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart';
import 'package:pointycastle/paddings/pkcs7.dart';
import 'package:pointycastle/block/aes_fast.dart';
import 'package:pointycastle/block/modes/cbc.dart';
main() {
//final key = 'dxxxxxxxxxxeX';
//final iv = '_Vxxxxxxxxxx1';
// TODO - convert the key and IV to bytes
// dummy key and IV values
Uint8List key = Uint8List.fromList(
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
);
Uint8List iv = Uint8List.fromList(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
);
// TODO - convert the plaintext to bytes
// example - just utf8 encode it
Uint8List plainText = Uint8List.fromList(utf8.encode('some plain text'));
PaddedBlockCipher cipher = PaddedBlockCipherImpl(
PKCS7Padding(),
CBCBlockCipher(AESFastEngine()),
);
cipher.init(
true,
PaddedBlockCipherParameters<CipherParameters, CipherParameters>(
ParametersWithIV<KeyParameter>(KeyParameter(key), iv),
null,
),
);
Uint8List cipherText = cipher.process(plainText);
// TODO - convert the cipher text to a String to include as the 'Request' param
}
PS不要忘记多次重复使用相同的IV是不安全的。
推荐阅读
- php - 币安期货 API 给出错误的季度价格
- pytorch - 如何使用 MNIST 数据集在 pytorch 中进行手动批处理
- java - 在 ServerResponse 中将 mono 作为标头值传递
- c# - Xamarin.Forms 在 CollectionView 中实现延迟加载
- html - 如何使 Bootstrap 响应式表更宽
- javascript - 如何在 React 整页包装器中传递一个道具
- kotlin - 在 Kotlin 中使用列表或数组的范围
- javascript - 通过“闪烁”到自动获得最终值的动画元素高度
- html - Chrome 不会在 html 中自动播放我想要的音乐
- database - Data Vault 2.0 - 什么是多活动卫星?