Skip to content

Cannot decrypt bytes and giving vague error message #77

@SyedKaz00

Description

@SyedKaz00

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions