based on the example below the node js is showing what l need and don't have power to change but need it in c#
l have node js code generates : 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)
this is the c# code generates : 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;
}
how do l alter the c# code l have written to return : DhX6hhOUpeYZJexlMD 6zg== same as the node js
thank you Thomas Weller/jdweng/Lukas Hein it works based on your provided solution : updated code
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;
}
CodePudding user response:
By hashing your password you actually use another key. Just use byte[] key = Convert.FromBase64String(password);