简体   繁体   English

在JavaScript中从C#复制AES解密方法

[英]Reproduce AES decryption method from C# in JavaScript

I am trying to reproduce the following C# decryption method in JavaScript. 我试图在JavaScript中重现以下C#解密方法。

This method is used to decrypt short strings: names, addresses, email addresses, etc. 此方法用于解密短字符串:名称,地址,电子邮件地址等。

It feels tantalisingly close, because the strings I have been able to "successfully" decrypt seem partially decrypted. 感觉非常接近,因为我能够“成功”解密的字符串似乎已部分解密。

For instance, some of the emails look like this: x"R Îd¹1gtWÈ2)web@example.com 例如,某些电子邮件如下所示: x"R Îd¹1gtWÈ2)web@example.com

CSharp 尖锐的

public static readonly byte[] INIT_VECTOR = { 0x00, 0x00, ... };

public static string Decrypt(string cipherText) {

  string EncryptionKey = "Some Encryption Key";

  byte[] cipherBytes = Convert.FromBase64String(cipherText);

  using (Aes encryptor = Aes.Create())
  {
​
    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, INIT_VECTOR);

    encryptor.Key = pdb.GetBytes(32);
    encryptor.IV = pdb.GetBytes(16);

    using (MemoryStream ms = new MemoryStream())
    {
      using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
      {
        cs.Write(cipherBytes, 0, cipherBytes.Length);
        cs.Close();
      }
      cipherText = Encoding.Unicode.GetString(ms.ToArray());
    }
  }

  return cipherText;
}

JavaScript 的JavaScript

import atob from 'atob';
import forge from 'node-forge';

const InitVector = [0x00, ...];
const EncryptionKey = 'Some Encryption Key';

const iv = Buffer.from(InitVector).toString();

const convertBase64StringToUint8Array = input => {
  const data = atob(input);
  const array = Uint8Array.from(data, b => b.charCodeAt(0));

  return array;
};

const decrypt = cipher => {
  const cipherArray = convertBase64StringToUint8Array(cipher);

  const key = forge.pkcs5.pbkdf2(EncryptionKey, iv, 1000, 32);

  const decipher = forge.cipher.createDecipher('AES-CBC', key);

  decipher.start({ iv });

  decipher.update(forge.util.createBuffer(cipherArray, 'raw'));

  const result = decipher.finish();

  if (result) {
    return decipher.output.data;
  } else {
    return false;
  }
};

Thanks to kelalaka I managed to figure this out! 多亏了kelalaka,我设法弄清楚了!

This was the code I ended up with. 这是我最终得到的代码。

import atob from 'atob';
import forge from 'node-forge';

const InitVector = [0x00, ...];
const EncryptionKey = 'Some Encryption Key';

const initKey = Buffer.from(InitVector).toString(); // Changed this to `initKey`

const convertBase64StringToUint8Array = input => {
  const data = atob(input);
  const array = Uint8Array.from(data, b => b.charCodeAt(0));

  return array;
};

const decrypt = cipher => {
  const cipherArray = convertBase64StringToUint8Array(cipher);

  const key = forge.pkcs5.pbkdf2(EncryptionKey, iv, 1000, 32);

  /**
   * Added the following
   * Note the key size = 48
   *  This was due to the fact that the C# dictated that
   *  the IV was 16 bytes, starting at the end of the key.
   */
  const keyAndIV = forge.pkcs5.pbkdf2(encryptionKey, initKey, 1000, 32 + 16);

  /**
   * Therefore, we cut the iv from the new string
   */
  const iv = keyAndIV.slice(32, 32 + 16); // 16 bytes

  const decipher = forge.cipher.createDecipher(
    'AES-CBC',
    forge.util.createBuffer(key)
  );

  decipher.start({ iv });

  decipher.update(forge.util.createBuffer(cipherArray, 'raw'));

  const result = decipher.finish();

  if (result) {
    return decipher.output.data;
  } else {
    return false;
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM