Support Forums

Full Version: [C#] Simple RC2 Encryption/Decryption [Intermediate]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Wrote this in about 20 minutes. Give credit if you use it.

Imports:
Code:
using System.Security.Cryptography;
using System.IO;

Code:
Code:
static string strRC2Encryption(string strInput, string strKey, string strIV)
        {
            try
            {
                byte[] byteInput = Encoding.UTF8.GetBytes(strInput);
                byte[] byteKey = Encoding.ASCII.GetBytes(strKey);
                byte[] byteIV = Encoding.ASCII.GetBytes(strIV);
                MemoryStream MS = new MemoryStream();
                RC2CryptoServiceProvider CryptoMethod = new RC2CryptoServiceProvider();
                CryptoStream CS = new CryptoStream(MS, CryptoMethod.CreateEncryptor(byteKey, byteIV), CryptoStreamMode.Write);
                CS.Write(byteInput, 0, byteInput.Length);
                CS.FlushFinalBlock();
                return Convert.ToBase64String(MS.ToArray());
            }
            catch (Exception up)
            {
                throw up; //YUCK! ;D
            }
        }

        static string strRC2Decryption(string strInput, string strKey, string strIV)
        {
            try
            {
                byte[] byteInput = Convert.FromBase64String(strInput);
                byte[] byteKey = Encoding.ASCII.GetBytes(strKey);
                byte[] byteIV = Encoding.ASCII.GetBytes(strIV);
                MemoryStream MS = new MemoryStream();
                RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider();
                CryptoStream CS = new CryptoStream(MS, RC2.CreateDecryptor(byteKey, byteIV), CryptoStreamMode.Write);
                CS.Write(byteInput, 0, byteInput.Length);
                CS.FlushFinalBlock();
                return Encoding.UTF8.GetString(MS.ToArray());
            }
            catch (Exception up)
            {
                throw up; //YUCK! ;D
            }
        }

Usage:
Code:
string s = strRC2Encryption("Mike", "AAAAAAAA", "AAAAAAAA");
Console.WriteLine(s);
Console.WriteLine(strRC2Decryption(s, "AAAAAAAA", "AAAAAAAA"));
very nice snippet mate,keep up the good work!
Indeed, post some more soon.
Thanks for this Big Grin