//Encryption Region public static string EncryptText(this string password, string input) { byte[] baPwd = Encoding.UTF8.GetBytes(password); // Hash the password with SHA256 byte[] baPwdHash = SHA256Managed.Create().ComputeHash(baPwd); byte[] baText = Encoding.UTF8.GetBytes(input); byte[] baSalt = GetRandomBytes(); byte[] baEncrypted = new byte[baSalt.Length + baText.Length]; // Combine Salt + Text for (int i = 0; i < baSalt.Length; i++) { baEncrypted[i] = baSalt[i]; } for (int i = 0; i < baText.Length; i++) { baEncrypted[i + baSalt.Length] = baText[i]; } baEncrypted = CoreEncrypt.AES_Encrypt(baEncrypted, baPwdHash); string result = Convert.ToBase64String(baEncrypted); return(result); }
public static string DecryptString(this string password, string text) { byte[] baPwd = Encoding.UTF8.GetBytes(password); // Hash the password with SHA256 byte[] baPwdHash = SHA256Managed.Create().ComputeHash(baPwd); byte[] baText = Convert.FromBase64String(text); byte[] baDecrypted = CoreEncrypt.AES_Decrypt(baText, baPwdHash); // Remove salt int saltLength = GetSaltLength(); byte[] baResult = new byte[baDecrypted.Length - saltLength]; for (int i = 0; i < baResult.Length; i++) { baResult[i] = baDecrypted[i + saltLength]; } string result = Encoding.UTF8.GetString(baResult); return(result); }