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

"Key length is not legal" when using a binary secret key #45

Open
sovattha opened this issue Jan 7, 2016 · 5 comments
Open

"Key length is not legal" when using a binary secret key #45

sovattha opened this issue Jan 7, 2016 · 5 comments

Comments

@sovattha
Copy link

sovattha commented Jan 7, 2016

Hello,

I would like to encrypt a string using the algorithm 'rijndael-128' in 'ecb' mode.

My key is in binary format.

I create my key with the following code.

      var sha256Encrypt = function(key) {
        var cipher = crypto.createHash('sha256');
        return cipher.update(key, 'utf8').digest('binary');
      };

Then I would like to encrypt my message with the key using:

      var aes256Encrypt2 = function(message, secret) {
        var MCrypt = require('mcrypt').MCrypt;
        var rijEcb = new MCrypt('rijndael-128', 'ecb');
        rijEcb.validateKeySize(false);
        rijEcb.open(secret);
        return cipherText = rijEcb.encrypt(message).toString('base64');
      };

secret.length is 32. But I get the message:
Key length is not legal.

If I leave the key validation size, I get:
Invalid key size. Available key size are [16, 24, 32]

I know the key should be either a string or a buffer. I did convert my key into a buffer:

          var buf = new Buffer(32);
          buf.write(key);

It works, but the result is not the one expected.

Would you have any suggestion?

@sovattha sovattha changed the title Key length is not legal "Key length is not legal" when using a binary secret key Jan 11, 2016
@seeekr
Copy link

seeekr commented Mar 27, 2016

here's the example from the README:

var mc = new MCrypt('blowfish', 'ecb');
mc.validateKeySize(false); // disable key size checking
mc.open('typeconfig.sys^_-');

Just disable key size checking. It worked for me! I know very little about crypto, so I don't know what this implies, but since it seems to work on the surface...

@tugrul
Copy link
Owner

tugrul commented Mar 28, 2016

@sovattha is your key contains any null byte \x0?

@sovattha
Copy link
Author

sovattha commented Apr 6, 2016

@tugrul Hi, no my key does not contain any null byte.

I created a test here. Just click Run.
Or you can execute locally the code below:

 var crypto = require('crypto');

/**
 * Create a BINARY Key
 */
 var sha256Encrypt = function(key, format) {
   var cipher = crypto.createHash('sha256');
   return cipher.update(key, 'utf8').digest(format);
 };

/**
 * Encrypt the given message using the given binary key
 */
 var aes256Encrypt2 = function(message, secret) {
   var MCrypt = require('mcrypt').MCrypt;
   var rijEcb = new MCrypt('rijndael-128', 'ecb');
   rijEcb.validateKeySize(true);
   rijEcb.open(secret); // Get "TypeError: Invalid key size. Available key size are [16, 24, 32]"
   var cipherText = rijEcb.encrypt(message).toString('base64');
   return cipherText;
 };

// Create my key
 var sha256KeyAsString = sha256Encrypt('gKSfthqC5W5shXpfVujf', 'string');
 console.log('secret is: ', sha256KeyAsString); // Result is <Buffer 7b 37 2b 64 37 92 c8 81 84 80 43 85 03 2b 02 42 10 4e 6c c7 32 c0 5b 91 16 93 67 c6 46 b0 00 4d>
 console.log('secret length is: ', sha256KeyAsString.length); // Secret length is 32
 var sha256KeyAsBinary = sha256Encrypt('gKSfthqC5W5shXpfVujf', 'binary');
 console.log('secret is: ', sha256KeyAsBinary); // Result is a binary string
 console.log('secret length is: ', sha256KeyAsBinary.length); // Secret length is 32

// Try to encrypt the message using the key
 var message = '123456789;00005;1234567';
 var messageUtf8 = require('utf8').encode(message);
 var aes256KeyWithStringSecret = aes256Encrypt2(messageUtf8, sha256KeyAsString); // This works great with a key being a string but my key is a BINARY one
 console.log(aes256KeyWithStringSecret);

 // Try to encrypt the message using the key
 var message = '123456789;00005;1234567';
 var messageUtf8 = require('utf8').encode(message);
 var aes256KeyWithBinarySecret = aes256Encrypt2(messageUtf8, sha256KeyAsBinary); // This fails with a binary key
 console.log(aes256KeyWithBinarySecret);

Hope this helps you guys helping me :-)

@wangnan0610
Copy link

I also encountered the same problem !

@wangnan0610
Copy link

Hope someone help me ~

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

4 participants