cipher2 0.3.0 cipher2: ^0.3.0 copied to clipboard
A plugin for AES encrytion and decrytion.
cipher2 #
A flutter plugin project for AES encryption and decrytion which support both ios and android.
本插件帮助开发者在自己的应用内使用AES加密解密。
For now, it supports the following modes for AES.
- AES CBC mode 128bit pkcs padding 7 (works for both ios and android)
- AES GCM mode 128bit (works for android only now)
目前,本插件只支持下列AES模式加密解密, 如果你需要其他加密方式或者AES加密其他模式,请给我留言。我会升级这个插件。
- AES CBC mode 128bit pkcs padding 7
- AES GCM mode 128bit
I will update this plugin if anyone require other encrytion method.
如果有需要,我会更新插件代码以支持其他加密模式,以及其他加密算法。
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';
String nonce = await Cipher2.generateNonce(); // generate a nonce for gcm mode we use later
all strings in this plugin use UTF8 encoding
AES CBC mode 128bit pkcs padding 7 #
Encrytion #
this method will return a based 64 encoded ciphertext
// encrytion
// both of key and iv are string with 128bit length (key和iv分别对应秘钥和向量,字符串格式,长度都是128位)
String encryptedString = await Cipher2.encryptAesCbc128Padding7(plainText, key, iv);
Decryption #
// decrytion
// both of key and iv are string with 128bit length (key和iv分别对应秘钥和向量,字符串格式,长度都是128位)
decryptedString = await Cipher2.decryptAesCbc128Padding7(encryptedString, key, iv);
AES GCM mode 128bit #
Encrytion #
this method will return a based 64 encoded ciphertext
// encrytion
// the key is a string with 128bit length (key是128位秘钥字符串)
// the nonce was generated by the method Cipher2.generateNonce(), and it is a base64 encoded 12 bytes (nonce是由Cipher2.generateNonce()方法生成的base64编码的字符串,原始数据长度12字节,这是GCM模式推荐长度)
encryptedString = await Cipher2.encryptAesGcm128(plaintext, key, nonce);
Decryption #
// encrytion
// the key is a string with 128bit length (key是128位秘钥字符串)
// the nonce was generated by the method Cipher2.generateNonce(), and it is a base64 encoded 12 bytes (nonce是由Cipher2.generateNonce()方法生成的base64编码的字符串,原始数据长度12字节,这是GCM模式推荐长度)
result = await Cipher2.decryptAesGcm128(encryptedString, key, nonce);
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");
}
}