Comments (20)
Sorry the delay.
Added the option to use AES without padding, just give a null
value to it:
Encrypter(AES(key, iv, mode: AESMode.ecb, padding: null))
Does it helps?
from encrypt.
I understand that there is a fix. But the fix isn't backwards compatible. How is someone supposed to use an encryption tool/package if there is no guaratee that they will be able to decrypt the data in the future. I understand that there must have been a bug fix or something that required you to make this change. But the basic functioning of the package should remaing the same and it should be backwards compatible.
from encrypt.
Hi, folks
Please, just replace IV.fromLength(16)
by IV.allZerosOfLength(16)
.
As saw in #314
Remember that it is not cryptography safe
from encrypt.
I am facing this issue right now with the version 5.0.3.
However, when I was using version 5.0.1, it didn't have this issue. So, now I have data already encrypted and stored on the server with the defaault padding values.
I believe, the default mode was sic and padding PKCS7.
aes1Key = Key.fromUtf8(aes1Text);
aesKey = Key.fromUtf8(aesText);
aes1Encrypter = Encrypter(AES(aes1Key));
aesEncrypter = Encrypter(AES(aesKey));
If I use null for padding, it works. But it cannot decrypt my old data properly that was encrypted using the older library.
from encrypt.
Same issue here, version 5.0.3 just cant decrypt, thankfully the app is not in production yet
from encrypt.
You need the proper key and iv values to decode...
from encrypt.
static List<int> aes128Encode(String params) {
final key = Key.fromUtf8('15helloTCJTALK20');
final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(key, iv, mode: AESMode.ecb));
final encrypted = encrypter.encrypt(params);
final result = encrypter.decrypt(encrypted);
print('params: $params');
print('result: $result');
return encrypted.bytes;
}
static List<int> aes128Decode(List<int> data) {
final key = Key.fromUtf8('15helloTCJTALK20');
final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(key, iv, mode: AESMode.ecb));
final encrypted = Encrypted(Uint8List.fromList(data));
return encrypter.decrypt(encrypted).runes.toList();
}
Use aes128Encode function, i can get the right answer.
AES(this.key, this.iv, {this.mode = AESMode.sic})
: _cipher = PaddedBlockCipher('AES/${_modes[mode]}/PKCS7'),
_params = PaddedBlockCipherParameters(
mode == AESMode.ecb
? KeyParameter(key.bytes)
: ParametersWithIV<KeyParameter>(
KeyParameter(key.bytes), iv.bytes),
null,
);
Look at aes class define, when the mode is AESMode.ecb, the iv parameter is no use.
from encrypt.
Your code looks fine here:
import 'package:encrypt/encrypt.dart';
import 'dart:typed_data';
class Encoder {
static List<int> aes128Encode(String params) {
final key = Key.fromUtf8('15helloTCJTALK20');
final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(key, iv, mode: AESMode.ecb));
final encrypted = encrypter.encrypt(params);
final result = encrypter.decrypt(encrypted);
print('params: $params');
print('result: $result');
return encrypted.bytes;
}
static List<int> aes128Decode(List<int> data) {
final key = Key.fromUtf8('15helloTCJTALK20');
final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(key, iv, mode: AESMode.ecb));
final encrypted = Encrypted(Uint8List.fromList(data));
return encrypter.decrypt(encrypted).runes.toList();
}
}
void main() {
final text = 'Lorem Ipsum';
final encoded = Encoder.aes128Encode(text);
print(encoded);
final decoded = Encoder.aes128Decode(encoded);
print(decoded);
}
encrypt_issu30~ dart .\bin\main.dart
params: Lorem Ipsum
result: Lorem Ipsum
[193, 56, 172, 130, 112, 33, 0, 154, 30, 140, 46, 16, 54, 73, 41, 239]
[76, 111, 114, 101, 109, 32, 73, 112, 115, 117, 109]
I'm missing something?
from encrypt.
I know why, the server data don't need the pkcs7padding to decrypt. So what should i do.
PaddedBlockCipher('AES/${_modes[mode]}/PKCS7'),
from encrypt.
This is the encrypt function at client.
- (NSData *)AES128EncryptWithKey:(NSString *)key {
// 'key' should be 128 bytes for AES128, will be null-padded otherwise
char keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding + kCCOptionECBMode,
keyPtr, kCCKeySizeAES128,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
This is the decrypt function.
- (NSData *)AES128DecryptHttpWithKey: (NSString *)key {
// 'key' should be 16 bytes for AES128, will be null-padded otherwise
char keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionECBMode,
keyPtr, kCCKeySizeAES128,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer); //free the buffer;
return nil;
}
In the two function, the third parameter is different at CCCrypt
invoke.
from encrypt.
thanks
from encrypt.
I have the same error, what's the reason ?
from encrypt.
It worked, I was working on a message I had received encrypted with AES 128 ECB, and I had the same issue! Can you add it to the documentation?. Thanks anyways!!
from encrypt.
It is already there https://github.com/leocavalcante/encrypt#nozero-padding
from encrypt.
I am facing this issue right now with the version 5.0.3.
However, when I was using version 5.0.1, it didn't have this issue. So, now I have data already encrypted and stored on the server with the defaault padding values.
I believe, the default mode was sic and padding PKCS7.
aes1Key = Key.fromUtf8(aes1Text); aesKey = Key.fromUtf8(aesText); aes1Encrypter = Encrypter(AES(aes1Key)); aesEncrypter = Encrypter(AES(aesKey));If I use null for padding, it works. But it cannot decrypt my old data properly that was encrypted using the older library.
same bug on update to 5.0.3...
i fixed changing:
final iv = IV.fromLength(16);
to:
final iv = IV.fromUtf8('KeyWith16CharsHere');
but of course it not work for decrypt data encrypted by version 5.0.1 because we change the IV.
from encrypt.
Same issue here as well
from encrypt.
Same problem here with 5.0.3
from encrypt.
I am having the same problem. Data already encrypted is not decrypting anymore. This is a huge issue, is anything being done to fix this?
UPDATE:
for anyone with this problem still and not able to decrypt old data, the old IV string created with IV.fromLength() (in base64 below) was AAAAAAAAAAAAAAAAAAAAAA==
substitute your IV.fromLength(16) with IV.fromBase64('AAAAAAAAAAAAAAAAAAAAAA=='). this should let you at least decrypt the old password so you can now adjust to the new package encryption
from encrypt.
I have the same problem
Why did you close the issue?
Please check again
There is the same problem again
from encrypt.
Hey, i found a solution. I don´t know how good it is but . . .
My use case is that i need new key and IV every login, except for some cases but almost every time is like that.
I made a function that creates a random String from the characters bellow.
String _generateRandom(int length) {
const String allowedChars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
final random = Random();
final buffer = StringBuffer();
for (var i = 0; i < length; i++) {
final charIndex = random.nextInt(allowedChars.length);
buffer.write(allowedChars[charIndex]);
}
return buffer.toString();
}
And i just use the generated values
final encrypter =
Encrypter(AES(Key.fromUtf8(secret.key)));
final encrypted = encrypter.encrypt(original, iv: IV.fromUtf8(secret.iv));
from encrypt.
Related Issues (20)
- I can't decrypt the text in 5.0.3 😕 HOT 11
- Output double in size, AES in CTR
- After RSA public key encryption, I am unable to decrypt it. It shows an error message: 'Error: Expected a value of type 'ASN1Integer', but got one of type 'ASN1Sequence'.
- Getting Private Key Issue HOT 1
- Invalid argument(s): Initialization vector must be the same length as block size
- You can add an onProgress
- Invalid argument(s): Invalid or corrupted pad block HOT 6
- Different encrypted output
- Encryption between platforms
- Web release Invalid or corrupted pad block HOT 3
- '(x >= 0) && (x <= _MASK_32)': is not true
- Exception has occurred. ArgumentError (Invalid argument(s): Invalid or corrupted pad block) HOT 11
- About Privacy Manifest HOT 1
- RSAKeyParser is not defined in poiintycastle
- [v5.0.3][Web] decrypt incorrect HOT 6
- crypto dependency issue
- Insecure Random on ios
- Invalid argument(s): Invalid or corrupted pad block HOT 10
- (mistake) `Key.stretch` should be a factory constructor HOT 1
- OpenSSL's EVP envelope HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from encrypt.