Flutter使用Cipher2插件實現AES加解密

Flutter是當下最流行的新興APP跨平臺開發架構。學習需趁早。

因爲我的項目需要使用AES加解密,而flutter package中並沒有支持Dart 2的AES加密庫,所以寫了Cipher2插件並拿出來開源給大家用。本文介紹如何使用Cipher2插件在Flutter app中實現AES加密解密。本文不講述如何安裝配置Flutter開發環境。如有需要我會寫關於安裝配置flutter開環環境的文章。

Cipher2插件地址:

各位如果有其他加密算法需求,請在github發issue,我會盡快跟進。PR is welcome!

創建項目

打開cmd命令行,cd命令定位到你想要創建項目的目錄。然後創建flutter app項目

flutter create cipher2_test

圖片描述

用vscode打開項目目錄

圖片描述

安裝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就會用了。這裏不再重複

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章