Closed catouse closed 5 years ago
DId you take a look at this example? https://github.com/PointyCastle/pointycastle/pull/111
I had the same problem
Do not use SHA-256 encryption
import 'dart:typed_data';
import 'dart:convert';
import "package:pointycastle/export.dart";
Uint8List encrypt(String data, String token, String cipherIV) {
var key = utf8.encode(token);
var iv = utf8.encode(cipherIV);
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:
[200, 168, 149, 245, 48, 112, 221, 227, 109, 217, 11, 202, 214, 71, 87, 107]
@catouse
@raodaqi Thanks.
import 'dart:typed_data';
import 'dart:convert';
import "package:pointycastle/export.dart";
Uint8List encrypt(String data, String token, String cipherIV) {
var key = utf8.encode(token);
var iv = utf8.encode(cipherIV);
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');
i have used the above code to encrypt on js But does it seem that AES.js cannot be decrypted? because there is no unpad method I see above you have to add the pad Do you have any way? @raodaqi
import 'dart:convert';
import 'dart:typed_data';
import "package:pointycastle/export.dart";
Uint8List encrypt(String data, String token, String cipherIV) {
var key = utf8.encode(token);
var iv = utf8.encode(cipherIV);
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));
}
String decrypt(Uint8List data, String token, String cipherIV) {
var key = utf8.encode(token);
var iv = utf8.encode(cipherIV);
CipherParameters params = new PaddedBlockCipherParameters(
new ParametersWithIV(new KeyParameter(key), iv), null);
PaddedBlockCipherImpl cipherImpl = new PaddedBlockCipherImpl(new PKCS7Padding(), new CBCBlockCipher(new AESFastEngine()));
cipherImpl.init(false, params);
return utf8.decode(cipherImpl.process(data));
}
// Test Code:
void main() {
var message = 'Ngoc create a chatroom. 👻 👻 김진학테스트 tiếng việt';
var token = '20180531100733_userUid02_15277360532520000'.substring(0, 32);
var cipherIV = '20180531100733_userUid02_15277360532520000'.substring(0, 16);
var result = encrypt(message, token, cipherIV);
print(result);
String resultBase64 = base64.encode(result);
print(resultBase64);
print(decrypt(base64.decode(resultBase64), token, cipherIV));
}
Here my code on the flutter dart I used to decode and encode. It's okie
const decryptByAES = (message, keyRSA) => { var cipherIV = keyRSA.substring(0,16); keyRSA = keyRSA.substring(0,32);
const key = aesjs.utils.utf8.toBytes(keyRSA);
const iv = aesjs.utils.utf8.toBytes(cipherIV);
const aesCbc = new aesjs.ModeOfOperation.cbc(key, iv);
var decryptedBytes = aesCbc.decrypt(aesjs.utils.hex.toBytes(message));
const unpadData = aesjs.padding.pkcs7.strip(decryptedBytes);
return aesjs.utils.utf8.fromBytes(unpadData);
};
here my code decrypt use AES
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.