【问题标题】:How to implement encryption (3des) in flutter/darts如何在颤振/飞镖中实现加密(3des)
【发布时间】:2019-07-02 17:36:54
【问题描述】:

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

我下载了这个库 (https://pub.dev/packages/tripledes) 但是,我无法添加所需的值。例如,你如何传递 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 等

谢谢

【问题讨论】:

  • 找到解决方案了吗?
  • 不..我在节点/后端实现它,然后从我的应用程序中调用它..

标签: flutter dart


【解决方案1】:

不要使用 3DES。不是因为被加密了才安全!

【讨论】:

  • 欢迎来到 StackOverflow。写答案时,您可能想解释为什么您认为您的答案是正确的,因为它可能不是针对完全相同的问题,并且解释可能有助于了解原因
  • 3des 仅以通常的方式加密一个 8 字节的数组。第一个位置是您要加密的值,其余 7 个位置是随机值。下面的答案进一步解释了该技术的工作原理。这不安全,只是味道不好。
【解决方案2】:

在我的情况下,我的客户使用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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 2022-07-27
    • 2020-11-05
    • 2020-11-28
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多