NodeJS中的AES加密方法类似于C Sharp函数

湿婆

我有一个用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#函数相比,两种方法都给出不同的令牌。

特里·伦诺克斯

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==

显然,我们绝不能在生产中使用此密钥!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法在C Sharp的声明中指定构造函数参数

来自分类Dev

类似于sql中的函数

来自分类Dev

C#中简化的类似于C宏的函数调用

来自分类Dev

C#中简化的类似于C宏的函数调用

来自分类Dev

F#函数中的类似于C ++的静态变量

来自分类Dev

C++ 中的函数类似于 numpy flatten

来自分类Dev

如何使用 1 个递归函数反转字符串中的 Word(c-sharp)

来自分类Dev

是否有类似于c#上的Environment.TickCount的NodeJs类/函数?

来自分类Dev

类似于Swift中的python的map函数

来自分类Dev

类似于Swift中的python的map函数

来自分类Dev

C ++中的Modulo函数,其行为类似于Matlab中的mod

来自分类Dev

函数的原型类似于在调用例程中声明函数?

来自分类Dev

是否有类似于 vba 中的 toString() 函数的实现函数?

来自分类Dev

C ++非虚函数的行为类似于虚函数

来自分类Dev

R中是否有类似于Excel中COUNTIF的函数?

来自分类Dev

python中是否有类似于matlab中的fzero的函数?

来自分类Dev

类似于创建对象而不是方法的匿名函数?

来自分类Dev

C ++中是否有任何可用于链表的内置函数,其功能类似于Java提供的indexOf函数?

来自分类Dev

我如何控制R中函数的输出(类似于lm)

来自分类Dev

是否可以在Python中创建类似于JS间隔的函数?

来自分类Dev

如何通过添加类似于++的函数在TypeScript中扩展Number

来自分类Dev

类似于Enterprise Architect中的Excels Macro函数

来自分类Dev

如何在Nim中编写类似于`echo`的函数?

来自分类Dev

JavaScript中的“ WebWorkers”概念是否类似于异步函数?

来自分类Dev

为什么`arguments`在JS函数中类似于数组?

来自分类Dev

在 Matlab 中隐藏类类似于私有函数

来自分类Dev

F#函数内部的类似于C ++的静态变量

来自分类Dev

无需超级用户即可访问的类似于C ++的ping函数

来自分类Dev

自己类的类似于C#数组的构造函数

Related 相关文章

  1. 1

    无法在C Sharp的声明中指定构造函数参数

  2. 2

    类似于sql中的函数

  3. 3

    C#中简化的类似于C宏的函数调用

  4. 4

    C#中简化的类似于C宏的函数调用

  5. 5

    F#函数中的类似于C ++的静态变量

  6. 6

    C++ 中的函数类似于 numpy flatten

  7. 7

    如何使用 1 个递归函数反转字符串中的 Word(c-sharp)

  8. 8

    是否有类似于c#上的Environment.TickCount的NodeJs类/函数?

  9. 9

    类似于Swift中的python的map函数

  10. 10

    类似于Swift中的python的map函数

  11. 11

    C ++中的Modulo函数,其行为类似于Matlab中的mod

  12. 12

    函数的原型类似于在调用例程中声明函数?

  13. 13

    是否有类似于 vba 中的 toString() 函数的实现函数?

  14. 14

    C ++非虚函数的行为类似于虚函数

  15. 15

    R中是否有类似于Excel中COUNTIF的函数?

  16. 16

    python中是否有类似于matlab中的fzero的函数?

  17. 17

    类似于创建对象而不是方法的匿名函数?

  18. 18

    C ++中是否有任何可用于链表的内置函数,其功能类似于Java提供的indexOf函数?

  19. 19

    我如何控制R中函数的输出(类似于lm)

  20. 20

    是否可以在Python中创建类似于JS间隔的函数?

  21. 21

    如何通过添加类似于++的函数在TypeScript中扩展Number

  22. 22

    类似于Enterprise Architect中的Excels Macro函数

  23. 23

    如何在Nim中编写类似于`echo`的函数?

  24. 24

    JavaScript中的“ WebWorkers”概念是否类似于异步函数?

  25. 25

    为什么`arguments`在JS函数中类似于数组?

  26. 26

    在 Matlab 中隐藏类类似于私有函数

  27. 27

    F#函数内部的类似于C ++的静态变量

  28. 28

    无需超级用户即可访问的类似于C ++的ping函数

  29. 29

    自己类的类似于C#数组的构造函数

热门标签

归档