【发布时间】:2022-11-14 23:06:45
【问题描述】:
我正在解密消息并从 String.fromCharCodes 收到此错误
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Invalid character (at character 1)
我用它来加密我的消息:
/// Encrypting String
String encrypt(String plaintext, RSAPublicKey publicKey) {
var cipher = new RSAEngine()
..init(true, new PublicKeyParameter<RSAPublicKey>(publicKey));
var utf8Encoded = utf8.encode(plaintext);
var encoded64 = base64.encode(utf8Encoded);
var cipherText = cipher.process(new Uint8List.fromList(encoded64.codeUnits));
var result = String.fromCharCodes(cipherText);
return result;
}
接着
/// Decrypting String
String decrypt(String ciphertext, RSAPrivateKey privateKey) {
var cipher = new RSAEngine()
..init(false, new PrivateKeyParameter<RSAPrivateKey>(privateKey));
var decrypted = cipher.process(new Uint8List.fromList(ciphertext.codeUnits));
var encoded64 = String.fromCharCodes(decrypted);
var decoded64 = utf8.decode(base64.decode(encoded64));
return decoded64;
}
在解密过程中,我确实在 encode64 中有无效的字符,但我不明白为什么。
我不明白为什么我会遇到这个问题,或者如何预防/避免它。有人可以帮我吗?
【问题讨论】:
-
你用什么包?
-
pointycastle,我刚刚看到 ciphertext.codeUnits 包含 UTF-16 顺便说一句。我在解密过程中收到错误
标签: flutter encryption char