<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Deconstructing Subtext</title>
        <link>http://www.sharpdeveloper.net/content/category/10.aspx</link>
        <description>Subtext is a fork of the .TEXT blogging application, and is the software that we are running.  We intend to regularly put articles here explaining how the actual code runs and how you can re-use some of the code in your own application.</description>
        <language>en-US</language>
        <copyright>Sameer Alibhai</copyright>
        <managingEditor>abdullah.adam@gmail.com</managingEditor>
        <generator>Subtext Version 1.9.5.176</generator>
        <item>
            <title>Encryption and Decryption for Dummies in .NET</title>
            <link>http://sharpdeveloper.net/content/archive/2007/06/27/encryption-for-dummies-in-.net.aspx</link>
            <description>&lt;p&gt;I was trying to figure out the easiest way that I could encrypt some text for the application I was working on. I struggled for a bit to figure out what was the best way to perform encryption in .NET.  Having a piece of open source software that you can look at is very helpful, and I highly recommend it.  You will frequently run into problems that have been solved by others time and time again, and instead of trying to re-invent the wheel, it would be in your best interests to have this toolbelt of code that you are familiar with, and that you can look up.&lt;/p&gt;
&lt;p&gt;The software that this site runs on is called Subtext.  You can download the &lt;a href="http://downloads.sourceforge.net/subtext/SubText-1.9.5-source.zip?modtime=1178844138&amp;amp;big_mirror=0"&gt;source code&lt;/a&gt; or the &lt;a href="http://downloads.sourceforge.net/subtext/SubText-1.9.5-INSTALL.zip?modtime=1178788468&amp;amp;big_mirror=0"&gt;compiled version&lt;/a&gt; and take a look inside at the source code to get some ideas and learn from the experience of those &lt;a href="http://www.haacked.com/archive/2007/06/25/understanding-productivity-differences-between-developers.aspx"&gt;more experienced&lt;/a&gt; than you and I.&lt;/p&gt;
&lt;p&gt;Encryption is a massive topic and I am not even going to scratch the surface of it, but the code that follows is a simple example of how you can use it.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
This code demonstrates a very easy way to Encrypt and Decrypt using &lt;a href="http://en.wikipedia.org/wiki/Advanced_Encryption_Standard"&gt;Rinjdael's (AES) algorithm&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It first creates an instance of the algorithm, followed by a method that uses our instance to Encrypt, and another that uses our instance to Decrypt.  &lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; SymmetricAlgorithm encryptionAlgorithm = InitializeEncryptionAlgorithm();
        
        &lt;span class="kwrd"&gt;static&lt;/span&gt; SymmetricAlgorithm InitializeEncryptionAlgorithm()
        {
            SymmetricAlgorithm rijaendel = RijndaelManaged.Create();
            rijaendel.GenerateKey();
            rijaendel.GenerateIV();
            &lt;span class="kwrd"&gt;return&lt;/span&gt; rijaendel;
        }

        &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// Encrypts the string and returns a base64 encoded encrypted string.&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;param name="clearText"&amp;gt;The clear text.&amp;lt;/param&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; EncryptString(&lt;span class="kwrd"&gt;string&lt;/span&gt; clearText)
        {
            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] clearTextBytes = Encoding.UTF8.GetBytes(clearText);
            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] encrypted = encryptionAlgorithm.CreateEncryptor().TransformFinalBlock(clearTextBytes, 0, clearTextBytes.Length);
            &lt;span class="kwrd"&gt;return&lt;/span&gt; Convert.ToBase64String(encrypted);
        }

        &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// Decrypts the base64 encrypted string and returns the cleartext.&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;param name="encryptedEncodedText"&amp;gt;The clear text.&amp;lt;/param&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;exception type="System.Security.Cryptography.CryptographicException"&amp;gt;Thrown the string to be decrypted &lt;/span&gt;
        &lt;span class="rem"&gt;/// was encrypted using a different encryptor (for example, if we recompile and &lt;/span&gt;
        &lt;span class="rem"&gt;/// receive an old string).&amp;lt;/exception&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; DecryptString(&lt;span class="kwrd"&gt;string&lt;/span&gt; encryptedEncodedText)
        {
            &lt;span class="kwrd"&gt;try&lt;/span&gt;
            {
                &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] encryptedBytes = Convert.FromBase64String(encryptedEncodedText);
                &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] decryptedBytes = encryptionAlgorithm.CreateDecryptor().TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
                &lt;span class="kwrd"&gt;return&lt;/span&gt; Encoding.UTF8.GetString(decryptedBytes);
            }
            &lt;span class="kwrd"&gt;catch&lt;/span&gt; (FormatException fe)
            {
                &lt;span class="rem"&gt;//throw new CaptchaExpiredException("Encrypted encoded text '" + encryptedEncodedText + "' was not valid.", fe);&lt;/span&gt;
            }
            &lt;span class="kwrd"&gt;catch&lt;/span&gt; (CryptographicException e)
            {
                &lt;span class="rem"&gt;//throw new CaptchaExpiredException("Captcha image expired, probably due to recompile making the key out of synch.", e);&lt;/span&gt;
            }
        }&lt;/pre&gt;
