解密模块安全令牌



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

当生成令牌时,我收到一个加密的SymmetricKey、一个初始化Vector(iv(和一个我需要解密的令牌。

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

然后,使用解密的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

链接的JavaScript和Java代码显示decodedToken是实际密文和16字节GCM标记的级联。然而,由于Dart代码中使用的密码包独立处理密文和标签,因此必须首先将这两部分分开。然后可以将它们传递给CCD_ 2。解密后的数据必须使用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代码相同。

最新更新