【问题标题】:Unhandled Exception: FormatException: Invalid character (at character 1)未处理的异常:FormatException:无效字符(字符 1)
【发布时间】: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


【解决方案1】:

我面临同样的问题,在我的案例中,问题与我传递给解密的字符串有关。请检查您传递的加密文本是否已正确加密(没有任何空格)。

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 2021-06-11
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 2021-10-20
    • 1970-01-01
    • 2021-07-31
    相关资源
    最近更新 更多