using System; using System.Security.Cryptography; using System.Text; namespace AyalaLauncherBeta2016 { public static class DPAPI { public static string Protect( this string clearText, string optionalEntropy = null, DataProtectionScope scope = DataProtectionScope.LocalMachine) { if (clearText == null) throw new ArgumentNullException("clearText"); byte[] clearBytes = Encoding.UTF8.GetBytes(clearText); byte[] entropyBytes = string.IsNullOrEmpty(optionalEntropy) ? null : Encoding.UTF8.GetBytes(optionalEntropy); byte[] encryptedBytes = ProtectedData.Protect(clearBytes, entropyBytes, scope); return Convert.ToBase64String(encryptedBytes); } public static string Protect2(string stringToEncrypt, string optionalEntropy, DataProtectionScope scope) { return Convert.ToBase64String( ProtectedData.Protect( Encoding.UTF8.GetBytes(stringToEncrypt) , optionalEntropy != null ? Encoding.UTF8.GetBytes(optionalEntropy) : null , scope)); } public static string Unprotect2(string encryptedString, string optionalEntropy, DataProtectionScope scope) { return Encoding.UTF8.GetString( ProtectedData.Unprotect( Convert.FromBase64String(encryptedString) , optionalEntropy != null ? Encoding.UTF8.GetBytes(optionalEntropy) : null , scope)); } public static string Unprotect( this string encryptedText, string optionalEntropy = null, DataProtectionScope scope = DataProtectionScope.LocalMachine) { if (encryptedText == null) throw new ArgumentNullException("encryptedText"); byte[] encryptedBytes = Convert.FromBase64String(encryptedText); byte[] entropyBytes = string.IsNullOrEmpty(optionalEntropy) ? null : Encoding.UTF8.GetBytes(optionalEntropy); byte[] clearBytes = ProtectedData.Unprotect(encryptedBytes, entropyBytes, scope); return Encoding.UTF8.GetString(clearBytes); } } }