C# AES 256 Encryption

EGhewg5

Member
Jul 2, 2018
721
94
0
Machan me code eka balala improve karaganna dewal thiyanawanam kiyanawada...
Mata programming loku danumak naha. String, password wage dewal encrypt karaganna class ekak hada gannayi try karanne.

Me code eka gaththa source eka...
https://stackoverflow.com/questions/45567962

Code:
        private void Usage()
        {
            string message = "My secret message 1234";
            string password = "3sc3RLrpd17";

            // 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 };

            //using (var aes = new AesCryptoServiceProvider())
            //{
            //   aes.GenerateIV();
            //}                     

            string encrypted = this.EncryptString(message, key, iv);
            string decrypted = this.DecryptString(encrypted, key, iv);

            Console.WriteLine(encrypted);
            Console.WriteLine(decrypted);
        }


Code:
        public 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;
        }

Code:
        public string DecryptString(string cipherText, 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 aesDecryptor = encryptor.CreateDecryptor();

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

            // Will contain decrypted plaintext
            string plainText = String.Empty;

            try
            {
                // Convert the ciphertext string into a byte array
                byte[] cipherBytes = Convert.FromBase64String(cipherText);

                // Decrypt the input ciphertext string
                cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);

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

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

                // Convert the decrypted byte array to string
                plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
            }
            finally
            {
                // Close both the MemoryStream and the CryptoStream
                memoryStream.Close();
                cryptoStream.Close();
            }

            // Return the decrypted data as a string
            return plainText;
        }
 

hiran108

Well-known member
  • Nov 1, 2006
    1,705
    747
    113
    88
    oke initial vector eka random ekak ganing, eka secret venna ona na, guest karanna bari vennai ona. net eke thawa search karala balapan simplify karala liyanna.
    ex:
    string IVText = random.Next(0, 9).ToString();//generate random number for IV
    DES.IV = md5.ComputeHash(Encoding.Unicode.GetBytes(IVText)).Take(8).ToArray();
     

    EGhewg5

    Member
    Jul 2, 2018
    721
    94
    0
    Secret key eka hardcode karanne nathuwa wena method ekak use karanna machan.

    .net application reverse engineer karanna marama lesi.

    Security Key kiwwe password ekada machan...
    Mekenam Password ekanam hardcode karanne naha.
    Usage() method eka damme call karana widiya pennanna.

    Hardcode karanna wena thanwaladi monawage widiyata use karanne?
    Example Email ekaka credential wage application eke hardcode karanna wena kota..!
     

    EGhewg5

    Member
    Jul 2, 2018
    721
    94
    0
    oke initial vector eka random ekak ganing, eka secret venna ona na, guest karanna bari vennai ona. net eke thawa search karala balapan simplify karala liyanna.
    ex:
    string IVText = random.Next(0, 9).ToString();//generate random number for IV
    DES.IV = md5.ComputeHash(Encoding.Unicode.GetBytes(IVText)).Take(8).ToArray();

    Random IV ekak ganna code eka use karanna puluwan neda..?

    Code:
                byte[] iv = new byte[16];
    
                using (Aes myAes = Aes.Create())
                {
                    iv = myAes.IV;
                }
     
    Last edited: