0

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;
        }
4
  • Why do you have an additional SHA-256 in the C# code? Commented Jun 13, 2022 at 11:14
  • hi @ThomasWeller l had done another one without the SHA-256 but was getting : Y/gHY4fOgwZ+nYAsGzG7iw== so wanted to try with it Commented Jun 13, 2022 at 11:47
  • 1
    Trial and error is a scientifical way to discover things. There might be better approaches for this cross compilation problem, though. Commented Jun 13, 2022 at 12:00
  • The default padding mode is different in c# from js. You have to change the padding like : AES.Padding = PaddingMode.PKCS7;. See : ourcodeworld.com/articles/read/471/… Commented Jun 13, 2022 at 12:07

1 Answer 1

3

By hashing your password you actually use another key. Just use byte[] key = Convert.FromBase64String(password);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.