Data Protection with System.Security

An example (from Steve Gilham) of using ProtectedData class from the System.Security assembly.

import clr clr.AddReference("System.Security") from System.Security.Cryptography import * import System.Text import System

def getSecureValue(self, section, key, default): raw = self.getValue(section, key, None) if raw== None: return default try: array = System.Convert.FromBase64String(raw) chars = ProtectedData.Unprotect(array,       None, DataProtectionScope.CurrentUser) result = System.Text.Encoding.UTF8.GetString(chars) for i in range(chars.Length): chars[i] = 0 return result except System.Exception, ex: print ex.ToString return default

def setSecureValue(self, section, key, value): bytes = System.Text.Encoding.UTF8.GetBytes(value) try: safed = ProtectedData.Protect(bytes,       None, DataProtectionScope.CurrentUser) for i in range(bytes.Length): bytes[i] = 0 string = System.Convert.ToBase64String(safed) for i in range(safed.Length): safed[i] = 0 self.setValue(section, key, string) except System.Exception, ex: print ex.ToString

This code also works with Mono.

For other unrelated security concerns, please visit this website. There you'll find a variety of topics and products able to assist you or your business.

Back to Contents.