Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
miraj98hossain opened this issue Mar 5, 2024 · 11 comments

Comments

@miraj98hossain
Copy link

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)

@YashSavsani
Copy link

Same issue i am facing

@miraj98hossain
Copy link
Author

@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");

@YashSavsani
Copy link

YashSavsani commented Mar 7, 2024

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

No, right!

Which version are you using?

@miraj98hossain
Copy link
Author

no it won't decrypt because iv is different.

@Prashant4900
Copy link

@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

@miraj98hossain
Copy link
Author

miraj98hossain commented Mar 16, 2024

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

@Prashant4900
Copy link

@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

@miraj98hossain
Copy link
Author

@Prashant4900 can you paste the full code ??

@Prashant4900
Copy link

@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}');
          }
        }

@miraj98hossain
Copy link
Author

miraj98hossain commented Mar 16, 2024

@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

@Prashant4900
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants