在我的Ffltter应用程序中,我需要解密来自ModulR API的安全令牌. 首先,我需要生成RSA密钥,模块R将使用它来生成安全令牌.

生成令牌时,我会收到一个加密的SymmetricKey、一个初始化向量(Iv)和一个需要解密的令牌.

加密的SymmetricKey使用RSA ECB密码和OAEP和散列SHA-256进行编码.

然后,使用解密的加密的SymmetricKey,我可以解密用没有填充的AES GCM密码编码的令牌.

我用的是PointtyCastle套餐.

这是我的代码:

  /// DECRYPT SYMMETRIC KEY
  final p = OAEPEncoding.withSHA256(RSAEngine());

  p.init(false, PrivateKeyParameter<RSAPrivateKey>(modulrKey.keypair.privateKey.asPointyCastle));

  final decryptedSymetricKeyBytes = p.process(base64Decode(result.encryptedSymmetricKey));

  /// AES-GCM ALGO
  final algo = AesGcm.with128bits();

  /// DECODE INIT VECTOR
  final decodedIv = base64Decode(result.initialisationVector);

  /// AES KEY
  final aesKey = await algo.newSecretKeyFromBytes(decryptedSymetricKeyBytes);

  /// DECRYPT TOKEN
  final decodedToken = base64Decode(result.token);

  final secretBox = SecretBox(decodedToken, nonce: decodedIv, mac: Mac.empty);
  final decryptedTokenBytes = await algo.decrypt(secretBox, secretKey: aesKey);
  final decryptedToken = base64Encode(decryptedTokenBytes);

但是当我执行它时,我得到这个错误:

SecretBox的报文验证码(MAC)错误

你有什么办法解决这个问题吗? 此外,这是MODBUR文档:https://modulr.readme.io/docs/retrieve-secure-card-details

推荐答案

linked JavaScript and Java code表示decodedToken是实际密文和16字节GCM标签的串联.但是,由于DART代码中使用的cryptography包独立处理密文和标签,因此必须首先将这两个部分分开.然后它们可以被传递到SecretBox.解密的数据必须是UTF-8解码的.

一种可能的解决方案是:

import 'dart:convert';
import 'package:cryptography/cryptography.dart';
...
// Separate ciphertext and tag
final decodedToken = base64Decode(result.token);
final token  = decodedToken.sublist(0, decodedToken.length - 16);
final tag = decodedToken.sublist(decodedToken.length - 16);
...
// Apply ciphertext and tag
final secretBox = SecretBox(token, nonce: decodedIv, mac: Mac(tag));
...
// Utf-8 decode
final decryptedToken = utf8.decode(decryptedTokenBytes);

这样,发布的DART代码在功能上分别与链接的JavaScript和Java代码相同.

Flutter相关问答推荐

如何在flutter文本字段中添加要点条目?

使用自定义控制器将项插入到ListView中时行为不正确

如何让一个名为ONTAP的回调函数、另一个名为onTapDown的回调函数和另一个名为onTapUp的回调函数不重叠?

在fltter_widget_from_html中启动url

如何在Flutter 中不从getX库中初始化GetxController中的变量

IsScrollable为True时如何删除Flutter 选项卡栏左侧填充

try 使用fl_Chart of Ffltter,但在导入它时遇到以下错误:Error:';TextScaler';is;t a type

无法构建AndroidBundle 包或iOS等

轻松本地化包翻译在底部导航栏项目标签Flutter 翼dart 中不起作用

如何从flutter中不同类别的列表中获取所有产品

如何从flutter连接PostgreSQL数据库?

为什么 ref.watch(otherProvider.stream) 在 Riverpod 2.x 中被弃用并且将在 3.x 中被删除?

状况不佳 - Cubit

Pub get 尚未运行

在 Flame Forge2d 中射击子弹

从物理设备 Flutter 中移除 USB 后启动画面不可见?

如何在椭圆形图像周围添加边框?Flutter

Firebase Cloud Messaging (Flutter):订阅主题时出现错误消息

Flutter .replaceFirst(或全部)不起作用

在右侧从 a 到 z Flutter 字母列表