对于许多有经验的开发人员来说,这可能是一个愚蠢而多余的问题,但是我没有经验,所以我提出了这个问题。我想访问此框中的加密值,以直观地手动验证并观察加密是否已完成。我似乎无法打印加密的值,只有解密的值出来。
我正在用一些演示代码测试这个例子。
我做错了什么还是 .get() 方法默认在通话时解密?
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:hive/hive.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive_flutter/hive_flutter.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
String key = 'key';
String secret = 'secret value';
final FlutterSecureStorage secureStorage = const FlutterSecureStorage();
var containsEncryptionKey = await secureStorage.containsKey(key: 'key');
if (!containsEncryptionKey) {
var key = Hive.generateSecureKey();
await secureStorage.write(key: 'key', value: base64UrlEncode(key));
}
print(await secureStorage.readAll());
var encryptionKey = base64Url.decode(await secureStorage.read(key: 'key'));
print('Encryption key: $encryptionKey');
var encryptedBox = await Hive.openBox('secure_box',
encryptionCipher: HiveAesCipher(encryptionKey));
encryptedBox.put(key, secret);
print(Hive.box('secure_box').values); // Should this not be encrypted text?
print(encryptedBox.get(key));
}
输出:
flutter: {key: Wr1fM3XHtIefLX8JKGJfPNiHdaWiNZspbml6NJeJkTk=}
flutter: Encryption key: [90, 189, 95, 51, 117, 199, 180, 135, 159, 45, 127, 9, 40, 98, 95, 60, 216, 135, 117, 165, 162, 53, 155, 41, 110, 105, 122, 52, 151, 137, 145, 57]
flutter: (secret value, secret_value)
flutter: secret_value