DESCrypter.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using System.IO;
  6. namespace Wayne.FDCPOSLibrary
  7. {
  8. public class DESCrypter
  9. {
  10. TripleDESCryptoServiceProvider cryptic = null;
  11. public DESCrypter()
  12. {
  13. cryptic = new TripleDESCryptoServiceProvider();
  14. MD5Crypter md5 = new MD5Crypter();
  15. cryptic.Key = md5.ComputeHash(UTF8Encoding.ASCII.GetBytes("DresserFusion}123"));
  16. //cryptic.IV = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes("DresserFusion}123"));
  17. cryptic.Mode = CipherMode.ECB;
  18. cryptic.Padding = PaddingMode.PKCS7;
  19. }
  20. public void Encrypt(String data, out string encryptedstring)
  21. {
  22. if (data == null)
  23. encryptedstring = "";
  24. byte[] databytes = UTF8Encoding.UTF8.GetBytes(data);
  25. byte[] dataencrypted = Encrypt(databytes);
  26. encryptedstring = Convert.ToBase64String(dataencrypted, 0, dataencrypted.Length);
  27. }
  28. public byte[] Encrypt(String data)
  29. {
  30. if (data == null)
  31. return null;
  32. byte[] databytes = UTF8Encoding.UTF8.GetBytes(data);
  33. return Encrypt(databytes);
  34. }
  35. public void Encrypt(byte[] data, out string encryptedstring)
  36. {
  37. if (data == null)
  38. encryptedstring = "";
  39. byte[] dataencrypted = Encrypt(data);
  40. encryptedstring = Convert.ToBase64String(dataencrypted, 0, dataencrypted.Length);
  41. }
  42. public byte[] Encrypt(byte[] databytes)
  43. {
  44. try
  45. {
  46. if (databytes == null)
  47. return null; ;
  48. // Create a CryptoStream using the FileStream
  49. // and the passed key and initialization vector (IV).
  50. ICryptoTransform encryptor = cryptic.CreateEncryptor(); // RijndaelAlg.CreateEncryptor(Key, IV);
  51. byte[] dataencrypted = encryptor.TransformFinalBlock(databytes, 0, databytes.GetLength(0));
  52. return dataencrypted;
  53. }
  54. catch (CryptographicException e)
  55. {
  56. //sstrace.WriteLine(string.Format("A Cryptographic error occurred: {0}", e.Message));
  57. }
  58. catch (UnauthorizedAccessException e)
  59. {
  60. //sstrace.WriteLine(string.Format("A file error occurred: {0}", e.Message));
  61. }
  62. catch (Exception ex)
  63. {
  64. //sstrace.WriteLine(string.Format("A generic error occurred: {0}", e.Message));
  65. }
  66. return null;
  67. }
  68. public void EncryptTextToFile(String Data, String FileName)
  69. {
  70. try
  71. {
  72. // Create or open the specified file.
  73. FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);
  74. // Create a CryptoStream using the FileStream
  75. // and the passed key and initialization vector (IV).
  76. ICryptoTransform transform = cryptic.CreateEncryptor(); // RijndaelAlg.CreateEncryptor(Key, IV);
  77. CryptoStream cStream = new CryptoStream(fStream,
  78. transform,
  79. CryptoStreamMode.Write);
  80. // Create a StreamWriter using the CryptoStream.
  81. StreamWriter sWriter = new StreamWriter(cStream);
  82. try
  83. {
  84. // Write the data to the stream
  85. // to encrypt it.
  86. sWriter.WriteLine(Data);
  87. }
  88. catch (Exception e)
  89. {
  90. //sstrace.WriteLine(string.Format("An error occurred: {0}", e.Message));
  91. }
  92. finally
  93. {
  94. // Close the streams and
  95. // close the file.
  96. sWriter.Close();
  97. cStream.Close();
  98. fStream.Close();
  99. }
  100. }
  101. catch (CryptographicException e)
  102. {
  103. //sstrace.WriteLine(string.Format("A Cryptographic error occurred: {0}", e.Message));
  104. }
  105. catch (UnauthorizedAccessException e)
  106. {
  107. //sstrace.WriteLine(string.Format("A file error occurred: {0}", e.Message));
  108. }
  109. }
  110. public void Decrypt(string data, out string decryptedstring)
  111. {
  112. byte[] databytes = Convert.FromBase64String(data);
  113. byte[] decrypteddata = Decrypt(databytes);
  114. decryptedstring = UTF8Encoding.UTF8.GetString(decrypteddata, 0, decrypteddata.GetLength(0));
  115. }
  116. public void Decrypt(byte[] data, out string decryptedstring)
  117. {
  118. byte[] decrypteddata = Decrypt(data);
  119. decryptedstring = UTF8Encoding.UTF8.GetString(decrypteddata, 0, decrypteddata.GetLength(0));
  120. }
  121. public byte[] Decrypt(string data)
  122. {
  123. byte[] databytes = Convert.FromBase64String(data);
  124. return Decrypt(databytes);
  125. }
  126. public byte[] Decrypt(byte[] data)
  127. {
  128. try
  129. {
  130. ICryptoTransform decryptor = cryptic.CreateDecryptor();
  131. return decryptor.TransformFinalBlock(data, 0, data.Length);
  132. }
  133. catch (CryptographicException e)
  134. {
  135. //sstrace.WriteLine(string.Format("A Cryptographic error occurred: {0}", e.Message));
  136. }
  137. catch (UnauthorizedAccessException e)
  138. {
  139. //sstrace.WriteLine(string.Format("A file error occurred: {0}", e.Message));
  140. }
  141. catch (Exception ex)
  142. {
  143. //sstrace.WriteLine(string.Format("A generic error occurred: {0}", e.Message));
  144. }
  145. return null;
  146. }
  147. public string DecryptTextFromFile(String FileName)
  148. {
  149. try
  150. {
  151. // Create or open the specified file.
  152. FileStream fStream = File.Open(FileName, FileMode.Open);
  153. // Create a CryptoStream using the FileStream
  154. // and the passed key and initialization vector (IV).
  155. ICryptoTransform transform = cryptic.CreateDecryptor(); // RijndaelAlg.CreateEncryptor(Key, IV);
  156. CryptoStream cStream = new CryptoStream(fStream,
  157. transform,
  158. CryptoStreamMode.Read);
  159. // Create a StreamReader using the CryptoStream.
  160. StreamReader sReader = new StreamReader(cStream);
  161. string val = "";
  162. try
  163. {
  164. // Read the data from the stream
  165. // to decrypt it.
  166. val = sReader.ReadToEnd();
  167. }
  168. catch (Exception e)
  169. {
  170. //sstrace.WriteLine(string.Format("An error occurred: {0}", e.Message));
  171. }
  172. finally
  173. {
  174. // Close the streams and
  175. // close the file.
  176. sReader.Close();
  177. cStream.Close();
  178. fStream.Close();
  179. }
  180. // Return the string.
  181. return val;
  182. }
  183. catch (CryptographicException e)
  184. {
  185. //sstrace.WriteLine(string.Format("A Cryptographic error occurred: {0}", e.Message));
  186. }
  187. catch (UnauthorizedAccessException e)
  188. {
  189. //sstrace.WriteLine(string.Format("A file error occurred: {0}", e.Message));
  190. }
  191. return null;
  192. }
  193. }
  194. }