<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WPF &#187; Encryption</title>
	<atom:link href="http://www.wpfsharp.com/category/encryption/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wpfsharp.com</link>
	<description>WPF and C#</description>
	<lastBuildDate>Fri, 17 May 2013 03:36:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>How to store a password in an XML file encrypted so it is not in clear text or how to encrypt any textstring?</title>
		<link>http://www.wpfsharp.com/2009/10/23/how-to-store-a-password-in-an-xml-file-encrypted-so-it-is-not-in-clear-text-or-how-to-encrypt-any-textstring/</link>
		<comments>http://www.wpfsharp.com/2009/10/23/how-to-store-a-password-in-an-xml-file-encrypted-so-it-is-not-in-clear-text-or-how-to-encrypt-any-textstring/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 17:20:44 +0000</pubDate>
		<dc:creator>Rhyous</dc:creator>
				<category><![CDATA[C# (C-Sharp)]]></category>
		<category><![CDATA[Encryption]]></category>

		<guid isPermaLink="false">http://rhyous.wordpress.com/?p=194</guid>
		<description><![CDATA[Ok, so I have an application that needs to take a password and I need to remember that password. All the configuration is stored in XML, which is usually clear text. I want to store the password in the XML file, but I don&#8217;t want anyone to be able to open the XML file and [...]]]></description>
				<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.wpfsharp.com%2F2009%2F10%2F23%2Fhow-to-store-a-password-in-an-xml-file-encrypted-so-it-is-not-in-clear-text-or-how-to-encrypt-any-textstring%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://www.wpfsharp.com/2009/10/23/how-to-store-a-password-in-an-xml-file-encrypted-so-it-is-not-in-clear-text-or-how-to-encrypt-any-textstring/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.wpfsharp.com/2009/10/23/how-to-store-a-password-in-an-xml-file-encrypted-so-it-is-not-in-clear-text-or-how-to-encrypt-any-textstring/"  data-text="How to store a password in an XML file encrypted so it is not in clear text or how to encrypt any textstring?" data-count="horizontal"></a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://www.wpfsharp.com/2009/10/23/how-to-store-a-password-in-an-xml-file-encrypted-so-it-is-not-in-clear-text-or-how-to-encrypt-any-textstring/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://www.wpfsharp.com/2009/10/23/how-to-store-a-password-in-an-xml-file-encrypted-so-it-is-not-in-clear-text-or-how-to-encrypt-any-textstring/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>Ok, so I have an application that needs to take a password and I need to remember that password.  All the configuration is stored in XML, which is usually clear text.  I want to store the password in the XML file, but I don&#8217;t want anyone to be able to open the XML file and be able to see the password in clear text.</p>
<p>So here is what I going to do.</p>
<p>I create a class called <code>PasswordEncoder</code> that is going to use <code>DESCryptoServiceProvider</code>, which is a C# function.</p>
<p>I found a few examples online that helped me create this, such as Microsoft&#8217;s site that explains this object and another users blog that shows an example, which I used but only slightly modified.</p>
<p>http://msdn.microsoft.com/en-us/library/system.security.cryptography.descryptoserviceprovider.aspx</p>
<p>http://www.dotnetspider.com/resources/21370-Password-Encryption-using-C.aspx</p>
<p>So here is my source.  All you have to do is create your own class and copy in this code and you are ready to encrypt and decrypt passwords you store in XML.</p>
<p>Important! You must change the <code>mInitializationVector</code> and the <code>mByteArray</code> variable values to be your own values. Yes you can simply make up your own values.   We don&#8217;t want a everyone using  the same keys.</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace PasswordEncoder
{
    class PasswordEncoder
    {
        string mEncryptedPassword;
        // Change the two values below to be something other than the example.
        // Once changed and in use, do not change the value below again or you
        // won't be able to decrypt previously stored passwords.
        string mByteArray = &quot;%$#&gt;#%232s+as#l)URa0$!@&quot;;
        byte[] mInitializationVector = { 0x01, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xf7, 0xEF };

        public PasswordEncoder()
        {
        }

        public PasswordEncoder(string inPassword)
        {
            mEncryptedPassword = EncryptWithByteArray(inPassword, mByteArray);
        }

        public string EncryptWithByteArray(string inPassword)
        {
            mEncryptedPassword = EncryptWithByteArray(inPassword, mByteArray);
            return mEncryptedPassword;
        }

        private string EncryptWithByteArray(string inPassword, string inByteArray)
        {
            try
            {
                byte[] tmpKey = new byte[20];
                tmpKey = System.Text.Encoding.UTF8.GetBytes(inByteArray.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputArray = System.Text.Encoding.UTF8.GetBytes(inPassword);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(tmpKey, mInitializationVector), CryptoStreamMode.Write);
                cs.Write(inputArray, 0, inputArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string DecryptWithByteArray()
        {
            return DecryptWithByteArray(mEncryptedPassword, mByteArray);
        }

        private string DecryptWithByteArray(string strText, string strEncrypt)
        {
           try
           {
                byte[] tmpKey = new byte[20];
                tmpKey = System.Text.Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] inputByteArray = inputByteArray = Convert.FromBase64String(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(tmpKey, mInitializationVector), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string EncryptedPassword
        {
            get { return mEncryptedPassword; }
            set { mEncryptedPassword = value; }
        }

        public string ByteArray
        {
            get { return mByteArray; }
            set { mByteArray = value; }
        }
    }
}
</pre>
<p>Here is a simple sample of how to use this:</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PasswordEncoder
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write(&quot;Enter a password: &quot;);
            string password = Console.ReadLine();
            Console.WriteLine(&quot;You entered this password: &quot; + password);

            PasswordEncoder pe = new PasswordEncoder();
            string encryptedPassword = pe.EncryptWithByteArray(password);
            Console.WriteLine(&quot;Your encrypted password string: &quot; + encryptedPassword);

            string decryptedPassword = pe.DecryptWithByteArray();
            Console.WriteLine(&quot;Your decrypted password string: &quot; + decryptedPassword);

            if (password.Equals(decryptedPassword))
            {
                Console.WriteLine(&quot;Good work, your password was successfully encrypted then decrypted.&quot;);
            }
            else
            {
                Console.WriteLine(&quot;Uh...what did you do wrong, these don't match.&quot;);
            }
        }
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.wpfsharp.com/2009/10/23/how-to-store-a-password-in-an-xml-file-encrypted-so-it-is-not-in-clear-text-or-how-to-encrypt-any-textstring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
