Support Forums
[C#] Simple RC2 Encryption/Decryption [Intermediate] - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [C#] Simple RC2 Encryption/Decryption [Intermediate] (/showthread.php?tid=11672)



[C#] Simple RC2 Encryption/Decryption [Intermediate] - Mike - 08-31-2010

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"));



RE: [C#] Simple RC2 Encryption/Decryption [Intermediate] - NathanE - 12-14-2010

very nice snippet mate,keep up the good work!


RE: [C#] Simple RC2 Encryption/Decryption [Intermediate] - Saint Michael - 12-21-2010

Indeed, post some more soon.


RE: [C#] Simple RC2 Encryption/Decryption [Intermediate] - ZxPwn420 - 12-21-2010

Thanks for this Big Grin