protect

Platform Pub Package License: MIT Donate Issue Forks Stars

Protect is a flutter and dart library for applying and removing password protection on excel files.

Table of Contents

Lets Get Started

1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  protect: ^0.0.2

2. Install it

You can install packages from the command line:

with pub:

$  pub get

with Flutter:

$  flutter packages get

3. Import it

Now in your Dart code, you can use:

    import 'package:protect/protect.dart';

Usage

Imports

    import 'dart:io';
    import 'package:protect/protect.dart';
    

Read XLSX File

    var file = "Path_to_pre_existing_Excel_File/excel_file.xlsx";
    var unprotectedExcelBytes = await File(file).readAsBytes();
    or
  //var protectedExcelBytes = await File(file).readAsBytes();
    

Read XLSX from Flutter's Asset Folder

    import 'package:flutter/services.dart' show ByteData, rootBundle;
    
    /* Your blah blah code here */
    
    ByteData data = await rootBundle.load("assets/existing_excel_file.xlsx");
    var bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    var unprotectedExcelBytes = await File(file).readAsBytes();
    or
  //var protectedExcelBytes = await File(file).readAsBytes();
    

Apply password protection on XLSX File

  ///
  /// Applying password protection
  /// where `unprotectedExcelBytes` is Uint8List
  ///
  ProtectResponse encryptedResponse = await Protect.encryptUint8List(unprotectedExcelBytes, 'contact@kawal.dev');

  var data;
  if (encryptedResponse.isDataValid) {
    data = encryptedResponse.processedBytes;
  } else {
    print('Excel file used for applying password over it is corrupted');
  }
    

Remove password protection on XLSX File

  ///
  /// Applying password protection 
  /// where `protectedExcelBytes` is Uint8List
  ///
  ProtectResponse decryptedResponse = await Protect.decryptUint8List(protectedExcelBytes, 'contact@kawal.dev');
  
  var data;
  if (decryptedResponse.isDataValid) {
    data = decryptedResponse.processedBytes;
  } else {
    print('Either password is wrong for opening the excel file or the Excel file is corrupted');
  }

Saving XLSX File

  // Save the Changes in file
  
  var outputPath = '/Users/kawal/Desktop/form_encrypted_file.xlsx';
  await File(outputPath)
    ..create(recursive: true)
    ..writeAsBytes(data);
    

Also checkout our other libraries:

Oooops, My laptop is slow but I'm not.

Libraries

protect