<?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>Circles and Crosses &#187; encryption</title>
	<atom:link href="http://ox.no/posts/tag/encryption/feed" rel="self" type="application/rss+xml" />
	<link>http://ox.no</link>
	<description>Håvard Stranden&#039;s website</description>
	<lastBuildDate>Sat, 19 Jun 2010 21:59:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>RSA using BouncyCastle</title>
		<link>http://ox.no/posts/rsa-using-bouncycastle</link>
		<comments>http://ox.no/posts/rsa-using-bouncycastle#comments</comments>
		<pubDate>Thu, 19 Mar 2009 06:44:51 +0000</pubDate>
		<dc:creator>Håvard</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[decryption]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[rsa]]></category>

		<guid isPermaLink="false">http://ox.no/?p=80</guid>
		<description><![CDATA[Trying to do RSA using BouncyCastle, but struggling to find your way around the API? In a previous post (see here) I pondered why the RSA implementation in System.Security.Cryptography is restricted to only the most common usage scenarios. I mentioned &#8230; <a href="http://ox.no/posts/rsa-using-bouncycastle">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Trying to do RSA using BouncyCastle, but struggling to find your way around the API? In a previous post (see <a href="/posts/why-cripple-the-net-rsa-implementation">here</a>) I pondered why the RSA implementation in <code>System.Security.Cryptography</code> is restricted to only the most common usage scenarios. I mentioned <a href="http://bouncycastle.org">BouncyCastle</a> as an alternative for those who wanted a more flexible API, but never got around to providing examples where BouncyCastle was used. By request, this post provides usage examples by building a crude and simple, but efficient set of methods for RSA key generation, encryption, and decryption, all built on top of BouncyCastle.</p>

<!-- more -->

<p>BouncyCastle provides flexibility and control over your encryption approach, which comes at a cost. The BouncyCastle API might be a bit hard to cope with at first, but if you know encryption in general you should be able to find your way around the API without too much effort. This post will be focusing on RSA, since that was my original need, but it should be mentioned that BouncyCastle provides many other asymmetric (and symmetric) algorithms for which the usage is similar to what you find below.</p>

<h2>Creating RSA keys</h2>

<p>Creating RSA keys is a simple task. The method below lets you specify the key size in bits, and creates a key pair for you.</p>

<p><pre class="brush: csharp; ">

public AsymmetricCipherKeyPair GenerateKeys(int keySizeInBits)
{
  RsaKeyPairGenerator r = new RsaKeyPairGenerator();
  r.Init(new KeyGenerationParameters(new SecureRandom(),
    keySizeInBits));
  AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
  return keys;
}

</pre></p>

<p>That&#8217;s all there is to it.</p>

<h2>Encryption</h2>

<p>Now that we have a key pair, we are ready to encrypt and decrypt using RSA. In the example below, we use a key (public or private) to encrypt a byte sequence. To encrypt a string, simply convert the string to a byte array using <code>Encoding.GetBytes</code>.</p>

<p><pre class="brush: csharp; ">

public byte[] Encrypt(byte[] data, AsymmetricKeyParameter key)
{
  RsaEngine e = new RsaEngine();
  e.Init(true, key);&lt;/p&gt;

&lt;p&gt;int blockSize = e.GetInputBlockSize();&lt;/p&gt;

&lt;p&gt;List&lt;byte&gt; output = new List&lt;byte&gt;();&lt;/p&gt;

&lt;p&gt;for (int chunkPosition = 0; chunkPosition &amp;lt; data.Length; 
    chunkPosition += blockSize)
  {
    int chunkSize = Math.Min(blockSize, data.Length - 
      (chunkPosition * blockSize));
    output.AddRange(e.ProcessBlock(data, chunkPosition,
      chunkSize));
  }
  return output.ToArray();
}

</pre></p>

<p>The approach above uses a list to gather output for the sake of simplicity. Note that the RSA engine can only process a limited block size at a time (block size depends on the key size). The approach above generalizes the RSA engine, and processes a data set of an arbitrary size.</p>

<p>The above method does not impose constraints on which key you use for encryption. Use the public key or the private key as you see fit for your solution.</p>

<h2>Decryption</h2>

<p>The <code>Decrypt</code> method is very similar to the <code>Encrypt</code> method:</p>

<p><pre class="brush: csharp; ">

public byte[] Decrypt(byte[] data, AsymmetricKeyParameter key)
{
  RsaEngine e = new RsaEngine();
  e.Init(false, key);&lt;/p&gt;

&lt;p&gt;int blockSize = e.GetInputBlockSize();&lt;/p&gt;

&lt;p&gt;List&lt;byte&gt; output = new List&lt;byte&gt;();&lt;/p&gt;

&lt;p&gt;for (int chunkPosition = 0; chunkPosition &amp;lt; data.Length;
    chunkPosition += blockSize)
  {
    int chunkSize = Math.Min(blockSize, data.Length - 
      (chunkPosition * blockSize));
    output.AddRange(e.ProcessBlock(data, chunkPosition,
      chunkSize));
  }
  return output.ToArray();
}

</pre></p>

<p>Again, it&#8217;s up to you which key you choose to use. If you want to use the common approach, encrypt using a symmetric cipher, hash the data, and sign the hash with your private key using the above <code>Encrypt</code> method. If you want to use another approach like encrypting the actual data using your private key, you are free to do so.</p>

<p>I hope this post helps those of you who want to apply RSA (or any other asymmetric cipher) to more subtle cases than those supported by the .NET framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://ox.no/posts/rsa-using-bouncycastle/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>
