Flutter使用Cipher2插件实现AES加解密

Flutter是当下最流行的新兴APP跨平台开发架构。学习需趁早。git

由于个人项目须要使用AES加解密,而flutter package中并无支持Dart 2的AES加密库,因此写了Cipher2插件并拿出来开源给你们用。本文介绍如何使用Cipher2插件在Flutter app中实现AES加密解密。本文不讲述如何安装配置Flutter开发环境。若有须要我会写关于安装配置flutter开环环境的文章。github

Cipher2插件地址:算法

各位若是有其余加密算法需求,请在github发issue,我会尽快跟进。PR is welcome!架构

建立项目

打开cmd命令行,cd命令定位到你想要建立项目的目录。而后建立flutter app项目app

flutter create cipher2_test

图片描述

用vscode打开项目目录async

图片描述

安装Cipher2插件

打开上图中pubspec.yaml文件的dependencies中,添加以下内容。而后ctrl + s保存,vscode会自动为你安装好插件。学习

dependencies:
  flutter:
    sdk: flutter
  cipher2: any

图片描述

Cipher2的API解释

Cipher2插件目前支持AES加密的cbc(128位 padding7)模式和gcm模式(128位)。插件提供了5个方法来实现加密解密。本插件全部字符串均使用UTF8编码。在处理第三方密文的时候,请注意字符串编码,以避免没必要要的麻烦。测试

  • AES cbc 128位padding7加密
/*
Cipher2.encryptAesCbc128Padding7
参数:
    plainText: 被加密字符串
    key:128 bit字符串
    iv: 128 bit字符串
返回:
    通过base64编码的密文字符串
*/
String encryptedString = await Cipher2.encryptAesCbc128Padding7(plainText, key, iv);
  • AES cbc 128位padding7解密
/*
Cipher2.decryptAesCbc128Padding7
参数:    
    encryptedString: base64编码的密文字符串
    key:128 bit字符串
    iv: 128 bit字符串
返回:
    明文字符串
*/
String decryptedString = await Cipher2.decryptAesCbc128Padding7(encryptedString, key, iv);
  • 生成GCM模式的nonce
String nonce = Cipher2.generateNonce()
  • AES gcm 128位加密
/*
Cipher2.encryptAesGcm128
参数:    
    plainText: 被加密字符串
    key:128 bit字符串
    nonce: based4编码的92bit nonce,能够用Cipher2.generateNonce()生成
返回:
    通过base64编码的密文字符串
*/
String encryptedString = await Cipher2.encryptAesGcm128(plaintext, key, nonce);
  • AES gcm 128位解密
/*
Cipher2.decryptAesGcm128
参数:
    encryptedString: base64编码的密文字符串
    key:128 bit字符串
    nonce: based4编码的92bit nonce,能够用Cipher2.generateNonce()生成
返回:
    明文字符串
*/
result = await Cipher2.decryptAesGcm128(encryptedString, key, nonce);

使用Cipher2插件

官方提供了很是简单明了的测试用例,方便加密解密和异常捕获编码

https://github.com/shyandsy/c...加密

// Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String encryptedString;
    String plainText = '我是shyandsy,never give up man';
    String key = 'xxxxxxxxxxxxxxxx';
    String iv = 'yyyyyyyyyyyyyyyy';
    String decryptedString;

    // 测试AES 128bit cbc padding 7加密
    await testEncryptAesCbc128Padding7();

    // 测试AES 128bit cbc padding 7解密
    await testDecryptAesCbc128Padding7();

    // 测试AES 128bit gcm加解密
    await testEncryptAesGcm128(); // GenerateNonce();
    
    // 加密,而后解密
    try {
      // 加密
      encryptedString = await Cipher2.encryptAesCbc128Padding7(plainText, key, iv);
      
      // 解密
      decryptedString = await Cipher2.decryptAesCbc128Padding7(encryptedString, key, iv);
    
    } on PlatformException catch(e) {

      encryptedString = "";
      decryptedString = "";
      print("exception code: " + e.code);
      print("exception message: " + e.message);

    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _plainText = plainText;
      _encryptedString = encryptedString;
      _decryptedString = decryptedString;
    });
  }

读一遍test case就会用了。这里再也不重复

相关文章
相关标签/搜索