基于下面的示例,节点js显示了我需要的东西,没有变化的能力,但是在c#中需要它
我有节点js代码生成:ciph DhX6hhOUpeYZJexlMD+6zg==
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==
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基于您提供的解决方案:更新的代码
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;
}发布于 2022-06-13 12:05:05
通过散列您的密码,您实际上使用了另一个密钥。只需使用byte[] key = Convert.FromBase64String(password);
https://stackoverflow.com/questions/72601871
复制相似问题