如何在Flutter / Dart中实施加密(3des)

时间:2019-07-02 17:36:54

标签: flutter dart

如何在Dart中实现3des加密。

我下载了此库(https://pub.dev/packages/tripledes),但是我无法添加require值。例如,如何传递iv值。

这是我需要转换为dart的JS代码(使用crypto.js)

  {
     key = CryptoJS.enc.Utf8.parse(key);
     iv = CryptoJS.enc.Utf8.parse(ivKey);
     var options = {
       mode: CryptoJS.mode.CBC,
       padding: CryptoJS.pad.Pkcs7,
       iv: iv
     };
     var encrypted = CryptoJS.TripleDES.encrypt(token, key,  options);

    }

在Dart中,这就是我到目前为止

static String getEncrypt() {
    String key = HR_KEY;
    String id = "test";
    String message = id  + getUtcDate();
    var blockCipher = new BlockCipher(new TripleDESEngine(), key);
    var ciphertext = blockCipher.encodeB64(message);
    return ciphertext;
  }

使用上面的代码,如何传递iv,mode,padding等

谢谢

2 个答案:

答案 0 :(得分:1)

请勿使用3DES。不是因为加密所以安全!

答案 1 :(得分:0)

在我的情况下,我的客户使用ECB模式和DES.IV_ZEROS

我正在使用dart_des软件包,并通过以下代码在您的问题附近实现了解决方案:

  String key = apiObject.key;
  Uint8List data = convert.base64Decode(key);

  DES3 desECB = DES3(key: data, mode: DESMode.ECB, iv: DES.IV_ZEROS);

  String cypher = "cypher text";
  List<int> bytes = convert.utf8.encode(cypher);
  bytes = [bytes[0], 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]; // this is the secret
  // create a 8 positions byte array, and the first one is the only one that you update
  //and after this you encrypt the byte key with TripleDes
  List<int> encryptedChyper = desECB.encrypt(bytes);

  String value = convert.base64Encode(encryptedChyper);