cipher2 0.3.5

Build Status

cipher2 #

A flutter plugin project for AES encryption and decrytion which support both ios and android.

本插件帮助开发者在自己的应用内使用AES加密解密。

This package contains a set of high-level functions for the encryption and decryption. For now, this package only support AES algorithm. Also, there are two modes support right now. The first one is CBC 128 bit padding 7, and second is GCM 128 bit. I will continue working on this project to make it sopport other mode of AES, and the other algorithms like DES, MD5, SHA1 and so on.

all strings in this plugin use UTF8 encoding

本插件所有字符串都使用utf8编码

Features:

  • AES CBC mode 128bit pkcs padding 7 (works for both ios and android)
  • AES GCM mode 128bit (works for android only now)

I will update this plugin if anyone require other encrytion method.

如果有需要,我会更新插件代码以支持其他加密模式,以及其他加密算法。

Getting Started #

Instructions #

  1. Open a command line and cd to your projects root folder. (打开命令行,cd进入到项目根目录)
  2. In your pubspec, add an entry for cipher2 to your dependencies. The example shows below(打开pubspec.yaml, 加入cipher2依赖项,下面有例子)
  3. execute pub install in cmd (命令行执行pub install命令)
  4. If you wish to run tests, just run the example project example\lib\main.dart, and the test case will be running on the app start. (如果你想看单元测试,请执行example项目example\lib\main.dart, 测试用例会在app启动时候自动执行)

Pubspec #

you can use 'any' instead of a version if you just want the latest always

dependencies:
  cipher2: 0.3.0

Usage #

Define plain text to be encrypted, key, iv and generate a nonce for GCM mode

String plainText = '我是shyandsy,never give up man';
String key = 'xxxxxxxxxxxxxxxx';
String iv = 'yyyyyyyyyyyyyyyy';
String nonce = await Cipher2.generateNonce();   // generate a nonce for gcm mode we use later

AES CBC mode 128bit pkcs padding 7 #

Encryption: this method will return a based 64 encoded ciphertext

/*
Cipher2.encryptAesCbc128Padding7

Parameters:
    plainText: the string to be encrypted
        plainText: 被加密字符串
    key: a string with 128bit length.
        key:128 bit字符串
    iv: string with 128bit length.
        iv: 128 bit字符串

Return:
    String, the base64 encoded encrypted data
*/
String encryptedString = await Cipher2.encryptAesCbc128Padding7(plainText, key, iv);

Decryption

/*
Cipher2.decryptAesCbc128Padding7

Parameters:
    encryptedString: the base64 encoded encrypted data
        encryptedString: base64编码的密文字符串
    key: a string with 128bit length.
        key:128 bit字符串
    iv: string with 128bit length.
        iv: 128 bit字符串

Return:
    String, the plainText
*/
decryptedString = await Cipher2.decryptAesCbc128Padding7(encryptedString, key, iv);

AES GCM mode 128bit #

Encrytion

/*
Cipher2.encryptAesGcm128

Parameters:
    plainText: the string to be encrypted
        plainText: 被加密字符串
    key: a string with 128bit length.
        key:128 bit字符串
    nonce: based4 encoded 92bit nonce, can be generate by the method Cipher2.generateNonce()
        nonce: based4编码的92bit nonce,可以用Cipher2.generateNonce()生成

Return:
    String, the base64 encoded encrypted data
*/
encryptedString = await Cipher2.encryptAesGcm128(plaintext, key, nonce);

Decryption #

/*
Cipher2.decryptAesGcm128

Parameters:
    encryptedString: the base64 encoded encrypted data
        encryptedString: base64编码的密文字符串
    key: a string with 128bit length.
        key:128 bit字符串
    nonce: based4 encoded 92bit nonce, can be generate by the method Cipher2.generateNonce()
        nonce: based4编码的92bit nonce,可以用Cipher2.generateNonce()生成

Return:
    String, the plainText
*/
result = await Cipher2.decryptAesGcm128(encryptedString, key, nonce);

API #

static Future

Encryption method for the AES CBC mode 128bit pkcs padding 7

  • data:(String) the string to be encrypted (被加密字符串)
  • key:(String) a string with 128bit length. (128 bit字符串)
  • iv:(String) string with 128bit length. (128 bit字符串)

Cipher2.encryptAesCbc128Padding7() return a String, which is a base64 encoded encrypted data

static Future

Decryption method for the AES CBC mode 128bit pkcs padding 7

  • data:(String) the base64 encoded encrypted data (base64编码的密文字符串)
  • key:(String) a string with 128bit length. (128 bit字符串)
  • iv:(String) string with 128bit length. (128 bit字符串)

Cipher2.encryptAesCbc128Padding7() return the plain text

