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 when trying to use 64 digit key #1

Closed
lasergoat opened this issue Jan 8, 2020 · 3 comments
Closed

Exception when trying to use 64 digit key #1

lasergoat opened this issue Jan 8, 2020 · 3 comments

Comments

@lasergoat
Copy link

lasergoat commented Jan 8, 2020

FIrst off, thank you for making this repo!

node -v
v10.16.0

Using this code:

    const { Encryptor } = require("node-laravel-encryptor");
    const key = Encryptor.generateRandomKey(64, "base64");

    let encryptor = new Encryptor({
      key: key,
      key_length: 64
    });

    const result = encryptor.encryptSync("TEXTTOENCRYPT");
    console.log(result);

Exception is thrown:

     EncryptorError: Invalid key length
      at Function.throwError (node_modules/node-laravel-encryptor/dist/base_encryptor.js:256:15)
      at Encryptor.createCipher (node_modules/node-laravel-encryptor/dist/base_encryptor.js:144:28)
      at Encryptor.encryptItSync (node_modules/node-laravel-encryptor/dist/base_encryptor.js:35:29)
      at Encryptor.encryptSync (node_modules/node-laravel-encryptor/dist/Encryptor.js:21:50)

Yet, if I make a key of 32 and don't pass a key_length into the Encryptor constructor, it works. It's like the cipher algorithm isn't being set properly when the key is 64 digits.

@lasergoat
Copy link
Author

When I use a 32 bit Key, it encrypts, but I am unable to make the encryptor instance in laravel:

    const originalPassword = "98b29b3b";

    const { Encryptor } = require("node-laravel-encryptor");
    const key = Encryptor.generateRandomKey(32);

    // copy this key to paste into laravel
    console.log("key:", key.toString("base64"));

    let encryptor = new Encryptor({
      key
    });

    const result = encryptor.encryptSync(originalPassword);
    console.log("base_encryptor.js:", result);

Now, take that key, and the encryption and use in artisan tinker for decryption:

// run php artisan tinker

$newEncrypter = new \Illuminate\Encryption\Encrypter( base64_decode('SrA+WaJ9CZilLJ51WWdQl/nor/6xMJ779yhi4JK43bk='), 'AES-128-CBC' );

// ERROR: RuntimeException with message 'The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.'

Screen Shot 2020-01-09 at 9 33 48 AM

@AdSegura
Copy link
Owner

Hi.

Sorry for the delay answering , right now I'm not able to dig into it but I'll look this issue more carefully when I finish to settle down.

@AdSegura
Copy link
Owner

AdSegura commented Feb 3, 2020

FIrst off, thank you for making this repo!

node -v
v10.16.0

Using this code:

    const { Encryptor } = require("node-laravel-encryptor");
    const key = Encryptor.generateRandomKey(64, "base64");

    let encryptor = new Encryptor({
      key: key,
      key_length: 64
    });

    const result = encryptor.encryptSync("TEXTTOENCRYPT");
    console.log(result);

Exception is thrown:

     EncryptorError: Invalid key length
      at Function.throwError (node_modules/node-laravel-encryptor/dist/base_encryptor.js:256:15)
      at Encryptor.createCipher (node_modules/node-laravel-encryptor/dist/base_encryptor.js:144:28)
      at Encryptor.encryptItSync (node_modules/node-laravel-encryptor/dist/base_encryptor.js:35:29)
      at Encryptor.encryptSync (node_modules/node-laravel-encryptor/dist/Encryptor.js:21:50)

Yet, if I make a key of 32 and don't pass a key_length into the Encryptor constructor, it works. It's like the cipher algorithm isn't being set properly when the key is 64 digits.

I just published an update.

Now options.key_length should be 128 or 256 to reflect what Laravel is ussing, aes-128-cbc or aes-256-cbc

generateRandomKey too:

const key = Encryptor.generateRandomKey();
const key = Encryptor.generateRandomKey(128);

Thanks for the advise and let me know if you have any problem.

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

2 participants