using System; using System.Security.Cryptography; using System.Text; namespace D2Multi { static class D2AccountCrypto { static readonly byte[] Entropy = Encoding.UTF8.GetBytes("D2Multi.Accounts.v1"); public static string Protect(string plain) { if (string.IsNullOrEmpty(plain)) return string.Empty; var bytes = Encoding.UTF8.GetBytes(plain); var protectedBytes = ProtectedData.Protect(bytes, Entropy, DataProtectionScope.CurrentUser); return Convert.ToBase64String(protectedBytes); } public static string Unprotect(string base64Cipher) { if (string.IsNullOrEmpty(base64Cipher)) return string.Empty; try { var protectedBytes = Convert.FromBase64String(base64Cipher); var bytes = ProtectedData.Unprotect(protectedBytes, Entropy, DataProtectionScope.CurrentUser); return Encoding.UTF8.GetString(bytes); } catch { return string.Empty; } } } }