【问题标题】:AES encryption method in NodeJS similar to C sharp functionNodeJS中类似于CSharp函数的AES加密方法
【发布时间】:2020-10-22 19:19:58
【问题描述】:

我有一个用 C# 编写的函数。基本上,该函数用于根据文本和密钥等参数生成令牌。

public string Encrypt(string input, string key) {
     byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
     byte[] toEncrptArray = UTF8Encoding.UTF8.GetBytes(input);
     Aes kgen = Aes.Create("AES");
     kgen.Mode = CipherMode.ECB;
     kgen.Key = keyArray;
     ICryptoTransform cTransform = kgen.CreateEncryptor();
     byte[] resultArray = cTransform.TransformFinalBlock(toEncrptArray, 0, toEncrptArray.Length);

     return Convert.ToBase64String(resultArray, 0, resultArray.Length);
   }

我正在尝试在 NodeJS 中为上述函数搜索任何相同的替代方法,或者通过任何编译器在 NodeJS 脚本中运行此函数。

我在 NodeJS 中尝试了 crypto-js 模块,但得到了不同的令牌字符串。请提出替代函数或关于在 NodeJS 脚本中运行此函数的任何想法。

我最近在 NodeJS 中的代码:

第一种方法:

var CryptoJS = require("crypto-js");

// Encrypt
var ciphertext = CryptoJS.AES.encrypt("<input>", "<key>").toString();

第二种方法:

var crypto = require('crypto'),
    algorithm = 'aes-256-ctr',
    password = '<key>';

function encrypt(text){
  var cipher = crypto.createCipher(algorithm,password)
  var crypted = cipher.update(text,'utf8','hex')
  crypted += cipher.final('hex');
  return crypted;
}

如果与 C# 函数相比,这两种方法都给出不同的标记。

【问题讨论】:

  • 这只是带有 ECB 模式的 AES(顺便说一下,ECB 是不安全的)。这可以通过 CryptoJS 和来自 NodeJS 的加密模块来实现。发布您最近的代码。
  • @Topaco:在问题中添加了最近的 Nodejs 代码。请看一下并提出建议。

标签: javascript c# node.js encryption aes


【解决方案1】:

C# 代码中使用的 AES 算法是 ECB 模式下的 AES 128 位。

我们可以在 Node.js 中执行相同的加密(如果我们愿意也可以解密),使用以下代码:

Node.js 代码

const crypto = require("crypto");

function encrypt(plainText, key, outputEncoding = "base64") {
    const cipher = crypto.createCipheriv("aes-128-ecb", key, null);
    let encrypted = cipher.update(plainText, 'utf8', outputEncoding)
    encrypted += cipher.final(outputEncoding);
    return encrypted;
}

function decrypt(cipherText, key, outputEncoding = "utf8") {
    const cipher = crypto.createDecipheriv("aes-128-ecb", key, null);
    let encrypted = cipher.update(cipherText)
    encrypted += cipher.final(outputEncoding);
    return encrypted;
}

const KEY = Buffer.from("abcdefghijklmnop", "utf8");

console.log("Key length (bits):", KEY.length * 8);
const encrypted = encrypt("hello world", KEY, "base64");
console.log("Encrypted string (base64):", encrypted);

// And if we wish to decrypt as well:
const decrypted = decrypt(Buffer.from(encrypted, "base64"), KEY, "utf8")
console.log("Decrypted string:", decrypted);

C# 代码

using System;
using System.Text;
using System.Security.Cryptography;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Result: " + Encrypt("hello world", "abcdefghijklmnop"));
    }
    
    public static string Encrypt(string input, string key) {
     byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
     byte[] toEncrptArray = UTF8Encoding.UTF8.GetBytes(input);
     Aes kgen = Aes.Create("AES");
     kgen.Mode = CipherMode.ECB;
     kgen.Key = keyArray;
     ICryptoTransform cTransform = kgen.CreateEncryptor();
     byte[] resultArray = cTransform.TransformFinalBlock(toEncrptArray, 0, toEncrptArray.Length);

     return Convert.ToBase64String(resultArray, 0, resultArray.Length);
   }
}

加密结果(明文和密钥同上)为:

.Net: f7sSBDV0N6MOpRJLpSJL0w==
Node.js: f7sSBDV0N6MOpRJLpSJL0w==

显然我们不能在生产中使用这个密钥!

【讨论】:

  • 谢谢@Terry 你节省了我很多时间。正如我的预期,它运行良好
  • 没问题!在不同的语言中以相同的方式进行加密/解密可能非常棘手!
猜你喜欢
  • 1970-01-01
  • 2018-01-04
  • 2021-11-23
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
  • 2021-09-24
  • 2019-02-14
相关资源
最近更新 更多