static Future

Cipher2.generateNonce() return a string of base64 encoded nonce, the nonce is 92 bit(12 byte)

static Future

Encryption method for the AES GCM mode 128bit

  • data:(String) the string to be encrypted (被加密字符串)
  • key:(String) a string with 128bit length. (128 bit字符串)
  • nonce:(String) based4 encoded 92bit nonce, can be generate by the method Cipher2.generateNonce(). (based4编码的92bit nonce,可以用Cipher2.generateNonce()生成)

Cipher2.encryptAesGcm128() return a String, which is a base64 encoded encrypted data

static Future

Dncryption method for the AES GCM mode 128bit

  • data:(String) the base64 encoded encrypted data (base64编码的密文字符串)
  • key:(String) a string with 128bit length. (128 bit字符串)
  • nonce:(String) based4 encoded 92bit nonce, can be generate by the method Cipher2.generateNonce(). (based4编码的92bit nonce,可以用Cipher2.generateNonce()生成)

Cipher2.decryptAesGcm128() return a return the plain text

Unit test #

First, I havent find a way in the unit test document to test the method provided by the native code. I will appreciate if you teach me how to do that!

Instead, I do that in the example code. Look the code in example\lib\main.dart.

String encryptedString;
String plainText = '我是shyandsy,never give up man';
String key = 'xxxxxxxxxxxxxxxx';
String iv = 'yyyyyyyyyyyyyyyy';
String decryptedString;

// test
await testEncryptAesCbc128Padding7();

await testDecryptAesCbc128Padding7();

await testEncryptAesGcm128(); // GenerateNonce();

Exception handle #

there are three types of exceptions in this plugin

  • ERROR_INVALID_KEY_OR_IV_LENGTH

    the length of key or iv is invalid

  • ERROR_INVALID_PARAMETER_TYPE

    all parameters must be string

  • ERROR_INVALID_ENCRYPTED_DATA

    the string to be descrytion must be a valid base64 string with the length at multiple of 128 bits

String encryptedString = '我是shyandsy,never give up man';
String key = 'xxxxxxxxxxxxxxxx';
String iv = 'yyyyyyyyyyyyyyyy';

try {
    // encrytion
    plainText = await Cipher2.decryptAesCbc128Padding7(encryptedString, key, iv);
    print("testDecrytion case6: failed");
} on PlatformException catch(e) {
    encryptedString = "";
    if(e.code == "ERROR_INVALID_ENCRYPTED_DATA"){
        print("testDecrytion: pass");
    }else{
        print("testDecrytion: failed");
    }
}

v0.0.1

  • Support AES 128 bit CBC with Padding 7 encrytion and descryption for android system.
  • method Cipher2.encryptAesCbc128Padding7(plaintext, key, iv);
  • method Cipher2.decryptAesCbc128Padding7(encryptedString, key, iv);

v0.1.0

  • AES CBC PKCSPADDING7 for both ios and android

v0.2.0

  • Add exception handle for the invalid parameter

v0.2.1

  • [bugfix], Add return statement after result.error in kotlin code to avoid the error message in console.

v0.3.0

  • Support AES 128 bit GCM encrytion and descryption (for android now)
  • method Cipher2.encryptAesGcm128(plaintext, key, iv);
  • method Cipher2.decryptAesGcm128(encryptedString, key, iv);

v0.3.1

  • update the CHANGELOG.md
  • update the README.md
  • update the widget test for example project
  • format the lib/cipher2.dart

v0.3.2

  • update the CHANGELOG.md

v0.3.3

  • update the CHANGELOG.md

v0.3.4

  • add dart doc for cipher2.dart

v0.3.5

  • switch to android.util.Base64 to support lower version android
  • add exception handle for bad encryted data

example/README.md

cipher2_example #

Demonstrates how to use the cipher2 plugin.

Getting Started #

This project is a plugin package for flutter which implements AES encrytion and decryption.

String plainText = '我是shyandsy,never give up man';
String key = 'xxxxxxxxxxxxxxxx';
String iv = 'yyyyyyyyyyyyyyyy';

encrytion #

this method will return a based 64 encoded ciphertext

// encrytion
String encryptedString = await Cipher2.encryptAesCbc128Padding7(plainText, key, iv);

decryption #

// decrytion
decryptedString = await Cipher2.decryptAesCbc128Padding7(encryptedString, key, iv);

Use this package as a library

1. Depend on it

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


dependencies:
  cipher2: ^0.3.5

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:cipher2/cipher2.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
84
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]
92
Learn more about scoring.

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

  • Dart: 2.5.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/cipher2.dart.

Run flutter format to format lib/cipher2.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
test ^1.5.1