首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >实现SHA256散列的问题,然后是pbkdf2Sync,最后是使用AES-256-cbc - c#对Nodejs进行加密。

实现SHA256散列的问题,然后是pbkdf2Sync,最后是使用AES-256-cbc - c#对Nodejs进行加密。
EN

Stack Overflow用户
提问于 2022-11-12 13:04:02
回答 1查看 57关注 0票数 0

我得到了一些c#代码,它实现了SHA256散列,然后是AES-256-cbc.现在我必须把同样的翻译成NodeJS。我在这里尝试了几种选择,文档和问题/答案,但都没有帮助。当我第一次使用加密的时候,可能是编码出了问题--但是无法准确地知道是什么。下面是c#实现:

代码语言:javascript
运行
复制
using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        HelloWorld h1 = new HelloWorld();
        Console.WriteLine(h1.EncryptText("Vitthal", "Vitthal"));
    }
        public string EncryptText(string pInput, string password)
        {
            byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(GenerateSHA256String(pInput));
            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
            byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
            string result = Convert.ToBase64String(bytesEncrypted);
            return result;
        }



//  method name GenerateSHA256String and code 


  public string GenerateSHA256String(string inputString)
        {
            StringBuilder stringBuilder = new StringBuilder();
            try
            {
                SHA256 sha256 = SHA256Managed.Create();
                byte[] bytes = Encoding.UTF8.GetBytes(inputString);
                byte[] hash = sha256.ComputeHash(bytes);
                for (int i = 0; i <= hash.Length - 1; i++)
                    stringBuilder.Append(hash[i].ToString("X2"));
                return stringBuilder.ToString();
            }
            catch (Exception ex)
            {
            }
            return stringBuilder.ToString();
        } 



//  method name AES_Encrypt and code 


 private byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
        {
            byte[] encryptedBytes = null;
            byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            using (MemoryStream ms = new MemoryStream())
            {
                using (RijndaelManaged AES = new RijndaelManaged())
                {
                    AES.KeySize = 256;
                    AES.BlockSize = 128;

                    var key = new System.Security.Cryptography.Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                    AES.Key = key.GetBytes(AES.KeySize / 8);
                    AES.IV = key.GetBytes(AES.BlockSize / 8);
                    AES.Mode = CipherMode.CBC;
                    using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                        cs.Close();
                    }
                    encryptedBytes = ms.ToArray();
                }
            }

            return encryptedBytes;
        }
}

下面是使用密码的NodeJS实现:

代码语言:javascript
运行
复制
const GenerateSHA256String = (object) => {
    const buff = Buffer.from(object.toString());
    const hash = createHash('sha256');
    hash.update(buff);
    const hashed = hash.digest('hex');
    return hashed;
}

const getEncryptedChecksum = (object) => {
    const payload = GenerateSHA256String(object);
    console.log(Buffer.from(payload));
    const passKey = Buffer.from('Vitthal');
    const saltString = [1,2,3,4,5,6,7,8];
    const key = pbkdf2Sync(GenerateSHA256String(passKey), Buffer.from(saltString), 1000, 100, 'sha1');
    const encKey = key.subarray(0, 32);
    const encIV = key.subarray(32, 48);
    const cipher = createCipheriv('aes-256-cbc', encKey, encIV);
    let encrypted = cipher.update(Buffer.from(payload), 'utf8', 'base64');
    encrypted += cipher.final('base64');
    return encrypted;
}

console.log(getEncryptedChecksum('Vitthal'));

在这方面的任何帮助都将受到高度赞赏。

EN

回答 1

Stack Overflow用户

发布于 2022-11-20 04:23:38

终于解决了。这是编码问题。c#和nodejs之间的一些奇怪的行为。无论如何,这是最后的nodejs代码。

代码语言:javascript
运行
复制
const GenerateSHA256String = (object, encoding) => {
    const buff = Buffer.from(object.toString());
    const hash = createHash('sha256');
    hash.update(buff);
    const hashed = hash.digest(encoding ? encoding : null);
    return hashed;
}

const getEncryptedChecksum = (object) => {
    const payload = GenerateSHA256String(object, 'hex');
    const payBuff = Buffer.from(payload.toUpperCase());
    const passKey = Buffer.from('NDSICDM');
    const saltString = [1,2,3,4,5,6,7,8];
    const key = pbkdf2Sync(GenerateSHA256String(passKey), Buffer.from(saltString), 1000, 64, 'sha1');
    const encKey = key.subarray(0, 32);
    const encIV = key.subarray(32, 48);
    const cipher = createCipheriv('aes-256-cbc', encKey, encIV);
    let encrypted = cipher.update(payBuff, 'utf8', 'base64');
    encrypted += cipher.final('base64');
    return encrypted;
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74413173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档