/* * Created by SharpDevelop. * User: Petteri Kautonen * Date: 26.11.2006 * Time: 21:21 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.IO; using System.Security.Cryptography; namespace VPKSoft { /// <summary> /// Description of SimpleCipher. /// </summary> public class SimpleCipher { public SimpleCipher() { } public const int KeyLen = 24; public const int IVLen = 8; public static byte [] ValidateKey(byte [] Key) { if (Key.Length > KeyLen) { byte [] Buffer = new byte[KeyLen]; Array.Copy(Key, Buffer, KeyLen); return Buffer; } else { byte [] Buffer = new byte [KeyLen]; Array.Copy(Key, Buffer, Key.Length); for (int i = Key.Length; i < KeyLen; i++) { Buffer[i] = (byte)i; } return Buffer; } } public static byte [] ValidateIV(byte [] IV) { if (IV.Length > IVLen) { byte [] Buffer = new byte[IVLen]; Array.Copy(IV, Buffer, IVLen); return Buffer; } else { byte [] Buffer = new byte [IVLen]; Array.Copy(IV, Buffer, IV.Length); for (int i = IV.Length; i < IVLen; i++) { Buffer[i] = (byte)i; } return Buffer; } } public static byte [] Encrypt(byte [] Bytes, byte [] Key, byte [] IV) { MemoryStream Ms = new MemoryStream(); TripleDESCryptoServiceProvider Tdcsp = new TripleDESCryptoServiceProvider(); CryptoStream Cs = new CryptoStream(Ms, Tdcsp.CreateEncryptor(Key , IV), CryptoStreamMode.Write); Cs.Write(Bytes, 0, Bytes.Length); try { Cs.Flush(); Cs.FlushFinalBlock(); Cs.Close(); } catch { } return Ms.GetBuffer(); } public static byte [] Decrypt(byte [] Bytes, byte [] Key, byte [] IV) { MemoryStream Ms = new MemoryStream(); TripleDESCryptoServiceProvider Tdcsp = new TripleDESCryptoServiceProvider(); CryptoStream Cs = new CryptoStream(Ms, Tdcsp.CreateDecryptor(Key, IV), CryptoStreamMode.Write); Cs.Write(Bytes, 0, Bytes.Length); try { Cs.Flush(); Cs.FlushFinalBlock(); Cs.Close(); } catch { } return Ms.GetBuffer(); } } }