encrypt icon indicating copy to clipboard operation
encrypt copied to clipboard

Exception has occurred. ArgumentError (Invalid argument(s): Invalid or corrupted pad block)

Open miraj98hossain opened this issue 2 years ago • 12 comments

I am trying to implement this package. this is my EncryptService

import 'package:encrypt/encrypt.dart';

class EncryptService {
  final _key = Key.fromUtf8('XXym1y2s3o9ftyZZ');
  final _iv = IV.fromLength(16);

  String encrypt({required String plainText}) {
    final encryptService = Encrypter(AES(_key));
    Encrypted encryptedData = encryptService.encrypt(plainText, iv: _iv);
    return encryptedData.base64;
  }

  String decrypt({required String encryptedText}) {
    Encrypted encryptedData = Encrypted.fromBase64(encryptedText);
    final encryptService = Encrypter(AES(_key));
    return encryptService.decrypt(encryptedData, iv: _iv);
  }
}

using this class i am encrypt the passwords and storing into the database.

while login i am trying to decrypting the password but it is showing

ArgumentError (Invalid argument(s): Invalid or corrupted pad block)

miraj98hossain avatar Mar 05 '24 06:03 miraj98hossain

Same issue i am facing

YashSavsani avatar Mar 07 '24 07:03 YashSavsani

@YashSavsani use this. this issue happens because of final _iv = IV.fromLength(16); here IV.fromLength(16); creates random number everytime. final _key = Key.fromUtf8('XXym1y2s3o9ftyZZ'); final _iv = IV.fromUtf8("XXym1y2s3o9ftyZZ");

miraj98hossain avatar Mar 07 '24 08:03 miraj98hossain

@miraj98hossain
Okay, but will it decrypt the previously encrypted data with IV.fromLength(16); ?

No, right!

Which version are you using?

YashSavsani avatar Mar 07 '24 08:03 YashSavsani

no it won't decrypt because iv is different.

miraj98hossain avatar Mar 07 '24 10:03 miraj98hossain

@YashSavsani use this. this issue happens because of final _iv = IV.fromLength(16); here IV.fromLength(16); creates random number everytime. final _key = Key.fromUtf8('XXym1y2s3o9ftyZZ'); final _iv = IV.fromUtf8("XXym1y2s3o9ftyZZ");

I'm doing the same but still getting the error Screenshot 2024-03-16 at 12 30 16 PM

Prashant4900 avatar Mar 16 '24 07:03 Prashant4900

@Prashant4900 don't use base64.decode() rather try using Encrypted encryptedData = Encrypted.fromBase64(encryptedText); maybe this is causing the issue. :)

miraj98hossain avatar Mar 16 '24 07:03 miraj98hossain

@Prashant4900 don't use base64.decode() rather try using Encrypted encryptedData = Encrypted.fromBase64(encryptedText); maybe this is causing the issue. :)

Still facing the same problem

Prashant4900 avatar Mar 16 '24 08:03 Prashant4900

@Prashant4900 can you paste the full code ??

miraj98hossain avatar Mar 16 '24 08:03 miraj98hossain

@Prashant4900 can you paste the full code ??

encrypter file

import 'dart:developer';

import 'package:encrypt/encrypt.dart' as ec;

// Keys for encryption/decryption
final key = ec.Key.fromUtf8('37911490979715163134003223491201');
// final secondKey = encrypt.Key.fromUtf8('54674138327930866480207815084989');
final iv = ec.IV.fromUtf8('37911490979715163134003223491201');
// final iv = encrypt.IV.fromLength(16);
// final iv = ec.IV.allZerosOfLength(16);
// final iv = ec.IV(Uint8List(16));

final encrypter = ec.Encrypter(ec.AES(key));

String encryptAES(String plaintext) {
  log('object1');
  final encrypted = encrypter.encrypt(plaintext, iv: iv);
  log('object2');
  return encrypted.base64;
}

String decryptAES(String ciphertext) {
  log('object-1');
  final decrypted =
      encrypter.decrypt(ec.Encrypted.fromBase64(ciphertext), iv: iv);
  log('object-2');
  return decrypted;
}

external file

  if (iframeResponse.statusCode == 200) {
          final scriptElement = html_parser
              .parse(iframeResponse.body)
              .querySelector('script[data-name="episode"]');

          if (scriptElement != null) {
            final scriptDataValue = scriptElement.attributes['data-value'];
            final id = iframeUri.queryParameters['id']!;
            // print(0);
            final url =
                'https://embtaku.pro/encrypt-ajax.php?id=${encryptAES(id)}&alias=$id&${decryptAES(scriptDataValue!)}';
            // print('url: $url');

            final res = await http.get(
              Uri.parse(url),
              headers: {
                'User-Agent': USER_AGENT,
                'X-Requested-With': 'XMLHttpRequest',
              },
            );

            log('res.body: ${res.body}');
          }
        }

Prashant4900 avatar Mar 16 '24 08:03 Prashant4900

@Prashant4900 I found this error saying Key length not 128/192/256 bits and iv length should be in range range 0..16 try using below code.. it is working fine....

import 'package:encrypt/encrypt.dart' as ec;

void main() {
  print(EncryptService.encryptAES("test"));
  print(EncryptService.decryptAES(EncryptService.encryptAES("test")));
}
class EncryptService {
  static final key = ec.Key.fromUtf8('XXyyZZxxxxxxxxxx');
  static final iv = ec.IV.fromUtf8('XXyyZZxxxxxxxxxx');

  static final encrypter = ec.Encrypter(ec.AES(key));

  static String encryptAES(String plaintext) {
    final encrypted = encrypter.encrypt(plaintext, iv: iv);
    return encrypted.base64;
  }

  static String decryptAES(String ciphertext) {
    final decrypted =
        encrypter.decrypt(ec.Encrypted.fromBase64(ciphertext), iv: iv);

    return decrypted;
  }
}

image

miraj98hossain avatar Mar 16 '24 09:03 miraj98hossain

I believe the issue arises from using the incorrect key value. I attempted to convert this JavaScript code to Dart, and I assumed I needed to use the same key they were using. However, when I used that key, it caused the problem. Additionally, if both algorithms are the same, shouldn't they work with the same key?

Prashant4900 avatar Mar 16 '24 09:03 Prashant4900