<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: protection code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 10:39:56 GMT</pubDate>
    <description>DZone Snippets: protection code</description>
    <item>
      <title>Primitive run count application (demo) protection</title>
      <link>http://snippets.dzone.com/posts/show/3891</link>
      <description>&lt;code&gt;&lt;br /&gt;        public static int GetRunCount()&lt;br /&gt;        {&lt;br /&gt;            int count = 0;&lt;br /&gt;            string guid = "";&lt;br /&gt;            RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes\Software\Settings", true);&lt;br /&gt;&lt;br /&gt;            if (regKey != null)&lt;br /&gt;            {&lt;br /&gt;                string temp = (string)regKey.GetValue("Options");&lt;br /&gt;                guid = (string)regKey.GetValue("Guid");&lt;br /&gt;                count = (int)(temp[0] ^ guid[0]);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            return count;&lt;br /&gt;        }&lt;br /&gt;        public static int IncrementRun()&lt;br /&gt;        {&lt;br /&gt;            int count = 0;&lt;br /&gt;            string guid = "";&lt;br /&gt;            RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes\Software\Settings", true);&lt;br /&gt;&lt;br /&gt;            if (regKey == null)&lt;br /&gt;            {&lt;br /&gt;                regKey = Registry.CurrentUser.CreateSubKey(@"Software\Classes\Software\Settings");&lt;br /&gt;                guid = Guid.NewGuid().ToString();&lt;br /&gt;                regKey.SetValue("Guid", guid);&lt;br /&gt;                count = 0;&lt;br /&gt;            }&lt;br /&gt;            else&lt;br /&gt;            {&lt;br /&gt;                string temp = (string)regKey.GetValue("Options");&lt;br /&gt;                guid = (string)regKey.GetValue("Guid");&lt;br /&gt;                count = (int)(temp[0] ^ guid[0]);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            Random rnd = new Random();&lt;br /&gt;            count++;&lt;br /&gt;            string value = string.Format("{0}{1}", (char)(guid[0] ^ count), GenerateGarbage(15));&lt;br /&gt;&lt;br /&gt;            regKey.SetValue("Options", value);&lt;br /&gt;&lt;br /&gt;            return count;&lt;br /&gt;        }&lt;br /&gt;        private static string GenerateGarbage(int length)&lt;br /&gt;        {&lt;br /&gt;            string retVal;&lt;br /&gt;            if (length &lt; 0)&lt;br /&gt;                retVal = null;&lt;br /&gt;            else if (length == 0)&lt;br /&gt;                retVal = "";&lt;br /&gt;            else&lt;br /&gt;            {&lt;br /&gt;                Random rnd = new Random();&lt;br /&gt;                StringBuilder str = new StringBuilder();&lt;br /&gt;&lt;br /&gt;                for (int i = 0; i &lt; length; i++)&lt;br /&gt;                    str.Append((char)rnd.Next(33, 126));&lt;br /&gt;                retVal = str.ToString();&lt;br /&gt;            }&lt;br /&gt;            return retVal;&lt;br /&gt;        }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 24 Apr 2007 22:43:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3891</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>Protect string content by marshalling</title>
      <link>http://snippets.dzone.com/posts/show/3884</link>
      <description>&lt;code&gt;&lt;br /&gt;IntPtr bstr = Marshal.SecureStringToBSTR(password);&lt;br /&gt;    &lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;    // ...&lt;br /&gt;    // use the bstr&lt;br /&gt;    // ...&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;    Marshal.ZeroFreeBSTR(bstr);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 24 Apr 2007 22:35:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3884</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
  </channel>
</rss>
