Closed proteye closed 6 years ago
This code is work:
// AES-256 (CFB) encryption
static String cfbEncrypt(String password, String plaintext) {
Uint8List derivedKey = deriveKey(password);
KeyParameter keyParam = new KeyParameter(derivedKey);
BlockCipher aes = new AESFastEngine();
var rnd = FortunaRandom();
rnd.seed(keyParam);
Uint8List iv = rnd.nextBytes(16);
ParametersWithIV cfbParams = new ParametersWithIV(keyParam, iv);
CFBBlockCipher cfb = new CFBBlockCipher(aes, aes.blockSize);
cfb.init(true, cfbParams);
Uint8List textBytes = createUint8ListFromString(plaintext);
Uint8List paddedText = pad(textBytes, aes.blockSize);
Uint8List cipherBytes = _processBlocks(cfb, paddedText);
Uint8List cipherIvBytes = new Uint8List(cipherBytes.length + iv.length)
..setAll(0, iv)
..setAll(iv.length, cipherBytes);
return formatBytesAsHexString(cipherIvBytes);
}
// AES-256 (CFB) decryption
static String cfbDecrypt(String password, String ciphertext) {
Uint8List derivedKey = deriveKey(password);
KeyParameter keyParam = new KeyParameter(derivedKey);
BlockCipher aes = new AESFastEngine();
Uint8List cipherIvBytes = createUint8ListFromHexString(ciphertext);
Uint8List iv = new Uint8List(aes.blockSize)
..setRange(0, aes.blockSize, cipherIvBytes);
ParametersWithIV cfbParams = new ParametersWithIV(keyParam, iv);
CFBBlockCipher cfb = new CFBBlockCipher(aes, aes.blockSize);
cfb.init(false, cfbParams);
int cipherLen = cipherIvBytes.length - aes.blockSize;
Uint8List cipherBytes = new Uint8List(cipherLen)
..setRange(0, cipherLen, cipherIvBytes, aes.blockSize);
Uint8List paddedText = _processBlocks(cfb, cipherBytes);
Uint8List textBytes = unpad(paddedText);
return new String.fromCharCodes(textBytes);
}
static Uint8List _processBlocks(BlockCipher cipher, Uint8List inp) {
var out = new Uint8List(inp.lengthInBytes);
for (var offset = 0; offset < inp.lengthInBytes;) {
var len = cipher.processBlock(inp, offset, out, offset);
offset += len;
}
return out;
}
How to implement AES with CFB-128 mode?
My code:
gives the result