Skip to content

decrypted return SecretBox has wrong message authentication code (MAC) #71

Description

@zupvlad

Hi,

I received from python the Ciphertext (this is formated from StringEcrypted + first 4 elements from MAC.
When I'll try to decrypt the message I received wrong MAC.
I think the problem is on MAC. I'm able to set the MAC length with 4 on the SecretBox?
I'll add the code:

Future<List> decrypt(List message) async {
final algorithm = AesGcm.with128bits();
final key = await kdf(isEncrypt: true);
final secretKey = SecretKey(key);
final mac = Mac(message.skip(message.length - 4).toList());
final secretMessage =
SecretBox(message.take(message.length - 4).toList(), nonce: HEX.decode(cpIV), mac: mac);
final clearText = await algorithm.decrypt(secretMessage, secretKey: secretKey);
print(clearText);
return clearText;
}

SecretBox details
"Nonce: [174, 1, 226, 226, 211, 55, 4, 206, 188, 172, 17, 149]"
"Ciphertext: [179, 43, 17]"
"MAC: [255, 47, 7, 170]"

Activity

  1. ramvdixit commented on Dec 13, 2022

    @ramvdixit

    This issue is still there. Here is my code:

    Encryption

    final streamCipherAlgorithm = crypto.Xchacha20.poly1305Aead();
          final nonce = streamCipherAlgorithm.newNonce();
          crypto.SecretKey secretKey = await streamCipherAlgorithm
              .newSecretKeyFromBytes(argonResult.rawBytes);
    
     crypto.SecretBox secretBox = await streamCipherAlgorithm.encrypt(
            utf8.encode(initialMessage),
            secretKey: secretKey,
            nonce: nonce,
            aad: utf8.encode(associatedData),
          );
    
    String encryptedInitialMessage = base64Encode(secretBox.cipherText); // encryptedInitialMessage is sent to the other party along with Mac and nonce.
    

    Decryption

    crypto.SecretBox secretBox = crypto.SecretBox(
            base64Decode(encryptedInitialMessage),
            nonce: base64Decode(nonce),
            mac: crypto.Mac(
              base64Decode(
                mac,
              ),
            ),
          );
    

    Decryption fails with 'SecretBox has wrong Message Authentication Code (MAC)' error.

    I have tried using the 'Cipher' class as shown in the example, still fails on decryption with the same exception.

  2. terrier989 commented on Mar 3, 2023

    @terrier989
    Member

    I believe any possible issue was fixed in cryptography 2.2.0 + cryptography_flutter 2.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions