using System; using System.Text; using System.Security.Cryptography; using System.IO; namespace Wayne.FDCPOSLibrary { public class DESCrypter { TripleDESCryptoServiceProvider cryptic = null; public DESCrypter() { cryptic = new TripleDESCryptoServiceProvider(); MD5Crypter md5 = new MD5Crypter(); cryptic.Key = md5.ComputeHash(UTF8Encoding.ASCII.GetBytes("DresserFusion}123")); //cryptic.IV = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes("DresserFusion}123")); cryptic.Mode = CipherMode.ECB; cryptic.Padding = PaddingMode.PKCS7; } public void Encrypt(String data, out string encryptedstring) { if (data == null) encryptedstring = ""; byte[] databytes = UTF8Encoding.UTF8.GetBytes(data); byte[] dataencrypted = Encrypt(databytes); encryptedstring = Convert.ToBase64String(dataencrypted, 0, dataencrypted.Length); } public byte[] Encrypt(String data) { if (data == null) return null; byte[] databytes = UTF8Encoding.UTF8.GetBytes(data); return Encrypt(databytes); } public void Encrypt(byte[] data, out string encryptedstring) { if (data == null) encryptedstring = ""; byte[] dataencrypted = Encrypt(data); encryptedstring = Convert.ToBase64String(dataencrypted, 0, dataencrypted.Length); } public byte[] Encrypt(byte[] databytes) { try { if (databytes == null) return null; ; // Create a CryptoStream using the FileStream // and the passed key and initialization vector (IV). ICryptoTransform encryptor = cryptic.CreateEncryptor(); // RijndaelAlg.CreateEncryptor(Key, IV); byte[] dataencrypted = encryptor.TransformFinalBlock(databytes, 0, databytes.GetLength(0)); return dataencrypted; } catch (CryptographicException e) { //sstrace.WriteLine(string.Format("A Cryptographic error occurred: {0}", e.Message)); } catch (UnauthorizedAccessException e) { //sstrace.WriteLine(string.Format("A file error occurred: {0}", e.Message)); } catch (Exception ex) { //sstrace.WriteLine(string.Format("A generic error occurred: {0}", e.Message)); } return null; } public void EncryptTextToFile(String Data, String FileName) { try { // Create or open the specified file. FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate); // Create a CryptoStream using the FileStream // and the passed key and initialization vector (IV). ICryptoTransform transform = cryptic.CreateEncryptor(); // RijndaelAlg.CreateEncryptor(Key, IV); CryptoStream cStream = new CryptoStream(fStream, transform, CryptoStreamMode.Write); // Create a StreamWriter using the CryptoStream. StreamWriter sWriter = new StreamWriter(cStream); try { // Write the data to the stream // to encrypt it. sWriter.WriteLine(Data); } catch (Exception e) { //sstrace.WriteLine(string.Format("An error occurred: {0}", e.Message)); } finally { // Close the streams and // close the file. sWriter.Close(); cStream.Close(); fStream.Close(); } } catch (CryptographicException e) { //sstrace.WriteLine(string.Format("A Cryptographic error occurred: {0}", e.Message)); } catch (UnauthorizedAccessException e) { //sstrace.WriteLine(string.Format("A file error occurred: {0}", e.Message)); } } public void Decrypt(string data, out string decryptedstring) { byte[] databytes = Convert.FromBase64String(data); byte[] decrypteddata = Decrypt(databytes); decryptedstring = UTF8Encoding.UTF8.GetString(decrypteddata, 0, decrypteddata.GetLength(0)); } public void Decrypt(byte[] data, out string decryptedstring) { byte[] decrypteddata = Decrypt(data); decryptedstring = UTF8Encoding.UTF8.GetString(decrypteddata, 0, decrypteddata.GetLength(0)); } public byte[] Decrypt(string data) { byte[] databytes = Convert.FromBase64String(data); return Decrypt(databytes); } public byte[] Decrypt(byte[] data) { try { ICryptoTransform decryptor = cryptic.CreateDecryptor(); return decryptor.TransformFinalBlock(data, 0, data.Length); } catch (CryptographicException e) { //sstrace.WriteLine(string.Format("A Cryptographic error occurred: {0}", e.Message)); } catch (UnauthorizedAccessException e) { //sstrace.WriteLine(string.Format("A file error occurred: {0}", e.Message)); } catch (Exception ex) { //sstrace.WriteLine(string.Format("A generic error occurred: {0}", e.Message)); } return null; } public string DecryptTextFromFile(String FileName) { try { // Create or open the specified file. FileStream fStream = File.Open(FileName, FileMode.Open); // Create a CryptoStream using the FileStream // and the passed key and initialization vector (IV). ICryptoTransform transform = cryptic.CreateDecryptor(); // RijndaelAlg.CreateEncryptor(Key, IV); CryptoStream cStream = new CryptoStream(fStream, transform, CryptoStreamMode.Read); // Create a StreamReader using the CryptoStream. StreamReader sReader = new StreamReader(cStream); string val = ""; try { // Read the data from the stream // to decrypt it. val = sReader.ReadToEnd(); } catch (Exception e) { //sstrace.WriteLine(string.Format("An error occurred: {0}", e.Message)); } finally { // Close the streams and // close the file. sReader.Close(); cStream.Close(); fStream.Close(); } // Return the string. return val; } catch (CryptographicException e) { //sstrace.WriteLine(string.Format("A Cryptographic error occurred: {0}", e.Message)); } catch (UnauthorizedAccessException e) { //sstrace.WriteLine(string.Format("A file error occurred: {0}", e.Message)); } return null; } } }