Home > Software design >  Has anyone worked with Rijndael encryption on Flutter? How to encrypt with Rijndael?
Has anyone worked with Rijndael encryption on Flutter? How to encrypt with Rijndael?

Time:06-14

I am using encrypt package flutter. I write code bellow for encrypt data. But i could not find expected result.

  String encrypterAESdata({String? data}){ 
    final key = Key.fromUtf8(aesKey);
    final iv = IV.fromUtf8(aesKey);
    final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: "PKCS7" ));
    final encrypted = encrypter.encrypt(data!, iv: iv);
    return encrypted.base64;
  }

Test Data:

{
  "remittanceNoPrefix": "IT",
  "adjustedDCAmount": 0,
  "beneAccNo": "01912163589",
  "benePhone": "01912163589",
  "beneAccTypeID": 199,
  "beneAmount": "4900",
  "beneBankName": "BKASH (MOBILE ACCOUNT)",
  "beneBankBranchID": 31637,
  "beneBankID": 1135,
  "beneBranchName": "BKASH (ANY AGENT POINT)",
  "beneBrnAddress": "ANYWHERE",
  "beneCountryID": 23,
  "beneCurrencyID": 15,
  "beneDocID": null,
  "beneDocNo": "",
  "beneficiaryID": 497656,
  "beneficiarySurname": "TEST",
  "beneficiaryName": "TEST",
  "relationID": null,
  "benePayeeModeID": 11,
  "subCompanyBranchID": 151886,
  "companyID": 76,
  "subCompanyID": 367,
  "debitCardID": null,
  "discountedComm": 0,
  "encashedPoint": 0,
  "encashedPointAmount": 0,
  "equiAmount": 50,
  "ofrdAmount": 50,
  "equiCommission": 3,
  "ofrdCommission": 3,
  "taxAmount": 0,
  "cardCharge": 0,
  "instrumentDetail": "",
  "instrumentTypeID": "120",
  "maturityDate": "2019-02-14T00:00:00.000Z",
  "issueDate": "2019-02-14T00:00:00.000Z",
  "othrRcvdAmount": 0,
  "othrRcvdCommission": 0,
  "pDRate": 98,
  "purposeDetail": "FAMILY HELP",
  "purposeID": 2,
  "aMLRemBhvClsID": null,
  "aMLRemBhvClsValue": 0,
  "aMLOpVoteClsID": null,
  "aMLOpVoteClsValue": 0,
  "aMLCoopClsID": null,
  "aMLCoopClsValue": 0,
  "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PfadQ1OLMRS84FKhv3xfXP20hca/mMDXWzVOfhMDhPQRx0txL6Qh6AqS984UgM6CTcWIGL7UC7UMDtEYrcsLOw0y7LR0vD0acH d4NESC7AzyDaN4//otzaYyOeNvYCraDWx0DpPyJCQfQSQKctzELHWgcb8hHgMup42ewu4 cMml42eTwVjrbKAYcSTYeimb0aVa9LlObOoUBFBrudzhhyXueYd1 RK83k8BuCyxo9jsk7oDlsngm4j8HdOvp2TJm4gFs1wTDR4lQmuqVNMdorJT6iO7f/tEuOMTb6lXueM7XWDlQ9vbUYDlG8py9K3Tfd6cPny2L7uzdCk2OnnmEpYmVM3nzhgY1wYz/TwS3sWAYriRDJ1WhXKoCWC7iXn rKq1QwctxatFiz14zYtU41ucXFM0uiU76jPYQ8q3UsGHMvoL7pbAii4qW05QEcDuGjI/kJgtTf8KWZz tk7ii3nnh nrh//1hiC8sC1GJ7/PIdvPj4yXWv0wG8XONfl43MIRTD1n43ePkNQaYyeGFmcZc BJjPTZ6Z7KZjACbaeS8YZlAE8EAOVGNzq4oZFpFt/ VJcMIa17LtCrjTqcDuDFggjO633Z3ISLnAZJeYoaXtFWaehdZVCimTh0xCnON63jlmyrZoL38cCqFfBiEcJ4O04Pfqob6FEhgTEk4vhQLV7n/O4LhsR9yyfnipY2IrZHVOIGYDJfnw3D4NrpKjBRxq2q/ihTLYzQCpmyXaFVNmgIsv37fdgk2TYZ6leyVs jDdUwogluqzbTEGOOqK4ZqvLgntEYEFjGEP4Ztjwu48BuLvsT2vH/zuKMW5L0WtZYn6Ytq2sFDJ2aoFSWvUwnz4w5lrrD1dmOOJJGpnn3aNAWcwx5DZk7tCFOp0D6zGtajLM7pMA==

Key:

Nec_uK_2@!6_CebD

IV:

Nec_uK_2@!6_CebD

CodePudding user response:

I never find the 'wrapper' packages around pointcastle useful. They just add layers of obfuscation in the name of adding 'simplicity'. It's often easier to just use the underlying package directly.

import 'dart:convert';
import 'dart:typed_data';

import 'package:pointycastle/export.dart';

void main() {
  final k = 'Nec_uK_2@!6_CebD';
  final key = utf8.encode(k) as Uint8List;
  final iv = key;

  print(key);

  final cipher = PaddedBlockCipherImpl(
    PKCS7Padding(),
    CBCBlockCipher(AESEngine()),
  )..init(
      true,
      PaddedBlockCipherParameters(
        ParametersWithIV(KeyParameter(key), iv),
        null,
      ),
    );

  final data = utf8.encode(pt) as Uint8List;
  final ct = cipher.process(data);

  print(data.length);
  print(ct.length);
  print(base64.encode(ct));
}

final pt = '''{
  "remittanceNoPrefix": "IT",
  "adjustedDCAmount": 0,
  ...
  "cdExp": "",
  "cdSecCode": ""
}''';

CodePudding user response:

easy peasy

String s = "i want to encrypt this init";

String key = 'AA­èìýUn}a…ópŒ¹%J'yL4ßK,£½';

List<int> listint= utf8.encode(s);

Uint8List listUint= Uint8List.fromList(key.codeUnits);


AesGcm algorithm = AesGcm.with256bits();

SecretKey secretKey = await algorithm.newSecretKeyFromBytes(listUint);


    SecretBox secretBox = await algorithm.encrypt(
      listint,
      secretKey: secretKey,
    );

    print('Nonce: ${secretBox.nonce}');
    print('Ciphertext: ${secretBox.cipherText}');
    print('MAC: ${secretBox.mac.bytes}');


    Uint8List listEncryptedUint = secretBox.concatenation();
    print('this is the encrypted payload init: ' listEncryptedUint .toString());
  • Related