AES/CBC/PKCS7 in pointycastle:
import 'dart:typed_data';
import 'dart:convert';
import "package:pointycastle/export.dart";
Uint8List encrypt(String data, String token, String cipherIV) {
var key = new SHA256Digest().process(UTF8.encode(token));
var iv = new SHA256Digest().process(UTF8.encode(cipherIV)).sublist(0, 16);
CipherParameters params = new PaddedBlockCipherParameters(new ParametersWithIV(new KeyParameter(key), iv), null);
PaddedBlockCipherImpl cipherImpl = new PaddedBlockCipherImpl(new PKCS7Padding(), new CBCBlockCipher(new AESFastEngine()));
cipherImpl.init(true, params);
return cipherImpl.process(UTF8.encode(data));
}
// Test Code:
var message = 'Hello world!';
var token = '01234567890123456789012345678901';
var cipherIV = '0123456789012345';
var result = encrypt(message, token, cipherIV);
print('result=$result');
The result with pointycastle is:
[93, 230, 77, 13, 75, 37, 207, 197, 131, 60, 244, 178, 212, 245, 233, 46]
encrypt with aes-js
AES-JS: https://github.com/ricmoo/aes-js
import AES from 'aes-js';
const encrypt = (data, token, cipherIV) => {
const key = AES.utils.utf8.toBytes(token);
const iv = AES.utils.utf8.toBytes(cipherIV);
const aesCbc = new AES.ModeOfOperation.cbc(key, iv);
const dataBytes = AES.utils.utf8.toBytes(data);
const paddedData = AES.padding.pkcs7.pad(dataBytes);
const encryptedBytes = aesCbc.encrypt(paddedData);
return encryptedBytes;
};
// Test Code:
var message = 'Hello world!';
var token = '01234567890123456789012345678901';
var cipherIV = '0123456789012345';
var result = encrypt(message, token, cipherIV);
console.log('result: ' + result);
The result with aes-js is:
[200, 168, 149, 245, 48, 112, 221, 227, 109, 217, 11, 202, 214, 71, 87, 107]
encrypt with nodejs
Crypto in Nodejs: https://nodejs.org/api/crypto.html
import crypto from 'crypto';
const encrypt = (data, token, cipherIV) => {
const cipher = crypto.createCipheriv('aes-256-cbc', token, cipherIV);
let crypted = cipher.update(data, 'utf8', 'binary');
crypted += cipher.final('binary');
crypted = new Buffer(crypted, 'binary');
return crypted;
};
// Test Code:
var message = 'Hello world!';
var token = '01234567890123456789012345678901';
var cipherIV = '0123456789012345';
var result = encrypt(message, token, cipherIV);
console.log('result: ' + result);
The result with nodejs:
[200, 168, 149, 245, 48, 112, 221, 227, 109, 217, 11, 202, 214, 71, 87, 107]
It is same with aes-js.
AES/CBC/PKCS7 in pointycastle:
The result with pointycastle is:
encrypt with aes-js
AES-JS: https://github.com/ricmoo/aes-js
The result with aes-js is:
encrypt with nodejs
Crypto in Nodejs: https://nodejs.org/api/crypto.html
The result with nodejs:
It is same with aes-js.