Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot decrypt bytes and giving vague error message #77

Open
SyedKaz00 opened this issue Oct 1, 2024 · 3 comments
Open

Cannot decrypt bytes and giving vague error message #77

SyedKaz00 opened this issue Oct 1, 2024 · 3 comments

Comments

@SyedKaz00
Copy link

Hi, I am using your library to encrypt and decrypt bytes within packets, it works successfully when Im working with a single packet, however when I fragment and split up the encrypted packet, then reassemble it to decrypt it on another device,
I get this error:
Instance of 'OpenPGPException'
. I also get this error when i split the packet into fragments first, and then encrypt the individual fragments, i get this error when decrypting the individual fragments aswell so I dont believe it is a fragmentation issue. Could you please help explain what this error means, as it works correctly with single packets but not with any once there is fragmentation. This is my section of code where I am decrypting it :

 try {
            PGPDecrypter pgpDecrypter = PGPDecrypter();
            Uint8List decryptedData =
                await pgpDecrypter.pgpDecryptBytes(completeDataBytes);
            // Ensure decrypted data is valid (truncate at null byte)
            decryptedData = Uint8List.fromList(
                decryptedData.takeWhile((byte) => byte != 0).toList());

            // Convert decrypted bytes to UTF-8 string
            String finalData = utf8.decode(decryptedData);

            // Process the data based on payload type
            if (payloadType == 0x01) {
              _insertDataPersistent(sourceUUID, destinationUUID, dateTimeString,
                  payloadType, finalData);
            } else if (payloadType == 0x02) {
              print("Received payment data");
              _insertPaymentDataPersistent(sourceUUID, destinationUUID,
                  dateTimeString, payloadType, finalData);
            }

            // Cleanup buffers for this sequence number
            fragmentBuffer.remove(sequenceNumber);
            receivedFragmentCount.remove(sequenceNumber);
            totalFragmentCount.remove(sequenceNumber);
          } catch (e) {
            print("Error decrypting packet: $e");
            // Handle specific decryption issues, potentially retrying with backoff
          }

And this is where I am encrypting it

//The data to send is now being encrypted

    Uint8List encrypted = await OpenPGP.encryptBytes(data, publicKey);
    // The maximum size of the payload is 469/437 bytes in this packet structure. Minus one more for the time to live
    const int payloadSize = 347;
    int totalFragments = (encrypted.length / payloadSize).ceil();
    print("TOTAL FRAGMENTS: $totalFragments");
    // Get the next sequence number for a new packet or fragment
    int sequenceNumber = sequenceNumberGenerator
        .getNextSequenceNumber(); // Generate or get the next sequence number
    //I think splits the payload anyway
    for (int i = 0; i < totalFragments; i++) {
      int start = i * payloadSize;
      int end = start + payloadSize > encrypted.length
          ? encrypted.length
          : start + payloadSize;
      Uint8List payload = encrypted.sublist(start, end);
      //print("Send encrypted data created with Source $sourceUUID Dest $destinationUUID");
      Uint8List packet = createPacket(
          sourceUUID,
          destinationUUID,
          dateTimeBytes,
          sequenceNumber,
          totalFragments,
          i, // TDOO Dmaybve here
          payloadType,
          payload);

      // Here, you would send this packet to the appropriate device.
      // This is a placeholder; you need to replace it with actual sending logic.
      //await sendPacketToDevice(packet, deviceId);
      //Save to Outoging Table
      insertDataOutgoing(packet);
    }

Thank you for the help :)

@jerson
Copy link
Owner

jerson commented Oct 1, 2024

Hi, thanks for report this issue, which version are you using, we added a toString method a few months ago

https://github.com/jerson/flutter-openpgp/blob/master/lib/openpgp.dart#L15

@SyedKaz00
Copy link
Author

I am using version 3.6.0, also here is the decryption code which works for a single packet but not multiple

  static final PGPDecrypter _instance = PGPDecrypter._internal();

  factory PGPDecrypter() {
    return _instance;
  }

  PGPDecrypter._internal();

  Future<Uint8List> pgpDecryptBytes(Uint8List encryptedBytes) async {
    // Implement decryption logic here
    // Use the provided privateKey and encryptedBytes to decrypt the data
    // Return the decrypted data as a String
    // Example:
    // var decryptedData = await OpenPGP.decryptBytes(
    //   encryptedBytes: encryptedBytes,
    //   privateKey: privateKey,
    // );
    // return utf8.decode(decryptedData);
    var uuid = await DatabaseHelper.instance.getUserUUID();
    var privateKey = await DatabaseHelper.instance.getMyPrivateKey();
    var unencrypted = await OpenPGP.decryptBytes(
      encryptedBytes,
      privateKey,
      uuid,
    );
    return unencrypted;
  }
} ```

@SyedKaz00
Copy link
Author

I updated the code to version 3.8.3 and am now getting this error : OpenPGPException: openpgp: invalid signature: MDC hash mismatch , how do I fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants