using System; using System.Diagnostics.Contracts; using System.IO; using System.IO.Compression; using System.Linq; using System.Security.Cryptography; using System.Text; namespace DominionBase.Utilities { public static class StringUtility { private const string PasswordHash = "D0m1n10NdotNET"; private const string SaltKey = "23987dfgsd89073"; private const string ViKey = "90sd8XKJd8&5dskj"; public static string Plural(string value, int count) { return $"{count} {Plural(value, count, false)}"; } public static string Plural(string value, int count, bool includeCount) { Contract.Requires(value != null, "value cannot be null"); if (includeCount) return Plural(value, count); if (count == 1 || count == -1) return value; // These are general rules and will require quite a few exceptions I'm sure. It will also need to be // refactored when "proper" multilingual support is added if (value[value.Length - 1] == 'y' && !"aeiou".Contains(value[value.Length - 2])) return $"{value.Substring(0, value.Length - 1)}ies"; if (value.EndsWith("o", StringComparison.InvariantCulture)) return $"{value}es"; return $"{value}s"; } public static string Encrypt(string plainText) { var plainTextBytes = Encoding.UTF8.GetBytes(plainText); using (var rfc2898DeriveBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey))) using (var symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros }) { var keyBytes = rfc2898DeriveBytes.GetBytes(256 / 8); var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(ViKey)); byte[] cipherTextBytes; using (var memoryStream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); cipherTextBytes = memoryStream.ToArray(); cryptoStream.Close(); } memoryStream.Close(); } return Convert.ToBase64String(cipherTextBytes); } } public static string Decrypt(string encryptedText) { var cipherTextBytes = Convert.FromBase64String(encryptedText); using (var rfc2898DeriveBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey))) using (var symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC, Padding = PaddingMode.None }) { var keyBytes = rfc2898DeriveBytes.GetBytes(256 / 8); var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(ViKey)); var memoryStream = new MemoryStream(cipherTextBytes); var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); var plainTextBytes = new byte[cipherTextBytes.Length]; var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); memoryStream.Close(); cryptoStream.Close(); return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray()); } } public static byte[] Zip(string str) { var bytes = Encoding.UTF8.GetBytes(str); using (var msi = new MemoryStream(bytes)) using (var mso = new MemoryStream()) { using (var gs = new GZipStream(mso, CompressionMode.Compress)) msi.CopyTo(gs); return mso.ToArray(); } } public static string Unzip(byte[] bytes) { using (var msi = new MemoryStream(bytes)) using (var mso = new MemoryStream()) { using (var gs = new GZipStream(msi, CompressionMode.Decompress)) gs.CopyTo(mso); return Encoding.UTF8.GetString(mso.ToArray()); } } } }