using System; using System.ComponentModel; using System.Collections; using System.Diagnostics; using System.IO; using System.Security.Cryptography; using System.Text; namespace Remoting.Common ... { public class CryptoService : System.ComponentModel.Component ...{ private System.ComponentModel.Container components = null; Fields#region Fields private byte[] _key=new Byte[16]; private byte[] _IV=new Byte[16]; private string _name="ajflaksowndkjiwernhirwer"; private string _password="kahiuenjkhiweruwknrku"; private bool _nameonly=false; TripleDESCryptoServiceProvider clientDESCryptoServiceProvider; #endregion Properties#region Properties public string Name ...{ get ...{ return "*******"; } set ...{ _key=GetKey16(value); if(_nameonly) _IV=GetKey16(value); } } public string Password ...{ get ...{ return "****"; } set ...{ if(!_nameonly) _IV=GetKey16(value); } } public bool UseNameOnly ...{ get ...{ return _nameonly; } set ...{ _nameonly=value; } } public byte[] Key ...{ set ...{ if(value.Length==16) this._key=value; else throw new System.Exception("Key is invalid."); } } public byte[] IV ...{ set ...{ if(value.Length==8||value.Length==16) this._IV=value; else throw new System.Exception("IV is invalid."); } } #endregion methods#region methods GenerateKey#region GenerateKey public void GenerateKey(string name,string password) ...{ _key=GetKey16(name); _IV=GetKey16(password); } public void GenerateKey(string name) ...{ _key=GetKey16(name); _IV=GetKey16(name); } public static byte[] GetKey16(string name) ...{ UnicodeEncoding UE = new UnicodeEncoding(); byte[] keys = UE.GetBytes(name); SHA1Managed SHhash = new SHA1Managed(); byte[] HashValue = SHhash.ComputeHash(keys); byte[] key=new Byte[16]; for(int i=0;i<16;i++) ...{ key[i]=HashValue[i]; } return key; } public static byte[] GetKey8(string name) ...{ UnicodeEncoding UE = new UnicodeEncoding(); byte[] keys = UE.GetBytes(name); SHA1Managed SHhash = new SHA1Managed(); byte[] HashValue = SHhash.ComputeHash(keys); byte[] key=new Byte[8]; for(int i=0;i<8;i++) ...{ key[i]=HashValue[i]; } return key; } #endregion public byte[] GetKey(string name, int bytes) ...{ if(bytes<0 || bytes>64) ...{ throw new System.Exception("Key Bytes not less than 0 or large than 64."); return null; } else ...{ UnicodeEncoding UE = new UnicodeEncoding(); byte[] keys = UE.GetBytes(name); SHA512Managed SHhash = new SHA512Managed(); byte[] HashValue = SHhash.ComputeHash(keys); byte[] key=new Byte[bytes]; for(int i=0;i<bytes;i++) ...{ key[i]=HashValue[i]; } return key; } } public static byte[] GetHashKey(string name) ...{ UnicodeEncoding UE = new UnicodeEncoding(); byte[] keys = UE.GetBytes(name); SHA512Managed SHhash = new SHA512Managed(); return SHhash.ComputeHash(keys); } static RijndaelManaged#region static RijndaelManaged public static string Encrypt(string strinput,byte[] key,byte[] IV) ...{ UnicodeEncoding UE = new UnicodeEncoding(); MemoryStream ms = new MemoryStream(); byte[] encryptBytes=null; RijndaelManaged RMCrypto = new RijndaelManaged(); ICryptoTransform desencrypt = RMCrypto.CreateEncryptor(key, IV); CryptoStream cs = new CryptoStream(ms,desencrypt,CryptoStreamMode.Write); try ...{ using(StreamWriter sw=new StreamWriter(cs,Encoding.Unicode)) ...{ sw.Write(strinput); sw.Flush(); cs.FlushFinalBlock(); ms.Position=0; encryptBytes=new byte[(int)ms.Length]; ms.Read(encryptBytes, 0,(int)ms.Length); } return Convert.ToBase64String(encryptBytes); } catch (System.Exception ee) ...{ Console.WriteLine(ee.Message); } finally ...{ ms.Close(); cs.Close(); } return Convert.ToBase64String(encryptBytes); } public static string Descrypt(string strinput,byte[] key,byte[] IV) ...{ UnicodeEncoding UE = new UnicodeEncoding(); Byte[] encryptBytes=Convert.FromBase64String(strinput); RijndaelManaged RMCrypto = new RijndaelManaged(); ICryptoTransform desencrypt = RMCrypto.CreateDecryptor(key, IV); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write); byte[] initialText = new Byte[encryptBytes.Length]; try ...{ cs.Write(encryptBytes, 0, encryptBytes.Length); cs.FlushFinalBlock(); StreamReader myReader; ms.Position=0; myReader=new StreamReader(ms,System.Text.Encoding.Unicode); return myReader.ReadToEnd(); } catch (System.Exception ee) ...{ Console.WriteLine(ee.Message); } finally ...{ ms.Close(); cs.Close(); } return UE.GetString(initialText); } #endregion RijndaelManaged#region RijndaelManaged public string Encrypt(string strinput) ...{ UnicodeEncoding UE = new UnicodeEncoding(); MemoryStream ms = new MemoryStream(); byte[] encryptBytes=null; RijndaelManaged RMCrypto = new RijndaelManaged(); ICryptoTransform desencrypt = RMCrypto.CreateEncryptor(_key, _IV); CryptoStream cs = new CryptoStream(ms,desencrypt,CryptoStreamMode.Write); try ...{ using(StreamWriter sw=new StreamWriter(cs,Encoding.Unicode)) ...{ sw.Write(strinput); sw.Flush(); cs.FlushFinalBlock(); ms.Position=0; encryptBytes=new byte[(int)ms.Length]; ms.Read(encryptBytes, 0,(int)ms.Length); } return Convert.ToBase64String(encryptBytes); } catch (System.Exception ee) ...{ Console.WriteLine(ee.Message); } finally ...{ ms.Close(); cs.Close(); } return Convert.ToBase64String(encryptBytes); } public string Descrypt(string strinput) ...{ UnicodeEncoding UE = new UnicodeEncoding(); Byte[] encryptBytes=Convert.FromBase64String(strinput); RijndaelManaged RMCrypto = new RijndaelManaged(); ICryptoTransform desencrypt = RMCrypto.CreateDecryptor(_key, _IV); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write); byte[] initialText = new Byte[encryptBytes.Length]; try ...{ cs.Write(encryptBytes, 0, encryptBytes.Length); cs.FlushFinalBlock(); StreamReader myReader; ms.Position=0; myReader=new StreamReader(ms,System.Text.Encoding.Unicode); return myReader.ReadToEnd(); } catch (System.Exception ee) ...{ Console.WriteLine(ee.Message); } finally ...{ ms.Close(); cs.Close(); } return UE.GetString(initialText); } #endregion TripleDES#region TripleDES public string EncryptString(string AString) ...{ clientDESCryptoServiceProvider = new TripleDESCryptoServiceProvider(); clientDESCryptoServiceProvider.Key = new Byte[] ...{111, 222, 121, 82, 172, 21, 185, 152, 228, 182, 72, 132, 123, 123, 131, 12}; clientDESCryptoServiceProvider.IV = new Byte[] ...{172, 223, 13, 42, 252, 102, 81, 211}; if(AString == String.Empty) ...{ return AString; } else ...{ byte[] encryptedData; MemoryStream dataStream=null; ICryptoTransform encryptor; encryptor = clientDESCryptoServiceProvider.CreateEncryptor(); try ...{ dataStream = new MemoryStream(); CryptoStream encryptedStream=null; try ...{ encryptedStream = new CryptoStream(dataStream, encryptor, CryptoStreamMode.Write); StreamWriter theWriter=null; try ...{ theWriter = new StreamWriter(encryptedStream, System.Text.Encoding.Unicode); theWriter.Write(AString); theWriter.Flush(); encryptedStream.FlushFinalBlock(); dataStream.Position = 0; encryptedData=new byte[dataStream.Length]; dataStream.Read(encryptedData, 0, (int)dataStream.Length); return Convert.ToBase64String(encryptedData); } finally ...{ theWriter.Close(); } } finally ...{ encryptedStream.Close(); } } finally ...{ dataStream.Close(); } } } public string DecryptString(string AString) ...{ clientDESCryptoServiceProvider = new TripleDESCryptoServiceProvider(); clientDESCryptoServiceProvider.Key = new Byte[] ...{111, 222, 121, 82, 172, 21, 185, 152, 228, 182, 72, 132, 123, 123, 131, 12}; clientDESCryptoServiceProvider.IV = new Byte[] ...{172, 223, 13, 42, 252, 102, 81, 211}; if(AString == String.Empty) ...{ return AString; } else ...{ byte[] encryptedData; char[] dData; MemoryStream dataStream=null; StreamReader myReader; CryptoStream encryptedStream=null; int strLen; encryptedData = Convert.FromBase64String(AString); try ...{ dataStream = new MemoryStream(); try ...{ ICryptoTransform decryptor ; decryptor = clientDESCryptoServiceProvider.CreateDecryptor(); encryptedStream = new CryptoStream(dataStream, decryptor, CryptoStreamMode.Write); encryptedStream.Write(encryptedData, 0, encryptedData.Length); encryptedStream.FlushFinalBlock(); dataStream.Position = 0; myReader = new StreamReader(dataStream, System.Text.Encoding.Unicode); return myReader.ReadToEnd(); } finally ...{ encryptedStream.Close(); } } finally ...{ dataStream.Close(); } } } #endregion Encrypt&Decrypt File#region Encrypt&Decrypt File private void EncryptFile(string cryptfile,string plainfile) ...{ try ...{ UnicodeEncoding UE = new UnicodeEncoding(); byte[] keys = UE.GetBytes("password"); SHA1Managed SHhash = new SHA1Managed(); byte[] HashValue = SHhash.ComputeHash(keys); byte[] key=new Byte[16]; for(int i=0;i<16;i++) ...{ key[i]=HashValue[i]; } FileStream fsCrypt=new FileStream(cryptfile, FileMode.Create); RijndaelManaged RMCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write); FileStream fsIn=new FileStream(plainfile,FileMode.Open); int data; while ((data=fsIn.ReadByte())!=-1) cs.WriteByte((byte) data); fsIn.Close(); cs.Close(); fsCrypt.Close(); } catch ...{ } } private void DecryptFile(string cryptfile,string plainfile) ...{ try ...{ UnicodeEncoding UE = new UnicodeEncoding(); byte[] keys = UE.GetBytes("password"); SHA1Managed SHhash = new SHA1Managed(); byte[] HashValue = SHhash.ComputeHash(keys); byte[] key=new Byte[16]; for(int i=0;i<16;i++) ...{ key[i]=HashValue[i]; } FileStream fsCrypt = new FileStream(cryptfile,FileMode.Open); RijndaelManaged RMCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read); FileStream fsOut=new FileStream(plainfile,FileMode.Create); int data; while ((data=cs.ReadByte())!=-1) fsOut.WriteByte((byte) data); fsOut.Close(); cs.Close(); fsCrypt.Close(); } catch ...{ } } #endregion #endregion Construct#region Construct public CryptoService(System.ComponentModel.IContainer container) ...{ container.Add(this); InitializeComponent(); } public CryptoService() ...{ InitializeComponent(); } #endregion Component Designer generated code#region Component Designer generated code private void InitializeComponent() ...{ components = new System.ComponentModel.Container(); } #endregion }}