Coder Social home page Coder Social logo

Comments (20)

leocavalcante avatar leocavalcante commented on August 17, 2024 6

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.

preetshah21699 avatar preetshah21699 commented on August 17, 2024 5

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.

leocavalcante avatar leocavalcante commented on August 17, 2024 4

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.

preetshah21699 avatar preetshah21699 commented on August 17, 2024 1

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.

alfatlaloc avatar alfatlaloc commented on August 17, 2024 1

Same issue here, version 5.0.3 just cant decrypt, thankfully the app is not in production yet

from encrypt.

leocavalcante avatar leocavalcante commented on August 17, 2024

You need the proper key and iv values to decode...

from encrypt.

chinabrant avatar chinabrant commented on August 17, 2024
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.

leocavalcante avatar leocavalcante commented on August 17, 2024

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.

chinabrant avatar chinabrant commented on August 17, 2024

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.

chinabrant avatar chinabrant commented on August 17, 2024

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.

chinabrant avatar chinabrant commented on August 17, 2024

thanks

from encrypt.

YDJSZY avatar YDJSZY commented on August 17, 2024

I have the same error, what's the reason ?

from encrypt.

KonKri avatar KonKri commented on August 17, 2024

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.

leocavalcante avatar leocavalcante commented on August 17, 2024

It is already there https://github.com/leocavalcante/encrypt#nozero-padding

from encrypt.

ismaeldmlv avatar ismaeldmlv commented on August 17, 2024

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.

oliverbytes avatar oliverbytes commented on August 17, 2024

Same issue here as well

from encrypt.

alfonsotesone avatar alfonsotesone commented on August 17, 2024

Same problem here with 5.0.3

from encrypt.

emadal95 avatar emadal95 commented on August 17, 2024

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.

ghazalcomputer avatar ghazalcomputer commented on August 17, 2024

I have the same problem
Why did you close the issue?
Please check again
There is the same problem again

from encrypt.

alfatlaloc avatar alfatlaloc commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.