Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C#] Simple RC2 Encryption/Decryption [Intermediate]
#1
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"));
Reply
#2
very nice snippet mate,keep up the good work!
Reply
#3
Indeed, post some more soon.
[Image: angelsig.jpg]


Reply
#4
Thanks for this Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Source] TripleDES String Encryption / Decryption euverve 2 3,347 05-13-2011, 10:35 AM
Last Post: Imports System.Net
  [Source] Encrypt/Decrypt a string using Data Encryption Standard (DES) algorithm euverve 0 1,998 05-12-2011, 08:43 PM
Last Post: euverve
  [Help] [VB.NET 2008] Textbox Contents Encryption. Example 4 4,002 02-23-2011, 01:16 AM
Last Post: Example
  [C#] Loading/Writing XML Files [Intermediate] Mike 4 2,281 09-29-2010, 10:17 AM
Last Post: Fitz-
  [C#] Get External IP through Webrequests [Intermediate] Mike 6 2,794 09-02-2010, 07:58 PM
Last Post: xHtmlPhP

Forum Jump:


Users browsing this thread: 1 Guest(s)