【发布时间】: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