首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将节点js代码转换为c#加密

如何将节点js代码转换为c#加密
EN

Stack Overflow用户
提问于 2022-06-13 11:11:09
回答 1查看 62关注 0票数 0

基于下面的示例,节点js显示了我需要的东西,没有变化的能力,但是在c#中需要它

我有节点js代码生成:ciph DhX6hhOUpeYZJexlMD+6zg==

代码语言:javascript
复制
const crypto = require('crypto'); 
var plainText = '124_12344612'; 
var keys ="gjOMA9mMihhOANN4erqksLIaHzDZWP0shFayUU39C0A="; //password/
  
var hash = Buffer.from(keys, 'base64');
 
var iv = Buffer.from([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
 
var cipher = crypto.createCipheriv('aes-256-cbc',hash,Buffer.from(iv, 'utf8'));
 
var ciph = cipher.update(plainText,'utf8','base64');

ciph +=cipher.final('base64')
 
console.log("ciph" ,ciph)

这是c#代码生成的: nXU8XunpCy6OhiM251qcqg==

代码语言:javascript
复制
     private const int AesKeySize = 16;
   static void Main(string[] args)
        {  
            string password =  "gjOMA9mMihhOANN4erqksLIaHzDZWP0shFayUU39C0A="; 
            string message = "124_12344612";
           

            // Create sha256 hash
            SHA256 mySHA256 = SHA256Managed.Create();
            byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(password));

            // Create secret IV
            byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };

            string encrypted = EncryptString(message, key, iv);
            string decrypted = DecryptString(encrypted, key, iv);
          
            Console.ReadLine();
        }
        public static string EncryptString(string plainText, byte[] key, byte[] iv)
        {
            // Instantiate a new Aes object to perform string symmetric encryption
            Aes encryptor = Aes.Create();

            encryptor.Mode = CipherMode.CBC;

            // Set key and IV
            byte[] aesKey = new byte[32];
            Array.Copy(key, 0, aesKey, 0, 32);
            encryptor.Key = aesKey;
            encryptor.IV = iv;

            // Instantiate a new MemoryStream object to contain the encrypted bytes
            MemoryStream memoryStream = new MemoryStream();

            // Instantiate a new encryptor from our Aes object
            ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();

            // Instantiate a new CryptoStream object to process the data and write it to the 
            // memory stream
            CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);

            // Convert the plainText string into a byte array
            byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);

            // Encrypt the input plaintext string
            cryptoStream.Write(plainBytes, 0, plainBytes.Length);

            // Complete the encryption process
            cryptoStream.FlushFinalBlock();

            // Convert the encrypted data from a MemoryStream to a byte array
            byte[] cipherBytes = memoryStream.ToArray();

            // Close both the MemoryStream and the CryptoStream
            memoryStream.Close();
            cryptoStream.Close();

            // Convert the encrypted byte array to a base64 encoded string
            string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
             

            // Return the encrypted data as a string
            return cipherText;
        }

如何更改我编写的返回的c#代码: DhX6hhOUpeYZJexlMD+6zg==与节点js相同

谢谢Thomas Weller/jdweng/Lukas Hein基于您提供的解决方案:更新的代码

代码语言:javascript
复制
  static void Main(string[] args)
        {
          
            string password = "gjOMA9mMihhOANN4erqksLIaHzDZWP0shFayUU39C0A"; 
            string message = "124_12344612";

            byte[] key = Convert.FromBase64String(password);

            // Create secret IV
            byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };

            string encrypted = EncryptString(message, key, iv);
            string decrypted = DecryptString(encrypted, key, iv);
          
            Console.ReadLine();
        }

public static string EncryptString(string plainText, byte[] key, byte[] iv)
        {
            // Instantiate a new Aes object to perform string symmetric encryption
            Aes encryptor = Aes.Create();

            encryptor.Mode = CipherMode.CBC;

            // Set key and IV
            byte[] aesKey = new byte[32];
            Array.Copy(key, 0, aesKey, 0, 32);
            encryptor.Key = aesKey;
            encryptor.IV = iv;
            //encryptor.Padding = PaddingMode.Zeros;
            //encryptor.Padding = PaddingMode.ISO10126;
            //encryptor.Padding = PaddingMode.ANSIX923;
            encryptor.Padding = PaddingMode.PKCS7;

            // Instantiate a new MemoryStream object to contain the encrypted bytes
            MemoryStream memoryStream = new MemoryStream();

            // Instantiate a new encryptor from our Aes object
            ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();

            // Instantiate a new CryptoStream object to process the data and write it to the 
            // memory stream
            CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);

            // Convert the plainText string into a byte array
            byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);

            // Encrypt the input plaintext string
            cryptoStream.Write(plainBytes, 0, plainBytes.Length);

            // Complete the encryption process
            cryptoStream.FlushFinalBlock();

            // Convert the encrypted data from a MemoryStream to a byte array
            byte[] cipherBytes = memoryStream.ToArray();

            // Close both the MemoryStream and the CryptoStream
            memoryStream.Close();
            cryptoStream.Close();

            // Convert the encrypted byte array to a base64 encoded string
            string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
             

            // Return the encrypted data as a string
            return cipherText;
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-13 12:05:05

通过散列您的密码,您实际上使用了另一个密钥。只需使用byte[] key = Convert.FromBase64String(password);

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

https://stackoverflow.com/questions/72601871

复制
相关文章

相似问题

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