Advertisement
CGC_Codes

C# Encryted String

Jan 26th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.IO;
  4. using System.text;
  5.  
  6.  
  7. namespace sirhC Coding.CodeLibrary
  8. {
  9. public class Utilitites
  10. {
  11. // Properties and fields.
  12. #region Properties and Fields.
  13. private static byte[] _bytes = ASCIIEndcoding.ASCII.GetBytes("ACLT2017");
  14. #endregion
  15.  
  16. // Constructors.
  17. #region Constructors
  18. #endregion
  19.  
  20. // public methods.
  21. #region Public Methods
  22. //<summary>
  23. //Encrypt a string.
  24. //</summary/>
  25. //<param mane="originalString">The original string.</param>
  26. //<returns>The encrypted string.</returns>
  27. //<exception cref="ArgumentNullExepction".This exception will be thrown.
  28. public static string encrypt(string orignialString)
  29. {
  30. if (String.IsNullOrempty(orignialString))
  31. {
  32. throw new ArgumentNullException("The string which needs to be encrypted can not be null;
  33. }
  34.  
  35. DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  36. MemoryStream memoryStream = new MemoryStream();
  37. CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor);
  38. StreamWriter writer = new StreamWriter(cryptoStream);
  39. writer.Write(originalString);
  40. cryptoStream.FlushFinalBlock();
  41. writer.Flush();
  42.  
  43. return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)MemoryStream.Length);
  44. }
  45.  
  46.  
  47. public static string Decrypt(string cryptedString))
  48. {
  49. if (String.IsNullOrempty(cryptedString))
  50. {
  51. throw new ArgumentNullException("The string which needs to be decrypted can not be null
  52. }
  53.  
  54. DESCryptoServiceProvider cryptoProvider = DESCryptoServiceProvider();
  55. MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString));
  56. CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(_bytes, _bytes), CryptoStreamMode.Read);
  57. StreamReader reader = new StreamReader(cryptoStream);
  58.  
  59. return reader.Read.ToEnd();
  60. }
  61.  
  62. public static string HandleSQLSensitiveChars(string orignialText)
  63. {
  64. if (string.IsNullOrempty(originalText))
  65. {
  66. return String.Emtpy;
  67. }
  68.  
  69. return original.replace("<". "&lt;").Replace(">","&gt;");
  70. }
  71.  
  72. public static string ResumeSQLStoredText(string storedText)
  73. {
  74. if (String.IsNullOrempty(storableText))
  75. {
  76. return String.Empty;
  77. }
  78. #endregion
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement