• flutterdart chacha20加密


    Usage
    encrypt:

    Future<Map?> encrypt(List<int> data, List<int> key)
    
    this method returns Map of {
        encrypted: List<int>,
        tag: List<int>,
        nonce: List<int> 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    // tag: authentication tag or MAC (message authentication code), the algorithm uses it to verify whether or not the ciphertext (encrypted data) and/or associated data have been modified.

    // nonce: (or “initialization vector”, “IV”, “salt”) is a unique non-secret sequence of data required by most cipher (encryption) algorithms, making the ciphertext (encrypted data) unique despite the same key
    encryptString:

    Future<Map?> encryptString(String string, String key, String keyEncoding, String outputEncoding)
    
    • 1

    this method takes inputs of UTF8 string, key in base64 or hex encoding string depending on keyEncoding

    this method returns Map of {
        encrypted: String (in base64 or hex encoded string depending on outputEncoding),
        tag: String (in base64 or hex encoded string depending on outputEncoding),
        nonce: String (in base64 or hex encoded string depending on outputEncoding) 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    decrypt:

    Future<List<int>?>decrypt(List<int> encrypted, List<int> key, List<int> nonce, List<int> tag)
    
    • 1

    this method returns List
    decryptString:

    Future<String?>decryptString(String inputEncoding, String encryptedString, String key, String nonce, String tag)
    
    • 1

    this method takes inputs in base64 or hex encoding string depending on inputEncoding

    this method returns String in UTF8

    try {
        final key =
        await FlutterKeyGenerator.generateSymmetricKey(256);
    
        print("256 bit key: ${key}");
        // new unique key will be generated every time generateSymmetricKey is run
        // [116, 89, 78, 246, 24, 145, 69, 153, 89, 21, 182, 39, 208, 83, 28, 190, 10, 254, 168, 181, 192, 9, 37, 129, 186, 197, 78, 107, 111, 196, 119, 250]
    
        if (key != null) {
        /*
        / Data in bytes encryption:
        */
        print("=================================================================================================================================================");
        print("Data in bytes encryption:");
        final data = [1, 2, 3];
        final sealedBox = await FlutterChacha20Poly1305.encrypt(data, key);
    
        print("Encrypted: ${sealedBox}");
        // Data in this object will be unique every time FlutterChacha20Poly1305.encrypt(data, key); is run
        // {
        //   encrypted: [48, 140, 193],
        //   tag: [2, 233, 22, 168, 82, 89, 110, 176, 158, 180, 147, 83, 250, 56, 15, 97],
        //   nonce: [186, 194, 249, 80, 235, 2, 210, 108, 23, 3, 133, 172] 
        // }
    
        final encrypted = [48, 140, 193];
        final uniqueKey = [116, 89, 78, 246, 24, 145, 69, 153, 89, 21, 182, 39, 208, 83, 28, 190, 10, 254, 168, 181, 192, 9, 37, 129, 186, 197, 78, 107, 111, 196, 119, 250];
        final nonce = [186, 194, 249, 80, 235, 2, 210, 108, 23, 3, 133, 172];
        final tag = [2, 233, 22, 168, 82, 89, 110, 176, 158, 180, 147, 83, 250, 56, 15, 97];
    
        final decryptedData = await FlutterChacha20Poly1305.decrypt(encrypted, uniqueKey, nonce, tag);
        print("Decrypted bytes: ${decryptedData} \n\n");
        // Decrypted bytes: [1, 2, 3]
    
        /*
        / Data in string encryption with key, nonce, tag in base64 or hex encoding
        */
    
        print("=================================================================================================================================================");
        print("Data in string encryption with key, nonce, tag in base64 or hex encoding:");
    
        final jsonString = "{ x: 1, y: 2, z: 3 }";
        final keyInBase64 = "LAnIp48R+r525MH9kme671+2Z2sta+yRGGmA783KBl8=";
        final keyEncoding = "base64";
        final outputEncoding = "base64"; // or "hex"
    
        final encryptStringObject = await FlutterChacha20Poly1305.encryptString(jsonString, keyInBase64, keyEncoding, outputEncoding);
        print("String encrypted object: ${encryptStringObject}");
        // Data in this object will be unique every time FlutterChacha20Poly1305.encryptString(jsonString, keyInBase64, keyEncoding, outputEncoding) is run
        // {
        //   tag: T/dbBWayYCdN+yvOvGU61Q==,
        //   encrypted: +OxmNIVA6gvwOCoQJAalHQS4Baw=,
        //   nonce: j43/wSHX6Dh6jIAF
        // }
    
        final inputEncoding = "base64"; // all the params below must be in base64 encoded string
        final encryptedString = "+OxmNIVA6gvwOCoQJAalHQS4Baw=";
        final tagBase64 = "T/dbBWayYCdN+yvOvGU61Q==";
        final nonceBase64 = "j43/wSHX6Dh6jIAF";
    
        final decryptedJSONString = await FlutterChacha20Poly1305.decryptString(inputEncoding, encryptedString, keyInBase64, nonceBase64, tagBase64);
        print("Decrypted JSON string: ${decryptedJSONString}");
        // { x: 1, y: 2, z: 3 }
        }
    } catch (e) {
        print(e);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    https://github.com/hayr-hotoca/flutter_chacha20_poly1305

  • 相关阅读:
    $set小概率的赋值失败问题都被我碰上了
    纷享销客《2022新增长系列之快消行业橙皮书》重磅发布
    MFC使用system有弹黑窗的解决 用WinExec(szBuffer, SW_HIDE);代替
    SpringBoot+Vue+Element-UI实现自动排课系统
    AD单片机九齐单片机NY8B062D SOP16九齐
    【高等数学基础进阶】定积分应用
    Android recyclerview 浮动头部
    APS计划排程在半导体行业的应用
    配置h5py、netCDF4库的方法:Anaconda环境
    微信小程序 movable-area 区域拖动动态组件演示
  • 原文地址:https://blog.csdn.net/shelutai/article/details/133880352