jwe_encryption_package 0.0.1 copy "jwe_encryption_package: ^0.0.1" to clipboard
jwe_encryption_package: ^0.0.1 copied to clipboard

PlatformAndroid

A project that provide the functionality with JWE encryption nd decryption

example/lib/main.dart

import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:jwe_encryption_package/jwe_encryption_package.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _publicKey = '';
  String _privateKey = '';
  String _encryptedPayload = '';
  String _decryptedPayload = '';

  var json = {"name": "John Doe", "email": "[email protected]"};

  @override
  void initState() {
    super.initState();
    _generateKeyPair(); // Start by generating a key pair when the app initializes
  }

  // Function to generate a key pair
  Future<void> _generateKeyPair() async {
    try {
      // Call the function to generate a key pair from your plugin

      List<String> keyPair = await generateKeyPair();
      String? keys = await JweEncryptionPackage().getkeysData();
      List<String> keyParts = keys!.split('\n');

// Now keyParts[0] contains publicKeyString and keyParts[1] contains privateKeyString
      String publicKeyString = keyParts[0];
      String privateKeyString = keyParts[1];

      print('publicKeyString: $publicKeyString');
      print('privateKeyString: $privateKeyString');

      setState(() {
        _publicKey = publicKeyString; // Store the public key
        _privateKey = privateKeyString; // Store the private key
      });
    } catch (e) {
      print('Error generating key pair: $e');
    }
  }

  List<String> generateKeyPair() {
    // Generate random bytes for the keys
    final random = Random.secure();
    final keyBytes = List<int>.generate(32, (index) => random.nextInt(256));

    // Encode the bytes using Base64
    final publicKey = base64UrlEncode(keyBytes);
    final privateKey = base64UrlEncode(keyBytes.reversed.toList());

    return [publicKey, privateKey];
  }

  // Function to encrypt and sign a payload
  Future<void> _encryptPayload() async {
    try {
      // Call the function to encrypt and sign the payload
      String? encryptedPayload = await JweEncryptionPackage()
          .jweEncryptAndSign(_publicKey, _privateKey, json.toString());
      setState(() {
        _encryptedPayload = encryptedPayload!; // Store the encrypted payload
      });
    } catch (e) {
      print('Error encrypting payload: $e');
    }
  }

  // Function to verify and decrypt a payload
  Future<void> _decryptPayload() async {
    try {
      // Call the function to verify and decrypt the payload
      String? decryptedPayload = await JweEncryptionPackage()
          .jweVerifyAndDecrypt(_publicKey, _privateKey, _encryptedPayload);
      setState(() {
        _decryptedPayload = decryptedPayload!; // Store the decrypted payload
      });
    } catch (e) {
      print('Error decrypting payload: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter JWE Encryption Plugin'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: _encryptPayload,
                child: Text('Encrypt Payload'),
              ),
              Text('Encrypted Payload: $_encryptedPayload'),
              ElevatedButton(
                onPressed: _decryptPayload,
                child: Text('Decrypt Payload'),
              ),
              Text('Decrypted Payload: $_decryptedPayload'),
            ],
          ),
        ),
      ),
    );
  }
}
1
likes
140
points
15
downloads

Publisher

unverified uploader

Weekly Downloads

A project that provide the functionality with JWE encryption nd decryption

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on jwe_encryption_package

Packages that implement jwe_encryption_package