encrypt 2.0.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 98

encrypt #

Pub Package Build Status Donate

A set of high-level APIs over PointyCastle for two-way cryptography.

Looking for password hashing? Please, visit password.

API Overview #

Encrypter(Algorithm algo) #

Acts like a Adapter interface for any algorithm. Exposes:

  • Encrypted encrypt(String text) encrypts the given plain-text.
  • String decrypt(Encrypted encrypted) decrypts the given Encrypted value.
  • String decrypt16(String encoded) sugar for decrypt(Encrypted.fromBase16(encoded)).
  • String decrypt64(String encoded) sugar for decrypt(Encrypted.fromBase64(encoded)).

Encrypted(Uint8List bytes) #

Wraps the encrypted bytes. Exposes:

  • Encrypted.fromBase16(String encoded) creates an Encrypted object from a hexdecimal string.
  • Encrypted.fromBase64(String encoded) creates an Encrypted object from a Base64 string.
  • String base16 returns a hexdecimal representation of the bytes.
  • String base64 returns a Base64 representation of the bytes.
  • Uint8List bytes returns raw bytes.

Key(Uint8List bytes) #

Represents an Encryption Key. Exposes:

  • Key.fromBase16(String encoded) creates a Key from a hexdecimal string.
  • Key.fromBase64(String encoded) creates a Key from a Base64 string.
  • Key.fromUtf8(String encoded) creates a Key from a UTF-8 string.
  • Key.fromLength(int length) sugar for Key(Uint8List(length)).

IV(Uint8List bytes) #

Represents an Initialization Vector https://en.wikipedia.org/wiki/Initialization_vector. Exposes:

  • IV.fromBase16(String encoded) creates an IV from a hexdecimal string.
  • IV.fromBase64(String encoded) creates an IV from a Base64 string.
  • IV.fromUtf8(String encoded) creates an IV from a UTF-8 string.
  • IV.fromLength(int length) sugar for IV(Uint8List(length)).

Algorithms #

Current status is:

  • AES with PKCS7 padding
  • RSA with PKCS1 encoding
  • Salsa20

Usage #

Symmetric #

AES #

import 'package:encrypt/encrypt.dart';

void main() {
  final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
  final key = Key.fromUtf8('my 32 length key................');
  final iv = IV.fromLength(16);

  final encrypter = Encrypter(AES(key, iv));

  final encrypted = encrypter.encrypt(plainText);
  final decrypted = encrypter.decrypt(encrypted);

  print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
  print(encrypted.base64); // R4PxiU3h8YoIRqVowBXm36ZcCeNeZ4s1OvVBTfFlZRdmohQqOpPQqD1YecJeZMAop/hZ4OxqgC1WtwvX/hP9mw==
}
Mode of operation #

Default mode is SIC AESMode.sic, you can override it using the mode named parameter:

final encrypter = Encrypter(AES(key, iv, mode: AESMode.cbc));
}
Supported modes are: #
  • CBC AESMode.cbc
  • CFB-64 AESMode.cfb64
  • CTR AESMode.ctr
  • ECB AESMode.ecb
  • OFB-64/GCTR AESMode.ofb64Gctr
  • OFB-64 AESMode.ofb64
  • SIC AESMode.sic

Salsa20 #

import 'package:encrypt/encrypt.dart';

void main() {
  final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
  final key = Key.fromLength(32);
  final iv = IV.fromLength(8);
  final encrypter = Encrypter(Salsa20(key, iv));

  final encrypted = encrypter.encrypt(plainText);
  final decrypted = encrypter.decrypt(encrypted);

  print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
  print(encrypted.base64); // CR+IAWBEx3sA/dLkkFM/orYr9KftrGa7lIFSAAmVPbKIOLDOzGwEi9ohstDBqDLIaXMEeulwXQ==
}

Asymmetric #

RSA #

import 'dart:io';
import 'package:encrypt/encrypt.dart';
import 'package:pointycastle/asymmetric/api.dart';

void main() {
  final publicKeyFile = File('/path/to/public_key.pem');
  final privateKeyFile = File('/path/to/private_key.pem');

  final parser = RSAKeyParser();
  final RSAPublicKey publicKey = parser.parse(publicKeyFile.readAsStringSync());
  final RSAPrivateKey privateKey = parser.parse(privateKeyFile.readAsStringSync());

  final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
  final encrypter = Encrypter(RSA(publicKey: publicKey, privateKey: privateKey));

  final encrypted = encrypter.encrypt(plainText);
  final decrypted = encrypter.decrypt(encrypted);

  print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
  print(encrypted.base64); // XWMuHTeO86gC6SsUh14h+jc4iQW7Vy0TDaBKN926QWhg5c3KKoSuF+6uedLWBEis0LYgTON2rhtTOjmb6bU2P27lgf+5JKdLGKqri2F4sCS3+/p/EPb41f60vnr3whX2o5VRJhJagxtrq0V3eu3X4UeRiO2y7yOt6MXyJxMFcXs=
}
Note #

If you are just encrypting or just decrypting, you can ignore the respectives privateKey and publicKey. Trying the encrypt without a public key or decrypt without a private key will throw a StateError.

2.0.0 #

  • All new API

1.0.1 #

  • RSA

1.0.0 #

  • Stable and documented API

0.2.0+2 #

  • Remove unnecessary news
  • Improve static typing
  • Add examples index (README)

0.2.0+1 #

  • Refresh dependencies, make sure it works on Dart 2

example/README.md

Encrypt examples #

Use this package as a library

1. Depend on it

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


dependencies:
  encrypt: ^2.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:encrypt/encrypt.dart';
  
Version Uploaded Documentation Archive
3.2.0 Jun 18, 2019 Go to the documentation of encrypt 3.2.0 Download encrypt 3.2.0 archive
3.1.0 May 26, 2019 Go to the documentation of encrypt 3.1.0 Download encrypt 3.1.0 archive
3.0.0 Apr 23, 2019 Go to the documentation of encrypt 3.0.0 Download encrypt 3.0.0 archive
2.2.0 Apr 8, 2019 Go to the documentation of encrypt 2.2.0 Download encrypt 2.2.0 archive
2.1.0 Apr 3, 2019 Go to the documentation of encrypt 2.1.0 Download encrypt 2.1.0 archive
2.0.0 Feb 16, 2019 Go to the documentation of encrypt 2.0.0 Download encrypt 2.0.0 archive
1.0.1 Jan 17, 2019 Go to the documentation of encrypt 1.0.1 Download encrypt 1.0.1 archive
1.0.0+1 Dec 17, 2018 Go to the documentation of encrypt 1.0.0+1 Download encrypt 1.0.0+1 archive
1.0.0 Dec 17, 2018 Go to the documentation of encrypt 1.0.0 Download encrypt 1.0.0 archive
0.2.0+2 Oct 28, 2018 Go to the documentation of encrypt 0.2.0+2 Download encrypt 0.2.0+2 archive

All 13 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
96
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
98
Learn more about scoring.

We analyzed this package on Jun 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:encrypt/encrypt.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
asn1lib ^0.5.3 0.5.8
collection ^1.14.11 1.14.11
pointycastle ^1.0.0 1.0.1
Dev dependencies
test ^1.5.1+1