/Develop/SRC.mytrip.mvc/mtm.Core/Install/Cripto.cs

# · C# · 170 lines · 158 code · 10 blank · 2 comment · 10 complexity · 53f7eb37ce45b29f6fd43c5602c23a99 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.IO;
  8. using System.Xml.Linq;
  9. using mtm.Core.Settings;
  10. namespace mtm.Core.Install
  11. {
  12. internal static class Crypto
  13. {
  14. private static string password()
  15. {
  16. string absolutDirectory = GeneralMethods.GetPath("Web.config");
  17. XDocument doc = XDocument.Load(absolutDirectory);
  18. var core = doc.Root.Elements("appSettings").Elements("add");
  19. return core.FirstOrDefault(x => x.Attribute("key").Value == "applicationId")
  20. .Attribute("value").Value;
  21. }
  22. private static byte[] Encrypt(byte[] data)
  23. {
  24. SymmetricAlgorithm sa = Rijndael.Create();
  25. ICryptoTransform ct = sa.CreateEncryptor(
  26. (new PasswordDeriveBytes(password(), null)).GetBytes(16),
  27. new byte[16]);
  28. MemoryStream ms = new MemoryStream();
  29. CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
  30. cs.Write(data, 0, data.Length);
  31. cs.FlushFinalBlock();
  32. return ms.ToArray();
  33. }
  34. internal static string Encrypt(string data)
  35. {
  36. try
  37. {
  38. return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(data)));
  39. }
  40. catch { return "null"; }
  41. }
  42. private static byte[] Decrypt(byte[] data)
  43. {
  44. BinaryReader br = new BinaryReader(InternalDecrypt(data));
  45. return br.ReadBytes((int)br.BaseStream.Length);
  46. }
  47. internal static string Decrypt(string data)
  48. {
  49. try
  50. {
  51. CryptoStream cs = InternalDecrypt(Convert.FromBase64String(data));
  52. StreamReader sr = new StreamReader(cs);
  53. return sr.ReadToEnd();
  54. }
  55. catch { return "null"; }
  56. }
  57. private static CryptoStream InternalDecrypt(byte[] data)
  58. {
  59. SymmetricAlgorithm sa = Rijndael.Create();
  60. ICryptoTransform ct = sa.CreateDecryptor(
  61. (new PasswordDeriveBytes(password(), null)).GetBytes(16),
  62. new byte[16]);
  63. MemoryStream ms = new MemoryStream(data);
  64. return new CryptoStream(ms, ct, CryptoStreamMode.Read);
  65. }
  66. /******************************/
  67. private static byte[] Encrypt(byte[] data,string password)
  68. {
  69. SymmetricAlgorithm sa = Rijndael.Create();
  70. ICryptoTransform ct = sa.CreateEncryptor(
  71. (new PasswordDeriveBytes(password, null)).GetBytes(16),
  72. new byte[16]);
  73. MemoryStream ms = new MemoryStream();
  74. CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
  75. cs.Write(data, 0, data.Length);
  76. cs.FlushFinalBlock();
  77. return ms.ToArray();
  78. }
  79. private static string Encrypt(string data,string password)
  80. {
  81. try
  82. {
  83. return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(data),password));
  84. }
  85. catch { return "null"; }
  86. }
  87. private static byte[] Decrypt(byte[] data,string password)
  88. {
  89. BinaryReader br = new BinaryReader(InternalDecrypt(data,password));
  90. return br.ReadBytes((int)br.BaseStream.Length);
  91. }
  92. private static string Decrypt(string data,string password)
  93. {
  94. try
  95. {
  96. CryptoStream cs = InternalDecrypt(Convert.FromBase64String(data),password);
  97. StreamReader sr = new StreamReader(cs);
  98. return sr.ReadToEnd();
  99. }
  100. catch { return "null"; }
  101. }
  102. private static CryptoStream InternalDecrypt(byte[] data,string password)
  103. {
  104. SymmetricAlgorithm sa = Rijndael.Create();
  105. ICryptoTransform ct = sa.CreateDecryptor(
  106. (new PasswordDeriveBytes(password, null)).GetBytes(16),
  107. new byte[16]);
  108. MemoryStream ms = new MemoryStream(data);
  109. return new CryptoStream(ms, ct, CryptoStreamMode.Read);
  110. }
  111. /*****************************/
  112. internal static void ReplaceKeyEncrypt()
  113. {
  114. string email = EmailSetting.from_email();
  115. string smtp = EmailSetting.smtp();
  116. string loginemail = EmailSetting.login_email();
  117. string passwordemail = EmailSetting.pass_email();
  118. string server = CoreSetting.Server();
  119. string database = CoreSetting.DataBase();
  120. string user = CoreSetting.User();
  121. string password = CoreSetting.Password();
  122. string newkey=Guid.NewGuid().ToString();
  123. string _absolutDirectory = GeneralMethods.MytripConfigurationDirectory();
  124. XDocument _doc = XDocument.Load(_absolutDirectory);
  125. var _email = _doc.Root.Elements("email").Elements("add");
  126. _email.FirstOrDefault(x => x.Attribute("name").Value == "EmailAdress")
  127. .SetAttributeValue("value", Crypto.Encrypt(email, newkey));
  128. _email.FirstOrDefault(x => x.Attribute("name").Value == "Smtp")
  129. .SetAttributeValue("value", Crypto.Encrypt(smtp, newkey));
  130. _email.FirstOrDefault(x => x.Attribute("name").Value == "Login")
  131. .SetAttributeValue("value", Crypto.Encrypt(loginemail, newkey));
  132. _email.FirstOrDefault(x => x.Attribute("name").Value == "Password")
  133. .SetAttributeValue("value", Crypto.Encrypt(passwordemail, newkey));
  134. var connection = _doc.Root.Elements("connection").Elements("add");
  135. connection.FirstOrDefault(x => x.Attribute("name").Value == "DataBase")
  136. .SetAttributeValue("value", Crypto.Encrypt(database, newkey));
  137. connection.FirstOrDefault(x => x.Attribute("name").Value == "Password")
  138. .SetAttributeValue("value", Crypto.Encrypt(password, newkey));
  139. connection.FirstOrDefault(x => x.Attribute("name").Value == "Server")
  140. .SetAttributeValue("value", Crypto.Encrypt(server, newkey));
  141. connection.FirstOrDefault(x => x.Attribute("name").Value == "User")
  142. .SetAttributeValue("value", Crypto.Encrypt(user, newkey));
  143. _doc.Save(_absolutDirectory);
  144. string absolutDirectory = GeneralMethods.GetPath("Web.config");
  145. XDocument doc = XDocument.Load(absolutDirectory);
  146. var core = doc.Root.Elements("appSettings").Elements("add");
  147. core.FirstOrDefault(x => x.Attribute("key").Value == "applicationId")
  148. .SetAttributeValue("value", newkey);
  149. doc.Save(absolutDirectory);
  150. GeneralMethods.MytripCacheRemove("mtm_es_fromemail");
  151. GeneralMethods.MytripCacheRemove("mtm_es_smtp");
  152. GeneralMethods.MytripCacheRemove("mtm_es_user");
  153. GeneralMethods.MytripCacheRemove("mtm_es_password");
  154. GeneralMethods.MytripCacheRemove("mtm_cs_server");
  155. GeneralMethods.MytripCacheRemove("mtm_cs_database");
  156. GeneralMethods.MytripCacheRemove("mtm_cs_user");
  157. GeneralMethods.MytripCacheRemove("mtm_cs_password");
  158. }
  159. }
  160. }