Gunjancryption Flutter Package

Gunjancryption is a Flutter package that provides robust encryption, decryption, and signature functionalities using RSA and AES algorithms. It is designed for hybrid encryption workflows where performance, security, and reliability are essential.

This package supports encrypting JSON data, signing payloads, and decrypting hybrid-encrypted data, making it ideal for securing sensitive data in Flutter applications.


Features

  • Hybrid Encryption: Combines RSA and AES for secure and efficient data encryption.
  • Digital Signatures: Generate and verify digital signatures for secure payload validation.
  • RSA Key Management: Load and manage RSA keys from PEM files.
  • Validation: Verify encrypted data integrity and validate payloads.
  • Base64 Encoding/Decoding: Easily handle Base64 encoding of keys and payloads.

Installation

Add the package to your pubspec.yaml:

dependencies:
  gunjancryption: ^1.0.0

Run:

flutter pub get

Getting Started

Import the Package

import 'package:gunjancryption/gunjancryption.dart';

Example Usage

1. Load RSA Keys

Load the public and private keys from PEM files stored in the assets folder.

final gunjancryption = Gunjancryption();

final publicKey = await gunjancryption.loadPublicKeyFromPemFile(
  fullPath: 'assets/public_key.pem',
);

final privateKey = await gunjancryption.loadPrivateKeyFromPemFile(
  fullPath: 'assets/private_key.pem',
);

2. Encrypt JSON Data

Encrypt JSON data using an RSA public key.

final rawJSONData = {'name': 'Alice', 'age': 25, 'city': 'Wonderland'};

final encryptedData = await gunjancryption.encryptJsonDataWithPrivateKey(
  publicKey: publicKey,
  rawJSONData: rawJSONData,
);

print('Encrypted Data: $encryptedData');

3. Decrypt Hybrid Encrypted Data

Decrypt hybrid encrypted data using an RSA private key.

final encryptedDataObject = EncryptedData(
  encryptedKey: base64Encode(Uint8List(16)), // Mock key
  iv: base64Encode(Uint8List(16)),          // Mock IV
  encryptedData: base64Encode(Uint8List(64)), // Mock encrypted payload
  privateKey: privateKey,
);

final decryptedData = await gunjancryption.decryptRSAHybridData(
  encryptedDataObject,
);

print('Decrypted Data: $decryptedData');

4. Generate Digital Signature

Generate a digital signature for a JSON payload.

final signature = await gunjancryption.getSignature(
  rawJSONData: {'transactionId': '12345', 'amount': 100, 'currency': 'USD'},
  fullKeyPath: 'assets/private_key.pem',
);

print('Digital Signature: $signature');

5. Validate Encrypted Data

Verify that encrypted data is correctly formatted and valid.

final isValid = gunjancryption.verifyEncryptedData(encryptedDataObject);

print('Encrypted Data is Valid: $isValid');

Detailed API Documentation

loadPrivateKeyFromPemFile

Signature:

Future<RSAPrivateKey> loadPrivateKeyFromPemFile({required String fullPath});

Description:
Loads an RSA private key from a PEM file.

Parameters:

  • fullPath: The full path to the private key PEM file.

Returns:
An RSAPrivateKey object.


loadPublicKeyFromPemFile

Signature:

Future<RSAPublicKey> loadPublicKeyFromPemFile({required String fullPath});

Description:
Loads an RSA public key from a PEM file.

Parameters:

  • fullPath: The full path to the public key PEM file.

Returns:
An RSAPublicKey object.


encryptJsonDataWithPrivateKey

Signature:

Future<String> encryptJsonDataWithPrivateKey({
  required RSAPublicKey publicKey,
  required Map<String, Object> rawJSONData,
});

Description:
Encrypts JSON data using an RSA public key.

Parameters:

  • publicKey: The RSA public key.
  • rawJSONData: A map containing the JSON data to encrypt.

Returns:
A Base64-encoded string of the encrypted data.


decryptRSAHybridData

Signature:

Future<dynamic> decryptRSAHybridData(EncryptedData encryptedData);

Description:
Decrypts hybrid encrypted data using RSA and AES algorithms.

Parameters:

  • encryptedData: An EncryptedData object containing the encrypted key, IV, and data.

Returns:
The decrypted JSON payload.


getSignature

Signature:

Future<String> getSignature({
  required Map<String, Object> rawJSONData,
  required String fullKeyPath,
});

Description:
Generates a digital signature for a JSON payload using an RSA private key.

Parameters:

  • rawJSONData: The JSON payload.
  • fullKeyPath: The full path to the private key PEM file.

Returns:
A Base64-encoded string of the digital signature.


verifyEncryptedData

Signature:

bool verifyEncryptedData(EncryptedData data);

Description:
Validates the integrity and format of encrypted data.

Parameters:

  • data: An EncryptedData object.

Returns:
true if the data is valid; otherwise, throws an exception.


EncryptedData

A data class representing the encrypted payload.

Fields:

  • encryptedKey: Base64-encoded AES key.
  • iv: Base64-encoded initialization vector.
  • encryptedData: Base64-encoded encrypted payload.
  • privateKey: The RSA private key for decryption.

Error Handling

  • Invalid RSA Keys: Throws an Exception if invalid or empty keys are provided.
  • Malformed Data: Throws a FormatException for incorrectly formatted Base64 strings or invalid IV lengths.
  • Serialization Errors: Ensures JSON payloads are serializable and throws descriptive errors otherwise.

Contributing

Contributions are welcome! Please submit issues or pull requests via GitHub.


License

This package is distributed under the MIT License. See the LICENSE file for more information.


Contact

For further inquiries, please email: gunjansharma1112info@gmail.com.