9

I'm looking for an easy way to encrypt and decrypt some data using a fixed key (which will be stored in my configurations) and store the result as string (base16 or hex).

Something like

string myString = "hello world";
string myKey = "k2k3aij3h";
string enc = new Algorithm().Encrypt(myString, myKey);
string dec = new Alrorithm().Decrypt(enc, myKey);
venerik
  • 5,766
  • 2
  • 33
  • 43
Fabricio
  • 7,705
  • 9
  • 52
  • 87
  • The one I'm looking for.. anyone! I just want an easy step to do that, like examplified. – Fabricio Jun 20 '13 at 12:41
  • I'm checking if there is some already done in .net framework. – Fabricio Jun 20 '13 at 12:42
  • The search box can help you: http://stackoverflow.com/questions/165808/simple-2-way-encryption-for-c-sharp?rq=1 – D.Rosado Jun 20 '13 at 12:43
  • 1
    This totally depend on what and how much you want to encrypt. There are many APIs for the same. You need to select the best that fits in your application – Ankit Jun 20 '13 at 12:47
  • @AtishDipongkor Have you written many of your own good secure encryption algorithms lately that stand up to a beating? – Lloyd Jun 20 '13 at 13:08
  • @Fabricio you should not decrypt a password. To match encrypted the password with same key and algorithm then match the with original one. The value that can be decrypted is not string encryption algorithm. – pcbabu Jun 20 '13 at 13:15
  • Have you looked in System.Cryptography? There's a whole namespace dedicated to it. – Simon Halsey Jun 20 '13 at 13:29
  • 1
    You could always use ROT13 – Simon Halsey Jun 20 '13 at 13:30
  • @Fabricio, I had the same problem a while back and solved it with a c# implementation of Rinjdael. It worked totally great, and I recommend it. I still have the source laying around if you really need it too. – Gayot Fow Jun 20 '13 at 14:36

1 Answers1

9

try this

       private const string initVector = "tu89geji340t89u2";

        private const int keysize = 256;

        public static string Encrypt(string Text, string Key)
        {
            byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(Text);
            PasswordDeriveBytes password = new PasswordDeriveBytes(Key, null);
            byte[] keyBytes = password.GetBytes(keysize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] Encrypted = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            return Convert.ToBase64String(Encrypted);
        }

        public static string Decrypt(string EncryptedText, string Key)
        {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] DeEncryptedText = Convert.FromBase64String(EncryptedText);
            PasswordDeriveBytes password = new PasswordDeriveBytes(Key, null);
            byte[] keyBytes = password.GetBytes(keysize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream(DeEncryptedText);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[DeEncryptedText.Length];
            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        }

it's from here

Community
  • 1
  • 1
Alyafey
  • 1,455
  • 3
  • 15
  • 23
  • 2
    A static IV is not the correct way to do this. It will cause various security issues. The "Key" parameter is named wrong here, since it's passed to a key derivation function. Using a KDF without a salt is wrong too. What I'm trying to say is that cryptography is hard, and you really have to know what you're doing to use it correctly – copy-pasting code from the internet won't make it any easier. – ntoskrnl Jun 21 '13 at 08:54
  • 2
    -1 Dislike cut and paste of incorrect crypto that was written to "work" but doesn't really. – jbtule Jun 21 '13 at 13:00
  • Not good encryption code. Avoid copy&paste from other sites – zon7 Jan 30 '17 at 12:18