[Adapdev-commits] Adapdev/src/Adapdev/Cryptography Crypto.cs,1.2,1.3 DecryptTransformer.cs,1.2,1.3 D
Status: Beta
Brought to you by:
intesar66
Update of /cvsroot/adapdev/Adapdev/src/Adapdev/Cryptography In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19977/src/Adapdev/Cryptography Added Files: Crypto.cs DecryptTransformer.cs Decryptor.cs EncryptTransformer.cs EncryptionAlgorithm.cs Encryptor.cs Hasher.cs Log Message: Reposting to the repository after it got hosed --- NEW FILE: Decryptor.cs --- // Original Copyright 2002 Microsoft Corporation. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp #region Modified Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion using System; using System.Security.Cryptography; using System.IO; namespace Adapdev.Cryptography { /// <summary> /// Summary description for Decryptor. /// </summary> internal class Decryptor { private DecryptTransformer _transformer; private byte[] _initVec; /// <summary> /// Constructor /// </summary> /// <param name="algId">The algorithm to use for decryption</param> internal Decryptor(EncryptionAlgorithm algId) { _transformer = new DecryptTransformer(algId); } /// <summary> /// Decrypts the data /// </summary> /// <param name="bytesData">The data to decrypt</param> /// <param name="bytesKey">The key to use</param> /// <returns></returns> internal byte[] Decrypt(byte[] bytesData, byte[] bytesKey) { //Set up the memory stream for the decrypted data. MemoryStream memStreamDecryptedData = new MemoryStream(); //Pass in the initialization vector. _transformer.IV = _initVec; ICryptoTransform transform = _transformer.GetCryptoServiceProvider(bytesKey); CryptoStream decStream = new CryptoStream(memStreamDecryptedData, transform, CryptoStreamMode.Write); try { decStream.Write(bytesData, 0, bytesData.Length); } catch(Exception ex) { throw new Exception("Error while writing encrypted data to the stream: \n" + ex.Message); } decStream.FlushFinalBlock(); decStream.Close(); // Send the data back. return memStreamDecryptedData.ToArray(); } //end Decrypt /// <summary> /// Sets the initial vector (IV) /// </summary> internal byte[] IV { set{_initVec = value;} } } } --- NEW FILE: Hasher.cs --- // Original Copyright (c) 2004 Brad Vincent. http://www.codeproject.com/csharp/CyptoHashing.asp #region Modified Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion using System; using System.IO; using System.Text; using System.Security.Cryptography; namespace Adapdev.Cryptography { /// <summary> /// Hashing class. Only static members so no need to create an instance /// </summary> public class Hasher { #region enum, constants and fields //types of hashing available public enum HashType { SHA, SHA256, SHA384, SHA512, MD5 } #endregion #region static members public static string Hash(String inputText) { return ComputeHash(inputText,HashType.MD5); } public static string Hash(String inputText, HashType hashingType) { return ComputeHash(inputText,hashingType); } /// <summary> /// returns true if the input text is equal to hashed text /// </summary> /// <param name="inputText">unhashed text to test</param> /// <param name="hashText">already hashed text</param> /// <returns>boolean true or false</returns> public static bool IsHashEqual(string inputText, string hashText) { return (Hash(inputText) == hashText); } public static bool IsHashEqual(string inputText, string hashText, HashType hashingType) { return (Hash(inputText,hashingType) == hashText); } #endregion #region Hashing Engine /// <summary> /// computes the hash code and converts it to string /// </summary> /// <param name="inputText">input text to be hashed</param> /// <param name="hashingType">type of hashing to use</param> /// <returns>hashed string</returns> private static string ComputeHash(string inputText, HashType hashingType) { HashAlgorithm HA = getHashAlgorithm(hashingType); //declare a new encoder UTF8Encoding UTF8Encoder = new UTF8Encoding(); //get byte representation of input text byte[] inputBytes = UTF8Encoder.GetBytes(inputText); //hash the input byte array byte[] output = HA.ComputeHash(inputBytes); //convert output byte array to a string return Convert.ToBase64String(output); } /// <summary> /// returns the specific hashing alorithm /// </summary> /// <param name="hashingType">type of hashing to use</param> /// <returns>HashAlgorithm</returns> private static HashAlgorithm getHashAlgorithm(HashType hashingType) { switch (hashingType) { case HashType.MD5 : return new MD5CryptoServiceProvider(); case HashType.SHA : return new SHA1CryptoServiceProvider(); case HashType.SHA256 : return new SHA256Managed(); case HashType.SHA384 : return new SHA384Managed(); case HashType.SHA512 : return new SHA512Managed(); default : return new MD5CryptoServiceProvider(); } } #endregion } } --- NEW FILE: Encryptor.cs --- // Original Copyright 2002 Microsoft Corporation. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp #region Modified Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion using System; using System.Security.Cryptography; using System.IO; namespace Adapdev.Cryptography { /// <summary> /// Summary description for Encryptor. /// </summary> internal class Encryptor { private EncryptTransformer _transformer; private byte[] _initVec; private byte[] _encKey; /// <summary> /// Constructor /// </summary> /// <param name="algId">The encryption algorithm to use</param> internal Encryptor(EncryptionAlgorithm algId) { _transformer = new EncryptTransformer(algId); } /// <summary> /// Encrypts data /// </summary> /// <param name="bytesData">The data to encrypt</param> /// <param name="bytesKey">The key to use</param> /// <returns></returns> internal byte[] Encrypt(byte[] bytesData, byte[] bytesKey) { //Set up the stream that will hold the encrypted data. MemoryStream memStreamEncryptedData = new MemoryStream(); _transformer.IV = _initVec; ICryptoTransform transform = _transformer.GetCryptoServiceProvider(bytesKey); CryptoStream encStream = new CryptoStream(memStreamEncryptedData, transform, CryptoStreamMode.Write); try { //Encrypt the data, write it to the memory stream. encStream.Write(bytesData, 0, bytesData.Length); } catch(Exception ex) { throw new Exception("Error while writing encrypted data to the stream: \n" + ex.Message); } //Set the IV and key for the client to retrieve _encKey = _transformer.Key; _initVec = _transformer.IV; encStream.FlushFinalBlock(); encStream.Close(); //Send the data back. return memStreamEncryptedData.ToArray(); }//end Encrypt /// <summary> /// Gets / sets the initial vector /// </summary> internal byte[] IV { get{return _initVec;} set{_initVec = value;} } /// <summary> /// Gets / sets the key /// </summary> internal byte[] Key { get{return _encKey;} } } } --- NEW FILE: EncryptTransformer.cs --- // Original Copyright 2002 Microsoft Corporation. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp #region Modified Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion using System; using System.Security.Cryptography; namespace Adapdev.Cryptography { /// <summary> /// Summary description for EncryptTransformer. /// </summary> internal class EncryptTransformer { private EncryptionAlgorithm _algorithmID; private byte[] _initVec; private byte[] _encKey; internal EncryptTransformer(EncryptionAlgorithm algId) { //Save the algorithm being used. _algorithmID = algId; } internal ICryptoTransform GetCryptoServiceProvider(byte[] bytesKey) { // Pick the provider. switch (_algorithmID) { case EncryptionAlgorithm.Des: { DES des = new DESCryptoServiceProvider(); des.Mode = CipherMode.CBC; // See if a key was provided if (null == bytesKey) { _encKey = des.Key; } else { des.Key = bytesKey; _encKey = des.Key; } // See if the client provided an initialization vector if (null == _initVec) { // Have the algorithm create one _initVec = des.IV; } else { //No, give it to the algorithm des.IV = _initVec; } return des.CreateEncryptor(); } case EncryptionAlgorithm.TripleDes: { TripleDES des3 = new TripleDESCryptoServiceProvider(); des3.Mode = CipherMode.CBC; // See if a key was provided if (null == bytesKey) { _encKey = des3.Key; } else { des3.Key = bytesKey; _encKey = des3.Key; } // See if the client provided an IV if (null == _initVec) { //Yes, have the alg create one _initVec = des3.IV; } else { //No, give it to the alg. des3.IV = _initVec; } return des3.CreateEncryptor(); } case EncryptionAlgorithm.Rc2: { RC2 rc2 = new RC2CryptoServiceProvider(); rc2.Mode = CipherMode.CBC; // Test to see if a key was provided if (null == bytesKey) { _encKey = rc2.Key; } else { rc2.Key = bytesKey; _encKey = rc2.Key; } // See if the client provided an IV if (null == _initVec) { //Yes, have the alg create one _initVec = rc2.IV; } else { //No, give it to the alg. rc2.IV = _initVec; } return rc2.CreateEncryptor(); } case EncryptionAlgorithm.Rijndael: { Rijndael rijndael = new RijndaelManaged(); rijndael.Mode = CipherMode.CBC; // Test to see if a key was provided if(null == bytesKey) { _encKey = rijndael.Key; } else { rijndael.Key = bytesKey; _encKey = rijndael.Key; } // See if the client provided an IV if(null == _initVec) { //Yes, have the alg create one _initVec = rijndael.IV; } else { //No, give it to the alg. rijndael.IV = _initVec; } return rijndael.CreateEncryptor(); } default: { throw new CryptographicException("Algorithm ID '" + _algorithmID + "' not supported."); } } } internal byte[] IV { get{return _initVec;} set{_initVec = value;} } internal byte[] Key { get{return _encKey;} } } } --- NEW FILE: DecryptTransformer.cs --- // Original Copyright 2002 Microsoft Corporation. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp #region Modified Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion using System; using System.Security.Cryptography; namespace Adapdev.Cryptography { /// <summary> /// Summary description for DecryptTransformer. /// </summary> internal class DecryptTransformer { private EncryptionAlgorithm _algorithmID; private byte[] _initVec; internal DecryptTransformer(EncryptionAlgorithm deCryptId) { _algorithmID = deCryptId; } internal ICryptoTransform GetCryptoServiceProvider(byte[] bytesKey) { // Pick the provider. switch (_algorithmID) { case EncryptionAlgorithm.Des: { DES des = new DESCryptoServiceProvider(); des.Mode = CipherMode.CBC; des.Key = bytesKey; des.IV = _initVec; return des.CreateDecryptor(); } case EncryptionAlgorithm.TripleDes: { TripleDES des3 = new TripleDESCryptoServiceProvider(); des3.Mode = CipherMode.CBC; return des3.CreateDecryptor(bytesKey, _initVec); } case EncryptionAlgorithm.Rc2: { RC2 rc2 = new RC2CryptoServiceProvider(); rc2.Mode = CipherMode.CBC; return rc2.CreateDecryptor(bytesKey, _initVec); } case EncryptionAlgorithm.Rijndael: { Rijndael rijndael = new RijndaelManaged(); rijndael.Mode = CipherMode.CBC; return rijndael.CreateDecryptor(bytesKey, _initVec); } default: { throw new CryptographicException("Algorithm ID '" + _algorithmID + "' not supported."); } } } //end GetCryptoServiceProvider internal byte[] IV { set{_initVec = value;} } } } --- NEW FILE: EncryptionAlgorithm.cs --- // Original Copyright 2002 Microsoft Corporation. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp #region Modified Copyright / License Information /* Copyright 2004 - 2005 Adapdev Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================ Author Log ============================ III Full Name SMM Sean McCormack (Adapdev) ============================ Change Log ============================ III MMDDYY Change */ #endregion using System; namespace Adapdev.Cryptography { /// <summary> /// The available encryption methods /// </summary> public enum EncryptionAlgorithm { /// <summary> /// DES /// </summary> Des = 1, /// <summary> /// RC2 /// </summary> Rc2, /// <summary> /// Rijndael /// </summary> Rijndael, /// <summary> /// Triple DES /// </summary> TripleDes } } --- NEW FILE: Crypto.cs --- using System; using System.Text; namespace Adapdev.Cryptography { /// <summary> /// Summary description for Crypto. /// </summary> public class Crypto { // Des private static string _password8 = "aZbX12Yu"; // Rijndael / TripleDes private static string _password16 = "aI/c$kd8Hbb1R4nv"; // Des / TripleDes private static string _vector8 = "xbhhU7yp"; // Rijndael private static string _vector16 = "ai(hu#4x7^6txgGh"; /// <summary> /// Creates a new <see cref="Crypto"/> instance. /// </summary> private Crypto() { } /// <summary> /// Encrypts the specified text. /// </summary> /// <param name="text">Text.</param> /// <param name="algorithm">Algorithm.</param> public static byte[] Encrypt(string text, EncryptionAlgorithm algorithm) { if(algorithm == EncryptionAlgorithm.Des) { return Encrypt(text, Crypto._password8, Crypto._vector8, algorithm); } if(algorithm == EncryptionAlgorithm.Rijndael) { return Encrypt(text, Crypto._password16, Crypto._vector16, algorithm); } if(algorithm == EncryptionAlgorithm.TripleDes) { return Encrypt(text, Crypto._password16, Crypto._vector8, algorithm); } throw new Exception(algorithm.ToString() + " is not supported."); } /// <summary> /// Encrypts the specified text. /// </summary> /// <param name="text">Text.</param> /// <param name="key">Key.</param> /// <param name="vector">Vector.</param> /// <param name="algorithm">Algorithm.</param> public static byte[] Encrypt(string text, string key, string vector, EncryptionAlgorithm algorithm) { Validate(algorithm, key, vector); byte[] tIV = null; byte[] tkey = null; byte[] cipherText = null; byte[] plainText = Encoding.ASCII.GetBytes(text); Encryptor e = new Encryptor(algorithm); tkey = Encoding.ASCII.GetBytes(key); tIV = Encoding.ASCII.GetBytes(vector); e.IV = tIV; cipherText = e.Encrypt(plainText, tkey); return cipherText; } /// <summary> /// Validates the specified algorithm. /// </summary> /// <param name="algorithm">Algorithm.</param> /// <param name="key">Key.</param> /// <param name="vector">Vector.</param> private static void Validate(EncryptionAlgorithm algorithm, string key, string vector) { if(algorithm == EncryptionAlgorithm.Des) { if(key.Length != 8) throw new Exception("key length must be 8 for " + algorithm.ToString()); if(vector.Length != 8) throw new Exception("vector length must be 8 for " + algorithm.ToString()); } if(algorithm == EncryptionAlgorithm.Rijndael) { if(key.Length != 16) throw new Exception("key length must be 16 for " + algorithm.ToString()); if(vector.Length != 16) throw new Exception("vector length must be 16 for " + algorithm.ToString()); } if(algorithm == EncryptionAlgorithm.TripleDes) { if(key.Length != 16) throw new Exception("key length must be 16 for " + algorithm.ToString()); if(vector.Length != 8) throw new Exception("vector length must be 8 for " + algorithm.ToString()); } } /// <summary> /// Decrypts the specified bytes. /// </summary> /// <param name="key">Key.</param> /// <param name="vector">Vector.</param> /// <param name="algorithm">Algorithm.</param> /// <returns></returns> public static string Decrypt(byte[] cipherText, string key, string vector, EncryptionAlgorithm algorithm) { Validate(algorithm, key, vector); byte[] tIV = null; byte[] tkey = null; byte[] plainText = null; Decryptor dec = new Decryptor(algorithm); tkey = Encoding.ASCII.GetBytes(key); tIV = Encoding.ASCII.GetBytes(vector); dec.IV = tIV; // Go ahead and decrypt. plainText = dec.Decrypt(cipherText, tkey); return Encoding.ASCII.GetString(plainText); } /// <summary> /// Encrypts the specified text. /// </summary> /// <param name="cipherText">CipherText.</param> /// <param name="algorithm">Algorithm.</param> public static string Decrypt(byte[] cipherText, EncryptionAlgorithm algorithm) { if(algorithm == EncryptionAlgorithm.Des) { return Decrypt(cipherText, Crypto._password8, Crypto._vector8, algorithm); } if(algorithm == EncryptionAlgorithm.Rijndael) { return Decrypt(cipherText, Crypto._password16, Crypto._vector16, algorithm); } if(algorithm == EncryptionAlgorithm.TripleDes) { return Decrypt(cipherText, Crypto._password16, Crypto._vector8, algorithm); } throw new Exception(algorithm.ToString() + " is not supported."); } } } |