26 Temmuz 2011 Salı

TripleDES Encryption Methods

TripleDES de şifreleme ve deşifreleme yapmak için yazmış olduğum method...

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


public static class TripleDESEncryption
    {
        public static string Encrypt(string Input, string Key)
        {
            if (string.IsNullOrEmpty(Input) || string.IsNullOrEmpty(Key))
            {
                throw new ArgumentNullException("The input/key string can not be empty.");
            }
            ASCIIEncoding Encoding = new ASCIIEncoding();
            byte[] Hash = Encoding.GetBytes(Key);
            byte[] KeyArray = new byte[24];
            byte[] Key2Array = new byte[8];
            for (int x = 0; x < 24; ++x)
            {
                KeyArray[x] = Hash[x];
            }
            for (int x = 0; x < 8; ++x)
            {
                Key2Array[x] = Hash[x + 8];
            }
            byte[] Text = null;
            TripleDESCryptoServiceProvider Encryptor = new TripleDESCryptoServiceProvider();
            using (MemoryStream Stream = new MemoryStream())
            {
                using (CryptoStream DESStream = new CryptoStream(Stream, Encryptor.CreateEncryptor(KeyArray, Key2Array), CryptoStreamMode.Write))
                {
                    using (StreamWriter Writer = new StreamWriter(DESStream))
                    {
                        Writer.Write(Input);
                        Writer.Flush();
                        DESStream.FlushFinalBlock();
                        Writer.Flush();
                        Text = Stream.GetBuffer();
                    }
                }
            }
            Encryptor.Clear();
            return Convert.ToBase64String(Text, 0, (int)Text.Length);
        }
 

        public static string Decrypt(string Input, string Key)
        {
            if (string.IsNullOrEmpty(Input) || string.IsNullOrEmpty(Key))
            {
                throw new ArgumentNullException("The input/key string can not be empty.");
            }
            ASCIIEncoding Encoding = new ASCIIEncoding();
            byte[] Hash = Encoding.GetBytes(Key);
            byte[] KeyArray = new byte[24];
            byte[] Key2Array = new byte[8];
            for (int x = 0; x < 24; ++x)
            {
                KeyArray[x] = Hash[x];
            }
            for (int x = 0; x < 8; ++x)
            {
                Key2Array[x] = Hash[x + 8];
            }
            string Text = "";
            TripleDESCryptoServiceProvider Decryptor = new TripleDESCryptoServiceProvider();
            using (MemoryStream Stream = new MemoryStream(Convert.FromBase64String(Input)))
            {
                using (CryptoStream DESStream = new CryptoStream(Stream, Decryptor.CreateDecryptor(KeyArray, Key2Array), CryptoStreamMode.Read))
                {
                    using (StreamReader Reader = new StreamReader(DESStream))
                    {
                        Text = Reader.ReadToEnd();
                    }
                }
            }
            Decryptor.Clear();
            return Text;
        }
    }

Hiç yorum yok:

Yorum Gönder