using System; using System.Text; using System.Web.Security; namespace atakanozbancom.Models.classes { public static class SecretProtector { private static readonly string[] Purposes = { "atakanozbancom.TotpSecret.v1" }; public static string Protect(string plaintext) { if (string.IsNullOrEmpty(plaintext)) return plaintext; var bytes = Encoding.UTF8.GetBytes(plaintext); var protectedBytes = MachineKey.Protect(bytes, Purposes); return Convert.ToBase64String(protectedBytes); } public static string Unprotect(string protectedValue) { if (string.IsNullOrEmpty(protectedValue)) return protectedValue; try { var protectedBytes = Convert.FromBase64String(protectedValue); var bytes = MachineKey.Unprotect(protectedBytes, Purposes); return bytes == null ? null : Encoding.UTF8.GetString(bytes); } catch { return null; } } public static bool LooksProtected(string value) { if (string.IsNullOrWhiteSpace(value)) return false; try { var bytes = Convert.FromBase64String(value); return bytes.Length > 16; } catch { return false; } } } }