ダートでcipher.doFinal()メソッドのために必要なパラメータを埋めるためにどのように?

オマール・フセイン:

私は自分のフラッタアプリケーション使用して暗号化をやろうとしているpointy_castleに似ているパッケージ、crypto.CipherJavaでライブラリ。呼ばれる方法があるdoFinal()Javaでは、あなたは一つのパラメータを割り当てることができますが、。ダートでの間に、次の4つのパラメータを割り当てる必要があります。だから、どのように私は適切に必要なパラメータを埋めることができますか?私はこれを行う方法の一例を必要としています。

パッケージのドキュメントには、 doFinal(Uint8List inp, int inpOff, Uint8List out, int outOff) → int

これは、Javaのコードです:

 ...
    byte[] encrypted;
    encrypted = cipher.doFinal(padString(text).getBytes());
    String finalData = bytesToHex(encrypted);
    return finalData;
 ...

そして、ダートで:

...
    Uint8List encrypted; // <- I have to make it of type Uint8List because _bytesToHex method requires looping through the list. However, it produces an error because of that: `A value of type 'int' can't be assigned to a variable of type 'Uint8List'.`
    encrypted = cipher.doFinal(utf8.encode(_padString(data))); // <- This produces an error since doFinal() requires 4 params.
    String finalData = _bytesToHex(encrypted);
    return finalData;
...
オマール・フセイン:

場合には誰もがこのケースが発生しました。これは私がなってしまったものです:

import 'package:pointycastle/export.dart';

class Example {
  Uint8List encrypt(String data) {
    if (data == null || data.length == 0) return null;
    final Uint8List _key = utf8.encode('key');
    final Uint8List _iv = utf8.encode('iv');
    try {
      Uint8List encrypted;
      final CipherParameters params = PaddedBlockCipherParameters(
        ParametersWithIV(
          KeyParameter(_key),
          _iv,
        ),
        null,
      );
      final PaddedBlockCipherImpl cipher = PaddedBlockCipherImpl(
        PKCS7Padding(),
        CBCBlockCipher(
          AESFastEngine(),
        ),
      );
      cipher.init(true, params);
      encrypted = cipher.process(utf8.encode(data));
      return encrypted;
    } catch (_) {
      print(_);
      return null;
    }
  }

  String decrypt(Uint8List data) {
    if (data == null || data.length == 0) return null;
    final Uint8List _key = utf8.encode('key');
    final Uint8List _iv = utf8.encode('iv');
    try {
      final CipherParameters params = PaddedBlockCipherParameters(
        ParametersWithIV(
          KeyParameter(_key),
          _iv,
        ),
        null,
      );
      final PaddedBlockCipherImpl cipher = PaddedBlockCipherImpl(
        PKCS7Padding(),
        CBCBlockCipher(
          AESFastEngine(),
        ),
      );
      cipher.init(false, params);
      final String finalData = utf8.decode(cipher.process(data));
      return finalData;
    } catch (_) {
      print(_);
      return null;
    }
  }
}

おかげhttps://stackoverflow.com/users/9014097/topacohttps://github.com/Nguyenpk57インスピレーションを得るため。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=219359&siteId=1