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