<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" 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:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9 Forums - Tech Off - Using TripleDES to encrypt 64 bits, and only 64 bits</title>
	<atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Forums/rss"></atom:link>
	<image>
		<url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
		<title>Channel 9 Forums - Tech Off - Using TripleDES to encrypt 64 bits, and only 64 bits</title>
		<link>http://channel9.msdn.com/Forums</link>
	</image>
	<description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
	<link>http://channel9.msdn.com/Forums</link>
	<language>en</language>
	<pubDate>Sat, 25 May 2013 10:59:23 GMT</pubDate>
	<lastBuildDate>Sat, 25 May 2013 10:59:23 GMT</lastBuildDate>
	<generator>Rev9</generator>
	<c9:totalResults>7</c9:totalResults>
	<c9:pageCount>-7</c9:pageCount>
	<c9:pageSize>-1</c9:pageSize>
	<item>
		<title>Tech Off - Using TripleDES to encrypt 64 bits, and only 64 bits</title>
		<description><![CDATA[<p>I can't seem to figure out how to use the DES or TripleDES classes in .NET. I want to encrypt 64 bits (such that a 64-bit block is returned) but whilst I can get it to encrypt data, it either doesn't decrypt it (in the first example below), or returns 128 bits instead (second example). The third example just causes an exception.</p><p><pre class="brush: csharp">using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace EncTest {
    
    public class Program {
        
        private static Int32 _blockSize;
        
        public static void Main(String[] args) {
            
            SymmetricAlgorithm algo = 
//                DES.Create();
//                Rijndael.Create();
                TripleDES.Create();
            
            Console.WriteLine(&quot;Using: {0}&quot;, algo.GetType().Name );
            
            algo.GenerateKey();
            algo.GenerateIV();
            
            Console.WriteLine(&quot;Key: {0}\nIV: {1}&quot;, Convert.ToBase64String( algo.Key ), Convert.ToBase64String( algo.IV ) );
            
            _blockSize = algo.LegalBlockSizes[0].MaxSize / 8;
            
            Console.WriteLine(&quot;Encrypt this:&quot;);
            Byte[] inputBytes = GetBlock(_blockSize);
            
            Encrypt1( algo, inputBytes ); Console.WriteLine(&quot;----------------&quot;);
            Encrypt2( algo, inputBytes ); Console.WriteLine(&quot;----------------&quot;);
            Encrypt3( algo, inputBytes );
            
            Console.ReadLine();
        }
        
        private static void Encrypt1(SymmetricAlgorithm algo, Byte[] clearInput) {
            
            ICryptoTransform enc = algo.CreateEncryptor();
            ICryptoTransform dec = algo.CreateDecryptor();
            
            Byte[] encryptedBytes = new Byte[_blockSize];
            int m = enc.TransformBlock( clearInput, 0, clearInput.Length, encryptedBytes, 0 );
            Console.WriteLine(&quot;Encrypted in {0,2} bytes as \&quot;{1}\&quot;&quot;, m, Convert.ToBase64String( encryptedBytes ) );
            
            Byte[] decryptedBytes = new Byte[_blockSize];
            m = dec.TransformBlock( encryptedBytes, 0, encryptedBytes.Length, decryptedBytes, 0 );
            Console.WriteLine(&quot;Decrypted in {0,2} bytes as \&quot;{1}\&quot;&quot;, m, Encoding.UTF8.GetString( decryptedBytes ) );
        }
        
        private static void Encrypt2(SymmetricAlgorithm algo, Byte[] clearInput) {
            
            ICryptoTransform enc = algo.CreateEncryptor();
            ICryptoTransform dec = algo.CreateDecryptor();
            
            Byte[] encryptedBytes = enc.TransformFinalBlock( clearInput, 0, clearInput.Length );
            
            Console.WriteLine(&quot;Encrypted in {0,2} bytes as \&quot;{1}\&quot;&quot;, encryptedBytes.Length, Convert.ToBase64String( encryptedBytes ) );
            
            Byte[] decryptedBytes = dec.TransformFinalBlock( encryptedBytes, 0, encryptedBytes.Length );
            
            Console.WriteLine(&quot;Decrypted in {0,2} bytes as \&quot;{1}\&quot;&quot;, decryptedBytes.Length, Encoding.UTF8.GetString( decryptedBytes ) );
        }
        
        private static void Encrypt3(SymmetricAlgorithm algo, Byte[] clearInput) {
            
            ICryptoTransform enc = algo.CreateEncryptor();
            ICryptoTransform dec = algo.CreateDecryptor();
            
            MemoryStream ms  = new MemoryStream();
            CryptoStream csw = new CryptoStream( ms, enc, CryptoStreamMode.Write );
            
            csw.Write( clearInput, 0, clearInput.Length );
            csw.Flush(); // at this point, 'ms' reports 8 bytes were written
            csw.Close();
            
            Byte[] encdata = ms.ToArray();
            ms = new MemoryStream( encdata );
            
            Console.WriteLine(&quot;Encrypted in {0,2} bytes as \&quot;{1}\&quot;&quot;, encdata.Length, Convert.ToBase64String( encdata ) );
            
            Byte[] decryptedBytes = new Byte[ _blockSize ];
            
            CryptoStream csr = new CryptoStream( ms, dec, CryptoStreamMode.Read );
            int br = csr.Read( decryptedBytes, 0, decryptedBytes.Length ); // TODO: &quot;Bad Data&quot; exception thrown here
            csr.Close();
            
            Console.WriteLine(&quot;Decrypted in {0,2} bytes as \&quot;{1}\&quot; ({2} bytes read)&quot;, decryptedBytes.Length, Encoding.UTF8.GetString( decryptedBytes ), br );
        }
        
        private static Byte[] GetBlock(int blockSize) {
            
            while(true) {
                
                String input = Console.ReadLine();
                Byte[] inputBytes = Encoding.UTF8.GetBytes( input );
                if( inputBytes.Length &gt; blockSize ) continue;
                
                Byte[] ret = new Byte[blockSize];
                Array.Copy( inputBytes, ret, input.Length );
                return ret;
            }
            
        }
        
    }
}
</pre></p><p>This code should compile with .NET 2.0 or later (maybe even 1.1 too). Can anyone tell me where I'm going wrong?</p><p>Ta</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/a9332f3b61ef4a06acd79f2b011d0adb#a9332f3b61ef4a06acd79f2b011d0adb</link>
		<pubDate>Mon, 25 Jul 2011 17:17:48 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/a9332f3b61ef4a06acd79f2b011d0adb#a9332f3b61ef4a06acd79f2b011d0adb</guid>
		<dc:creator>W3bbo</dc:creator>
		<slash:comments>7</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/W3bbo/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - Using TripleDES to encrypt 64 bits, and only 64 bits</title>
		<description><![CDATA[<p>The first case doesn't work because you don't call TransformFinalBlock.</p><p>The second case works fine for me,&nbsp;I get 8 bytes, that is 64 bits.</p><p>The third case doesn't work because you flush the write stream but you should close it instead.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/14a47592e7e348fe93e39f2b012babbf#14a47592e7e348fe93e39f2b012babbf</link>
		<pubDate>Mon, 25 Jul 2011 18:11:04 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/14a47592e7e348fe93e39f2b012babbf#14a47592e7e348fe93e39f2b012babbf</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>7</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - Using TripleDES to encrypt 64 bits, and only 64 bits</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/14a47592e7e348fe93e39f2b012babbf">6 minutes&nbsp;ago</a>,<a href="/Niners/Dexter">Dexter</a> wrote</p><p>The first case doesn't work because you don't call TransformFinalBlock.</p><p>The second case works fine for me,&nbsp;I get 8 bytes, that is 64 bits.</p><p>The third case doesn't work because you flush the write stream but you should close it instead.</p><p></p></div></blockquote><p></p><p>I fixed the bug in the third case.</p><p>But when I run it I get 16 bytes (128 buts) for both the second and third cases.</p><p>&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/883a1a207eef4448aee69f2b012fbf24#883a1a207eef4448aee69f2b012fbf24</link>
		<pubDate>Mon, 25 Jul 2011 18:25:54 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/883a1a207eef4448aee69f2b012fbf24#883a1a207eef4448aee69f2b012fbf24</guid>
		<dc:creator>W3bbo</dc:creator>
		<slash:comments>7</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/W3bbo/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - Using TripleDES to encrypt 64 bits, and only 64 bits</title>
		<description><![CDATA[<p>Well, I tested again, this time on .NET 2 (previously I used 4).&nbsp;I get 8 bytes in both cases.</p><p>How do you know how many bytes have been returned? Your sample code doesn't show that and in the 3rd cases it's impossible to get more than 8 bytes unless the block size is&nbsp;&gt; 8.</p><p>Btw, in the 3rd case you should check the value returned by Read.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/a06a8165ee9c4ddba6169f2b013395bb#a06a8165ee9c4ddba6169f2b013395bb</link>
		<pubDate>Mon, 25 Jul 2011 18:39:52 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/a06a8165ee9c4ddba6169f2b013395bb#a06a8165ee9c4ddba6169f2b013395bb</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>7</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - Using TripleDES to encrypt 64 bits, and only 64 bits</title>
		<description><![CDATA[<p>@<a href="/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits#ca06a8165ee9c4ddba6169f2b013395bb">Dexter</a>:Weird.</p><p>I've updated my OP with the newer source code. When I run it I get these results in console:</p><pre>Using: TripleDESCryptoServiceProvider<br>Key: I7Oahhupbb64UE/M&#43;ex7r/JG08GXXIdd<br>IV: nXDMTEnbbj8=<br>Encrypt this:<br>123<br>Encrypted in  8 bytes as &quot;IrhKcDMCixU=&quot;<br>Decrypted in  0 bytes as &quot;        &quot;<br>----------------<br>Encrypted in 16 bytes as &quot;IrhKcDMCixXFhW3UMfsnIA==&quot;<br>Decrypted in  8 bytes as &quot;123     &quot;<br>----------------<br>Encrypted in 16 bytes as &quot;IrhKcDMCixXFhW3UMfsnIA==&quot;<br>Decrypted in  8 bytes as &quot;123     &quot; (8 bytes read)</pre>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/8d6f089f1a3f48e7bcfc9f2b0137eb25#8d6f089f1a3f48e7bcfc9f2b0137eb25</link>
		<pubDate>Mon, 25 Jul 2011 18:55:39 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/8d6f089f1a3f48e7bcfc9f2b0137eb25#8d6f089f1a3f48e7bcfc9f2b0137eb25</guid>
		<dc:creator>W3bbo</dc:creator>
		<slash:comments>7</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/W3bbo/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - Using TripleDES to encrypt 64 bits, and only 64 bits</title>
		<description><![CDATA[<p>Yay, you were talking about encrypted size and I was talking about the decrypted size. That's because of the padding. If you set PaddingMode to None you'll get 8 bytes if you really need that.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/5f967c6c67fa47deb8049f2b01417e73#5f967c6c67fa47deb8049f2b01417e73</link>
		<pubDate>Mon, 25 Jul 2011 19:30:31 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/5f967c6c67fa47deb8049f2b01417e73#5f967c6c67fa47deb8049f2b01417e73</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>7</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - Using TripleDES to encrypt 64 bits, and only 64 bits</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/5f967c6c67fa47deb8049f2b01417e73">2 minutes&nbsp;ago</a>,<a href="/Niners/Dexter">Dexter</a> wrote</p><p>Yay, you were talking about encrypted size and I was talking about the decrypted size. That's because of the padding. If you set PaddingMode to None you'll get 8 bytes if you really need that.</p><p></p></div></blockquote><p></p><p>huzzah, thank you!</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/5abe6ac3666e4ecdbb159f2b014c7919#5abe6ac3666e4ecdbb159f2b014c7919</link>
		<pubDate>Mon, 25 Jul 2011 20:10:29 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/Using-TripleDES-to-encrypt-64-bits-and-only-64-bits/5abe6ac3666e4ecdbb159f2b014c7919#5abe6ac3666e4ecdbb159f2b014c7919</guid>
		<dc:creator>W3bbo</dc:creator>
		<slash:comments>7</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/W3bbo/Discussions/RSS</wfw:commentRss>
	</item>
</channel>
</rss>