Data Protection with System.Security

From IronPython Cookbook

(Difference between revisions)
Line 43: Line 43:
This code also works with Mono.
This code also works with Mono.
 +
 +
For other unrelated [http://www.123cctv.com/ security concerns, please visit this website]. There you'll find a variety of [http://www.educause.edu/focus-areas-and-initiatives/policy-and-security/cybersecurity-initiative/community-engagement/information-security-awareness- topics and products] able to assist you or your business.
Back to [[Contents]].
Back to [[Contents]].

Revision as of 22:38, 19 July 2013

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.

TOOLBOX
LANGUAGES