SimpleStringCipher_NetStandard.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. //This code is got from http://stackoverflow.com/questions/10168240/encrypting-decrypting-a-string-in-c-sharp
  6. namespace Abp.Runtime.Security
  7. {
  8. /// <summary>
  9. /// Can be used to simply encrypt/decrypt texts.
  10. /// </summary>
  11. public class SimpleStringCipher
  12. {
  13. public static SimpleStringCipher Instance { get; }
  14. /// <summary>
  15. /// This constant string is used as a "salt" value for the PasswordDeriveBytes function calls.
  16. /// This size of the IV (in bytes) must = (keysize / 8). Default keysize is 256, so the IV must be
  17. /// 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array.
  18. /// </summary>
  19. public byte[] InitVectorBytes;
  20. /// <summary>
  21. /// Default password to encrypt/decrypt texts.
  22. /// It's recommented to set to another value for security.
  23. /// Default value: "gsKnGZ041HLL4IM8"
  24. /// </summary>
  25. public static string DefaultPassPhrase { get; set; }
  26. /// <summary>
  27. /// Default value: Encoding.ASCII.GetBytes("jkE49230Tf093b42")
  28. /// </summary>
  29. public static byte[] DefaultInitVectorBytes { get; set; }
  30. /// <summary>
  31. /// Default value: Encoding.ASCII.GetBytes("hgt!16kl")
  32. /// </summary>
  33. public static byte[] DefaultSalt { get; set; }
  34. /// <summary>
  35. /// This constant is used to determine the keysize of the encryption algorithm.
  36. /// </summary>
  37. public const int Keysize = 256;
  38. static SimpleStringCipher()
  39. {
  40. DefaultPassPhrase = "gsKnGZ041HLL4IM8";
  41. DefaultInitVectorBytes = Encoding.ASCII.GetBytes("jkE49230Tf093b42");
  42. DefaultSalt = Encoding.ASCII.GetBytes("hgt!16kl");
  43. Instance = new SimpleStringCipher();
  44. }
  45. public SimpleStringCipher()
  46. {
  47. InitVectorBytes = DefaultInitVectorBytes;
  48. }
  49. public string Encrypt(string plainText, string passPhrase = null, byte[] salt = null)
  50. {
  51. if (plainText == null)
  52. {
  53. return null;
  54. }
  55. if (passPhrase == null)
  56. {
  57. passPhrase = DefaultPassPhrase;
  58. }
  59. if (salt == null)
  60. {
  61. salt = DefaultSalt;
  62. }
  63. var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  64. using (var password = new Rfc2898DeriveBytes(passPhrase, salt))
  65. {
  66. var keyBytes = password.GetBytes(Keysize / 8);
  67. using (var symmetricKey = Aes.Create())
  68. {
  69. symmetricKey.Mode = CipherMode.CBC;
  70. using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, InitVectorBytes))
  71. {
  72. using (var memoryStream = new MemoryStream())
  73. {
  74. using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
  75. {
  76. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
  77. cryptoStream.FlushFinalBlock();
  78. var cipherTextBytes = memoryStream.ToArray();
  79. return Convert.ToBase64String(cipherTextBytes);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. public string Decrypt(string cipherText, string passPhrase = null, byte[] salt = null)
  87. {
  88. if (string.IsNullOrEmpty(cipherText))
  89. {
  90. return null;
  91. }
  92. if (passPhrase == null)
  93. {
  94. passPhrase = DefaultPassPhrase;
  95. }
  96. if (salt == null)
  97. {
  98. salt = DefaultSalt;
  99. }
  100. var cipherTextBytes = Convert.FromBase64String(cipherText);
  101. using (var password = new Rfc2898DeriveBytes(passPhrase, salt))
  102. {
  103. var keyBytes = password.GetBytes(Keysize / 8);
  104. using (var symmetricKey = Aes.Create())
  105. {
  106. symmetricKey.Mode = CipherMode.CBC;
  107. using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, InitVectorBytes))
  108. {
  109. using (var memoryStream = new MemoryStream(cipherTextBytes))
  110. {
  111. using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
  112. {
  113. var plainTextBytes = new byte[cipherTextBytes.Length];
  114. var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
  115. return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }