0

My clean data is : Test12345678910 My secret key is : 12345678901234561234567890123456

When i encrypt the data i get this output : ldJbAK2rYjDnS6kWz2O+Aw==

when i decrypt it i get error

internal/crypto/cipher.js:164
  const ret = this._handle.final();
                           ^

Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length

my code is ;

  const crypto = require('crypto');
  const encrypted = "ldJbAK2rYjDnS6kWz2O+Aw==";
  const key = "12345678901234561234567890123456";
  let decipher = crypto.createDecipher('aes-256-cbc', key);
  let decrypted = decipher.update(encrypted);
  decrypted = [decrypted, decipher.final("utf-8")];
  console.log(decrypted.toString());

Encrypting from C#

public static string EncryptToken(string text)
{
    string confkey = "12345678901234561234567890123456";
    byte[] array;

    try
    {
        using (Aes aes = Aes.Create())
        {

            aes.Key = Encoding.UTF8.GetBytes(confkey);
            aes.IV = new byte[16];

            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
                    {
                        streamWriter.Write(text);
                    }

                    array = memoryStream.ToArray();
                }
            }
        }

        return Convert.ToBase64String(array);
    }
    catch
    {
        return string.Empty;

    }
}
Somomo1q
  • 655
  • 2
  • 10
  • 19
  • 1
    `crypto.createDecipher` is deprecated, are you sure that you want use it? Did you use `createCipheriv` to crypt it? – Manuel Spigolon Feb 18 '20 at 10:20
  • i dont have iv :/ – Somomo1q Feb 18 '20 at 10:26
  • 1
    If you cannot specify how the ciphertext is created then this becomes a guessing game, one most of us won't want to play. Please show the encryption routine. – Maarten Bodewes Feb 18 '20 at 10:27
  • i put the encryption code its writen from C# – Somomo1q Feb 18 '20 at 10:30
  • Use [`crypto.createDecipheriv`](https://nodejs.org/api/crypto.html#crypto_crypto_createdecipheriv_algorithm_key_iv_options) as already implicitly suggested by @ManuelSpigolon, a 0-IV (`\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0`) and also specify the input- and output-encodings in the `update`-call (`base64` as input- and `utf-8` as output-encoding). Furthermore it makes more sense to concatenate the results of the `update`- and `final`-call (instead of creating an array). [Here](https://nodejs.org/api/crypto.html#crypto_class_decipher) you can find examples for `createDecipheriv`. – Topaco Feb 18 '20 at 11:56

0 Answers0