Archive for Luglio 8th, 2008

Hash Utility

CodeKeep C# Feed Luglio 8th, 2008

Description: Class to create Hash Values

Link: http://www.codekeep.net/snippets/1e501783-1268-487c-bb8d-7eff5e6ce820.aspx

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

namespace AjaxClassLibrary
{
    public sealed class HashUtility
    {
        #region Public Methods
            private static string _salt = "usm28";            
        #endregion Public Methods

        public HashUtility()
        {
        }

        private static string AddHashSalt(String s)
        {
            return ( _salt + s + s.Length.ToString());
        }

        // Convert String to Byte
        public static Byte[] ConvertStringToByteArray(String s)
        {
            return (new UnicodeEncoding()).GetBytes(HashUtility.AddHashSalt(s));
        }

        public static Byte[] Hashstring(String s)
        {
            Byte[] dataToHash = ConvertStringToByteArray(s);
            Byte[] hashvalue = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(dataToHash);
            return hashvalue;
        }

        public bool Compare(String comparestring, Byte[] HashField)
        {
            try
            {
                //Convert s1 to byte array
                Byte[] data1ToHash = ConvertStringToByteArray(comparestring);

                //Create hash value from String using MD5 instance returned by Crypto Config system
                byte[] hashvalue1 = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(data1ToHash);

                //Memberwise compare of hash value bytes
                int i = 0;
                bool same = true;
                do
                {
                    if (hashvalue1[i] != HashField[i])
                    {
                        same = false;
                        break;
                    }
                    i++;
                } while (i < hashvalue1.Length);

                return same;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

    }
}

       
   
  

File Encryption Class

CodeKeep C# Feed Luglio 8th, 2008

Description: Class to Encrypt File & File Streams

Link: http://www.codekeep.net/snippets/37c87564-e72e-4bad-bf34-52e4bcacb3a7.aspx

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

namespace AjaxClassLibrary
{
    public class FileEncryptionClass
    {
        #region ClassProperties
        string _password = "1234abcd";

        #endregion ClassProperties

        #region Private Properties

        private byte[] EncryptionKey()
        {
            UnicodeEncoding aUE = new UnicodeEncoding();
            byte[] keyBytes = aUE.GetBytes(_password);
            return keyBytes;
        }

        private bool CheckSourceFile(String SourceFile)       
        {
            if (File.Exists(SourceFile))
                return true;
            else
            {
                throw new ApplicationException("File does not Exist");
            }
        }


        #endregion Private Properties

        public FileEncryptionClass()
        {
        }

        public void EncryptionofFileStream(FileStream Source, FileStream Destination)
        {
            CryptoStream aCryptoStream = null;
            try
            {
                RijndaelManaged RMCrypto = new RijndaelManaged();
                aCryptoStream = new CryptoStream(Destination, RMCrypto.CreateEncryptor(EncryptionKey(), EncryptionKey()), CryptoStreamMode.Write);

                int data;
                while ((data = Source.ReadByte()) != -1)
                    aCryptoStream.WriteByte((byte)data);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (aCryptoStream != null)
                {
                    aCryptoStream.Close();
                }
            }
        } // EncryptionofFileStream

        public void EncryptionofFile(string FileSource, string FileDestination)
        {
            if (!CheckSourceFile(FileSource))
                return;

            FileStream fsIn = new FileStream(FileSource, FileMode.Open);
            FileStream file1 = new FileStream(FileDestination, FileMode.OpenOrCreate, FileAccess.Write);
            try
            {
                EncryptionofFileStream(fsIn, file1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                fsIn.Close();
                file1.Close();
            }
        } // EncryptionofFile

        public void DecryptionofFile(string EncryptionFileSource, string DecryptionFileSource)
        {
            FileStream file1 = new FileStream(EncryptionFileSource, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            FileStream fsIn = new FileStream(DecryptionFileSource, FileMode.OpenOrCreate, FileAccess.Write);

            CryptoStream aCryptoStream = null;
            try
            {
                RijndaelManaged RMCrypto = new RijndaelManaged();
                aCryptoStream = new CryptoStream(file1, RMCrypto.CreateDecryptor(EncryptionKey(), EncryptionKey()), CryptoStreamMode.Read);

                StreamReader reader = new StreamReader(aCryptoStream);

                string data = reader.ReadToEnd();
                byte[] data1 = ASCIIEncoding.ASCII.GetBytes(data);

                fsIn.Write(data1,0,data1.Length);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (aCryptoStream != null)
                {
                    aCryptoStream.Close();
                }
                fsIn.Close();
                file1.Close();
            }

        } // DecryptionofFile
    } 
}

transparent component

CodeKeep C# Feed Luglio 8th, 2008

Description: this is essential to create a component with transparent background

Link: http://www.codekeep.net/snippets/7c94d14a-c6ed-4f14-bfe1-bc110173845d.aspx

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;
                return cp;
            }
        }

public constructor()
{
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.Opaque, true);
}

transparent component

CodeKeep C# Feed Luglio 8th, 2008

Description: this is essential to create a component with transparent background

Link: http://www.codekeep.net/snippets/64c5818b-142f-4068-bc28-d12aa189b0cb.aspx

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;
                return cp;
            }
        }

public constructor()
{
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.Opaque, true);
}

Call private method by method name

CodeKeep C# Feed Luglio 8th, 2008

Description: You have a class with a private method. From within the class, you want to call the private method by method name instead of calling the method directly.

Link: http://www.codekeep.net/snippets/1535fbdd-9343-4825-9030-412f9a7227ce.aspx

MethodInfo methodInfo = this.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
methodInfo.Invoke(this, args);

//note: use BindingFlags.Static if the private method is static