gunjancryption 0.0.1 gunjancryption: ^0.0.1 copied to clipboard
Gunjancryption is a Flutter package that provides robust encryption, decryption, and signature functionalities using RSA and AES algorithms.
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:gunjancryption/gunjancryption.dart';
import 'package:gunjancryption/EncryptedData.dart';
import 'package:pointycastle/asymmetric/api.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Gunjancryption Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: EncryptionExampleScreen(),
);
}
}
class EncryptionExampleScreen extends StatefulWidget {
@override
_EncryptionExampleScreenState createState() =>
_EncryptionExampleScreenState();
}
class _EncryptionExampleScreenState extends State<EncryptionExampleScreen> {
final Gunjancryption _gunjancryption = Gunjancryption();
late String _privateKeyPath;
late String _publicKeyPath;
String _encryptedData = '';
String _decryptedData = '';
String _signature = '';
String _verificationStatus = '';
@override
void initState() {
super.initState();
_privateKeyPath = 'assets/private_key.pem'; // Add your private key here
_publicKeyPath = 'assets/public_key.pem'; // Add your public key here
}
Future<void> _encryptAndDecrypt() async {
try {
// Sample JSON data
final Map<String, Object> rawJSONData = {
'name': 'John Doe',
'age': 30,
'city': 'New York'
};
// Load public key
final RSAPublicKey publicKey = await _gunjancryption
.loadPublicKeyFromPemFile(fullPath: _publicKeyPath);
// Encrypt data
final encryptedData = await _gunjancryption.encryptJsonDataWithPrivateKey(
publicKey: publicKey,
rawJSONData: rawJSONData,
);
setState(() {
_encryptedData = encryptedData;
});
// Decrypt data (requires an EncryptedData object and private key)
final RSAPrivateKey privateKey = await _gunjancryption
.loadPrivateKeyFromPemFile(fullPath: _privateKeyPath);
final EncryptedData encryptedDataObject = EncryptedData(
encryptedKey: encryptedData,
iv: base64Encode(Uint8List(16)), // Example IV
encryptedData: encryptedData,
privateKey: privateKey,
);
final decryptedData =
await _gunjancryption.decryptRSAHybridData(encryptedDataObject);
setState(() {
_decryptedData = decryptedData.toString();
});
} catch (e) {
print('Encryption/Decryption error: $e');
}
}
Future<void> _generateSignature() async {
try {
// Sample JSON data
final Map<String, Object> rawJSONData = {
'transactionId': '12345',
'amount': 5000,
'currency': 'USD'
};
// Generate signature using private key
final signature = await _gunjancryption.getSignature(
rawJSONData: rawJSONData,
fullKeyPath: _privateKeyPath,
);
setState(() {
_signature = signature;
});
} catch (e) {
print('Signature generation error: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Gunjancryption Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ElevatedButton(
onPressed: _encryptAndDecrypt,
child: const Text('Encrypt & Decrypt'),
),
const SizedBox(height: 8),
Text('Encrypted Data: $_encryptedData'),
const SizedBox(height: 8),
Text('Decrypted Data: $_decryptedData'),
const Divider(height: 20),
ElevatedButton(
onPressed: _generateSignature,
child: const Text('Generate Signature'),
),
const SizedBox(height: 8),
Text('Generated Signature: $_signature'),
],
),
),
),
);
}
}