【问题标题】:Decrypt Modulr secure token解密 Modulr 安全令牌
【发布时间】:2022-11-02 03:27:55
【问题描述】:

在我的 Flutter App 中,我需要从 Modulr API 解密一个安全令牌。 首先,我需要生成一个 RSA 密钥,Modulr 将使用它来生成一个安全令牌。

生成令牌时,我会收到一个 encryptedSymmetricKey、一个 initialisationVector (iv) 和一个我需要解密的令牌。

encryptedSymmetricKey 使用带有 OAEP 的 RSA ECB 密码和散列 SHA-256 进行编码。

然后使用解密后的 encryptedSymmetricKey,我可以解密使用 AES GCM 密码编码的令牌,无需填充。

我正在使用 pointycastle 包。

这是我的代码:

  /// 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)

知道如何解决此错误吗?

此外,这是 Modlur 文档:https://modulr.readme.io/docs/retrieve-secure-card-details

【问题讨论】:

    标签: flutter dart encryption cryptography rsa


    【解决方案1】:

    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 代码相同。

    【讨论】:

      猜你喜欢
      • 2013-09-20
      • 2013-08-15
      • 1970-01-01
      • 2014-08-16
      • 2018-11-12
      • 2015-08-17
      • 1970-01-01
      • 2019-04-28
      相关资源
      最近更新 更多