6

I'm currently needing a way to encrypt a string and decrypt a byte array using AES-128 symmetrical encryption, in C#. I can't find a way how to do this, but maybe I've missed something.

kdh
  • 927
  • 1
  • 8
  • 18
  • 2
    Why do you want to avoid IVs? They're an important security feature. – CodesInChaos Oct 18 '13 at 09:29
  • 1
    To expand on @CodesInChaos comment. IVs can be randomly generated and transmitted in the clear along with the ciphertext. Secrecy of the IV is not necessary for security. The requirements for an IV are that you don't reuse the same key and IV combination and that the IV be difficult to predict. – Dev Oct 21 '13 at 03:13
  • This question was for a project where somebody was interested in encrypting using AES without an IV. I do agree that they're an important security feature though! – kdh May 01 '14 at 06:17

1 Answers1

13

Import namespaces

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

     static void Main(string[] args)
        {
            string value = "@arifansari300<3>";

            string encryptedValue= EncryptDecrypt.Encrypt(value);

            string decryptedValue = EncryptDecrypt.Decrypt(encryptedValue);
        }

    public static string Encrypt(string clearText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new 
                Rfc2898DeriveBytes(EncryptionKey, new byte[] 
                { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return clearText;
    }

    public static string Decrypt(string cipherText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new 
                Rfc2898DeriveBytes(EncryptionKey, new byte[] 
                { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    }
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
Arif Ansari
  • 472
  • 1
  • 4
  • 12
  • 2
    `Rfc2898DeriveBytes` is only a good choice when you have to use passwords entered by the end-user instead of generating a proper key. But in that case you should use a salt (in your example it's a constant, that misses the point of IVs) and more iterations (at minimum 20k, preferably more). If you have a proper key, normal encryption is simpler and *much* faster. – CodesInChaos Oct 18 '13 at 15:15
  • How can i convert this function PHP ? – user3581428 Aug 08 '17 at 11:35
  • @CodesInChaos What do you mean by "normal encryption"? – Jamie Kitson Nov 21 '17 at 14:52
  • @JamieKitson Encryption where you use a random high-entropy key (e.g. 256 bits) instead of weak user memorable passwords. – CodesInChaos Nov 21 '17 at 15:57
  • 5
    Make sure to change the salt: 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 = Ivan Medvedev – Mathias Apr 03 '18 at 10:47
  • this code is so old, it might come from [here](https://social.msdn.microsoft.com/Forums/vstudio/en-US/245037b3-9c92-4bbd-bab0-6852002c26aa/decrypt-hash?forum=clr) which is a short form from [here](https://web.archive.org/web/20080208125623/http://www.dotnetthis.com/Articles/Crypto.htm). its 2019 and everyone still misses the `Ivan Medvedev` one. – Bagus Tesa Aug 29 '19 at 10:12
  • Muy buen aporte. – Maurico Flores Apr 16 '22 at 19:17