private DataProtectionScope ToDataProtectionScope(PasswordProtectionScope scope) { switch (scope) { case PasswordProtectionScope.CurrentUser: return DataProtectionScope.CurrentUser; case PasswordProtectionScope.LocalMachine: return DataProtectionScope.LocalMachine; } return DataProtectionScope.CurrentUser; }
public string Protect(string password, PasswordProtectionScope scope) { using (var memoryStream = new MemoryStream()) { var toEncrypt = Encoding.UTF8.GetBytes(password); var entropy = _protector.CreateRandomEntropy(); memoryStream.Write(entropy, 0, entropy.Length); memoryStream.WriteByte((byte)scope); _protector.EncryptDataToStream(toEncrypt, entropy, ToDataProtectionScope(scope), memoryStream); var bytes = memoryStream.ToArray(); return Convert.ToBase64String(bytes); } }