Flu*_*008 2 rsa encryption-asymmetric public-key-encryption flutter
import 'package:encrypt/encrypt.dart';
import 'package:encrypt/encrypt_io.dart';
import 'dart:io';
import 'package:pointycastle/asymmetric/api.dart';
import 'dart:async';
import 'package:flutter/services.dart' show rootBundle;
class Encrypt {
Future<String> loadPrivateKey() async {
return await rootBundle.loadString('assets/private_key.pem');
}
Future<String> loadPublicKey() async {
return await rootBundle.loadString('assets/public_key.pem');
}
encryptString() async {
print(loadPublicKey().toString());
final publicKey =
await parseKeyFromFile<RSAPublicKey>('${loadPublicKey()}');
final privateKey =
await parseKeyFromFile<RSAPrivateKey>('${loadPrivateKey()}');
final plainText = 'James Bond';
final encrypter =
Encrypter(RSA(publicKey: publicKey, privateKey: privateKey));
final encrypted = encrypter.encrypt(plainText);
final decrypted = encrypter.decrypt(encrypted);
print(decrypted);
print(encrypted.base64);
}
}
Run Code Online (Sandbox Code Playgroud)
错误:正在执行热重载...正在将文件同步到 IA 模拟器上的设备 AOSP...在 1,021 毫秒内重新加载了 707 个库中的 8 个。I/flutter(7395):“未来”的实例 E/flutter(7395):[错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:FileSystemException:无法打开文件,路径=“实例” Future''(操作系统错误:没有这样的文件或目录,errno = 2)
我确实在 yaml 文件中添加了资产:
flutter:
assets:
- assets/
Run Code Online (Sandbox Code Playgroud)
parseKeyFromFile是一个读取文件并解析内容的便捷函数。您没有文件,您有一个资产,您已经在执行读入字符串的工作。读取文件后,它只是解析它 - 这就是您所需要的。
这应该有效:
final publicPem = await rootBundle.loadString('assets/public_key.pem');
final publicKey = RSAKeyParser().parse(publicPem) as RSAPublicKey;
Run Code Online (Sandbox Code Playgroud)
私钥也类似。
| 归档时间: |
|
| 查看次数: |
5885 次 |
| 最近记录: |