&lt;p&gt;As you can see, all you need is a few methods like this, and you have .NET encryption.  To encrypt data, simply call EncryptString("your string"), and to decrypt data, simply call DecryptString(encryptedString).  I commented out the exceptions, you will need to re-implement them as you like.  Or you can just leave it crashing ;)&lt;/p&gt;
&lt;p&gt;Source from &lt;font face="Arial"&gt;&lt;strong&gt;&lt;a href="http://subtext.svn.sourceforge.net/viewvc/subtext/trunk/SubtextSolution/Subtext.Web.Controls/Captcha/CaptchaBase.cs?view=markup&amp;amp;pathrev=2716"&gt;SubText-1.9.5-source\Subtext.Web.Controls\Captcha\CaptchaBase.cs&lt;/a&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This code is licensed with the new &lt;a href="http://www.opensource.org/licenses/bsd-license.php"&gt;BSD template&lt;/a&gt; - Open Source baby!&lt;/p&gt;
&lt;p&gt;Update (July 10, 2007)&lt;br /&gt;
This method will create a random key every time the application restarts, which may not be what you want.  If this is the case, you can hardcode the key and IV values so that it will always be the same:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;        rijaendel.Key = Convert.FromBase64String(&lt;span class="str"&gt;"HUfIj72qL4OnPu1OlMBKqoufdLSw/nOsIrJiSr+lRgg="&lt;/span&gt;);
        rijaendel.IV = Convert.FromBase64String(&lt;span class="str"&gt;"p1D5hkd4xa5kyX6O7ZXR2A=="&lt;/span&gt;);
&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;How did I get these values?  You don't want to use these, but you can generate these values using the built in method above (&lt;font face="Arial"&gt;InitializeEncryptionAlgorithm)&lt;/font&gt;, and then store it as a string by running Convert.ToBase64String(keyValue)&lt;/p&gt;
&lt;p&gt;Read "&lt;a href="http://haacked.com/archive/2006/10/02/Better_CAPTCHA_Through_Encryption.aspx"&gt;Better Captcha Through Encryption&lt;/a&gt;" for more details on how this code was implemented.&lt;/p&gt;
&lt;p&gt;Another option is to set your &lt;a href="http://www.codinghorror.com/blog/archives/000132.html"&gt;machineKey value&lt;/a&gt; in your web.config&lt;/p&gt;&lt;img src="http://sharpdeveloper.net/content/aggbug/34.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Sameer Alibhai, Ashiq Alibhai</dc:creator>
            <guid>http://sharpdeveloper.net/content/archive/2007/06/27/encryption-for-dummies-in-.net.aspx</guid>
            <pubDate>Wed, 27 Jun 2007 22:21:03 GMT</pubDate>
            <wfw:comment>http://sharpdeveloper.net/content/comments/34.aspx</wfw:comment>
            <comments>http://sharpdeveloper.net/content/archive/2007/06/27/encryption-for-dummies-in-.net.aspx#feedback</comments>
            <wfw:commentRss>http://sharpdeveloper.net/content/comments/commentRss/34.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>