Skip to content

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

@sovattha

Description

@sovattha

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions