<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx</link><description>One common mistake that people make when using managed encryption classes is that they attempt to store the result of an encryption operation in a string by using one of the Encoding classes. That seems to make sense right? After all, Encoding.ToString()</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#495185</link><pubDate>Mon, 21 Nov 2005 12:13:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:495185</guid><dc:creator>Robin</dc:creator><description>I have been trying this code, but it fails on the conversion of the Base64String to byte (byte[] rawData = Convert.FromBase64String. The error I got in the first place was about an invalid character, namely the comma. &lt;br&gt;&lt;br&gt;When I removed the comma and replaced it with a slash, I got the invalid length for a Base-64 char arry. &lt;br&gt;&lt;br&gt;From MSDN I learned, the string has to be at least 4 characters, ignoring the white space characters, plus it has to be a multiple of 4, ignoring the white space characters.&lt;br&gt;&lt;br&gt;Have I misread something, or am I doing something wrong here?&lt;br&gt;&lt;br&gt;I would like to know the solution.</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#495951</link><pubDate>Wed, 23 Nov 2005 00:32:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:495951</guid><dc:creator>shawnfa</dc:creator><description>Hi Robin,&lt;br&gt;&lt;br&gt;I haven't seen that problem before.  Do you have repro code available?&lt;br&gt;&lt;br&gt;-Shawn</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#499320</link><pubDate>Fri, 02 Dec 2005 14:32:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:499320</guid><dc:creator>SKiLLa</dc:creator><description>@Robin --&amp;gt; I tried the code above and it works fine.  Did you add the:&lt;br&gt;&lt;br&gt;byte[] encrypted = memoryStream.ToArray();&lt;br&gt;    return Convert.ToBase64String(encrypted);&lt;br&gt;&lt;br&gt;&lt;br&gt;    byte[] rawData = Convert.FromBase64String(data);&lt;br&gt;&lt;br&gt;&lt;br&gt;changes ?&lt;br&gt;</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#502922</link><pubDate>Tue, 13 Dec 2005 02:45:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:502922</guid><dc:creator>cw</dc:creator><description>THANKYOU!!</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#522325</link><pubDate>Wed, 01 Feb 2006 22:08:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:522325</guid><dc:creator>Hristo Yankov</dc:creator><description>Flawless!</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#534878</link><pubDate>Sun, 19 Feb 2006 04:06:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:534878</guid><dc:creator>tyson m</dc:creator><description>i cant encrypt ascii special characters like xml - how is that possible ??</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#537035</link><pubDate>Wed, 22 Feb 2006 20:27:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:537035</guid><dc:creator>shawnfa</dc:creator><description>The encryption classes don't care about &amp;quot;special&amp;quot; characters, they only see a stream of bytes. &amp;nbsp;What problem are you seeing exactly?&lt;br&gt;&lt;br&gt;-Shawn</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#538591</link><pubDate>Fri, 24 Feb 2006 15:36:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:538591</guid><dc:creator>Sam</dc:creator><description>So I think I'm in bad shape. &amp;nbsp;My encryption function doesn't return the same value in ASP 2.0 as it does in 1.1. &amp;nbsp;I'm sure its because of the different behavior in Encoding classes. &amp;nbsp;What can I do? &amp;nbsp;I have WAY too much data encrypted under my &amp;quot;bad scheme&amp;quot;. &amp;nbsp;I need a way to get the &amp;quot;old encoding methods&amp;quot; from 1.1, and include them in my project. &amp;nbsp;Any ideas?&lt;br&gt;&lt;br&gt;Thanks a bunch. &amp;nbsp;I'm in a pickle!&lt;br&gt;&lt;br&gt;Sam&lt;br&gt;&lt;br&gt;&lt;br&gt;Public Function EncryptString(ByVal Source As String) As String&lt;br&gt;&lt;br&gt;Dim larrSourceData As Byte()&lt;br&gt;Dim larrDestinationData As Byte()&lt;br&gt;&lt;br&gt;	larrSourceData = Encoding.Unicode.GetBytes(Source)&lt;br&gt;&lt;br&gt;	Call SetAESValues()&lt;br&gt;	larrDestinationData = _AESManaged.CreateEncryptor.TransformFinalBlock(larrSourceData, 0, larrSourceData.Length)&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;	Return Encoding.Unicode.GetString(larrDestinationData)&lt;br&gt;&lt;br&gt;&lt;br&gt;End Function</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#539825</link><pubDate>Mon, 27 Feb 2006 15:42:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:539825</guid><dc:creator>Aleks</dc:creator><description>Hi all,&lt;br&gt;&lt;br&gt;I have a problem with this code.&lt;br&gt;When I encode two times the exactely same string I get a different encrypted string.&lt;br&gt;&lt;br&gt;Any help ?</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#539923</link><pubDate>Mon, 27 Feb 2006 19:20:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:539923</guid><dc:creator>shawnfa</dc:creator><description>Hi Sam,&lt;br&gt;&lt;br&gt;Your best bet is probably to bind old versions of your application to the v1.1 framework via an app.config file, and create a new version of your application which does not use the Encoding classes to store ciphertext.&lt;br&gt;&lt;br&gt;When you install the new version, you could have some sort of upgrade utility that is also bound to the v1.1 runtime and reads in the old data, writing it out in base 64. &amp;nbsp;Or you could have the new version of the application detect old data files and run the upgrade tool automatically.&lt;br&gt;&lt;br&gt;-Shawn</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#539925</link><pubDate>Mon, 27 Feb 2006 19:21:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:539925</guid><dc:creator>shawnfa</dc:creator><description>Hi Aleks,&lt;br&gt;&lt;br&gt;The fact that ciphertext differs does not mean that it's incorrect. &amp;nbsp;If you're using symmetric encryption, you should chekc that your key, IV, and padding mode are the same. &amp;nbsp;Asymmetric encryption will always have different output due to reandom padding.&lt;br&gt;&lt;br&gt;As long as you can round trip your data, you should be fine.&lt;br&gt;&lt;br&gt;-Shawn</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#539941</link><pubDate>Mon, 27 Feb 2006 19:31:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:539941</guid><dc:creator>Aleks</dc:creator><description>OK Now I have it functionning (I hope). It was because of the &amp;quot;salt&amp;quot; that I didn't need.&lt;br&gt;&lt;br&gt;The problem is that, for some string, the decryption fail with the old method (without Base64) and the new one too (with Base64).&lt;br&gt;Theses strings are passwords and I absolutely need to have it functionning as quick as possible.&lt;br&gt;&lt;br&gt;I can send some code by email I you'd like ...&lt;br&gt;&lt;br&gt;Thank you shawnfa</description></item><item><title>The .NET Security Blog</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#553937</link><pubDate>Fri, 17 Mar 2006 21:26:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:553937</guid><dc:creator>Peter Stathakos - Stack Of Toast</dc:creator><description /></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#1081742</link><pubDate>Wed, 15 Nov 2006 21:36:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1081742</guid><dc:creator>MarkW</dc:creator><description>&lt;p&gt;I have heard rumours that a certain type of implementation of AES (128bit) has been cracked &amp;nbsp;(in milliseconds rather than years). If this is true, how can we be sure that the Rijndael implementation within this Crypto namespace is not at risk.&lt;/p&gt;
&lt;p&gt;Just curious ;)&lt;/p&gt;</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#1313210</link><pubDate>Mon, 18 Dec 2006 04:07:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1313210</guid><dc:creator>shawnfa</dc:creator><description>&lt;p&gt;I hadn't seen anything about AES being cracked, so I'm not sure I can comment on RijndaelManaged :-)&lt;/p&gt;
&lt;p&gt;-Shawn&lt;/p&gt;
</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#1380963</link><pubDate>Fri, 29 Dec 2006 23:10:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1380963</guid><dc:creator>John Glynn</dc:creator><description>&lt;p&gt;How can you store encrypted values in a database if they aren't converted into strings?&lt;/p&gt;</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#1399959</link><pubDate>Tue, 02 Jan 2007 21:53:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1399959</guid><dc:creator>shawnfa</dc:creator><description>&lt;p&gt;Hi John,&lt;/p&gt;
&lt;p&gt;You could store the encrypted byte array as a blob field in the database, or you can continue to store as a string. &amp;nbsp;However, when converting to a string do not use the Encoding classes, but instead use Convert.ToBase64String / Convert.FromBase64String. &amp;nbsp;This will create a string that can always be round-tripped back to the original byte array.&lt;/p&gt;
&lt;p&gt;-Shawn&lt;/p&gt;
</description></item><item><title>Your encryption algorithm may fail moving to .NET 2.0</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#1463446</link><pubDate>Sun, 14 Jan 2007 03:52:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1463446</guid><dc:creator>CodeClimber</dc:creator><description>&lt;p&gt;Your encryption algorithm may fail moving to .NET 2.0&lt;/p&gt;</description></item><item><title>Remember not to use strings for random byte sequences.</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#1685023</link><pubDate>Thu, 15 Feb 2007 20:47:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1685023</guid><dc:creator>I'm not a Klingon</dc:creator><description>&lt;p&gt;A different, more secure, Shawn , blogged &amp;quot; Don't Roundtrip Ciphertext Via a String Encoding &amp;quot;. I've&lt;/p&gt;
</description></item><item><title>Encryption / license issues when upgrading from .Net 1.1 to .Net 2 &amp;laquo; Cottleston Pie</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#2003458</link><pubDate>Sun, 01 Apr 2007 08:08:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2003458</guid><dc:creator>Encryption / license issues when upgrading from .Net 1.1 to .Net 2 « Cottleston Pie</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://fernandof.wordpress.com/2007/04/01/encryption-license-issues-when-upgrading-from-net-11-to-net-2/"&gt;http://fernandof.wordpress.com/2007/04/01/encryption-license-issues-when-upgrading-from-net-11-to-net-2/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#2107101</link><pubDate>Fri, 13 Apr 2007 04:26:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2107101</guid><dc:creator>Amir Hussein</dc:creator><description>&lt;p&gt;When I decrypt a binary file I can not be opened! The PDF Document wheb decrypted is blank. Why? amirhussein@gmail.com&lt;/p&gt;</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#2116323</link><pubDate>Fri, 13 Apr 2007 19:06:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2116323</guid><dc:creator>michelle</dc:creator><description>&lt;p&gt;Okaaayy... So what if we don't want to use Base64? I'm trying to encrypt and store text that has commas, colons, etc -- more than just the letters and numbers that Base64 includes.&lt;/p&gt;</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#2117468</link><pubDate>Fri, 13 Apr 2007 20:33:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2117468</guid><dc:creator>shawnfa</dc:creator><description>&lt;p&gt;Hi Michelle,&lt;/p&gt;
&lt;p&gt;Base64 is just used to encode the ciphertext, you certainly do not need to limit your input to characters that appear in the base64 set. &amp;nbsp;In fact your input to the encryption algorithm doesn't even need to be a string at all.&lt;/p&gt;
&lt;p&gt;For instance (all hypothetical and not the real encodings):&lt;/p&gt;
&lt;p&gt;Plaintext: &amp;quot;Here-Is=Some:Plain, Text&amp;quot;&lt;/p&gt;
&lt;p&gt;Ciphertext: 0x12, 0x34, 0x56, 0x78, ...&lt;/p&gt;
&lt;p&gt;Cipertext to base64: abcdefg1234==&lt;/p&gt;
&lt;p&gt;The in the reverse&lt;/p&gt;
&lt;p&gt;Base64: abcdefg1234==&lt;/p&gt;
&lt;p&gt;Ciphertext from base64: 0x12, 0x34, 0x56, 0x78 ...&lt;/p&gt;
&lt;p&gt;Plaintext decrypted: &amp;quot;Here-Is=Some:Plain, Text&amp;quot;&lt;/p&gt;
&lt;p&gt;-Shawn&lt;/p&gt;
</description></item><item><title>Base64 Data in XML File</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#2299844</link><pubDate>Fri, 27 Apr 2007 17:01:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2299844</guid><dc:creator>Jason Siatkowski</dc:creator><description>&lt;p&gt;Hi All, I'm hoping that this thread still gets read. I am having a problem with encrypting and decrypting an XML file. Since the relevant code blocks are fairly short, I will post them in this message.&lt;/p&gt;
&lt;p&gt;This block of code passes my XML to the encryption method.&lt;/p&gt;
&lt;p&gt;MemoryStream myDataStream = new MemoryStream();&lt;/p&gt;
&lt;p&gt;myDS.WriteXml(myDataStream, XmlWriteMode.IgnoreSchema);&lt;/p&gt;
&lt;p&gt;byte[] myBytes = myDataStream.GetBuffer();&lt;/p&gt;
&lt;p&gt;blCryptography.EncryptByte(myBytes);&lt;/p&gt;
&lt;p&gt;string encryptedTransactionData = Convert.ToBase64String(myBytes);&lt;/p&gt;
&lt;p&gt;That call to blCryptography is the actual encryption method, it looks like this...&lt;/p&gt;
&lt;p&gt;public static byte[] EncryptByte(byte[] data)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	if ( data == null || data.Length == 0 )&lt;/p&gt;
&lt;p&gt;		throw new CryptoException(&amp;quot;Data: Cannot use null data&amp;quot;);&lt;/p&gt;
&lt;p&gt;	byte[] initVectorBytes = Encoding.ASCII.GetBytes(InitVector);&lt;/p&gt;
&lt;p&gt;	byte[] saltValueBytes &amp;nbsp;= Encoding.ASCII.GetBytes(Salt);&lt;/p&gt;
&lt;p&gt;	SymmetricAlgorithm sma = blCryptography.CreateRijndael(PassPhrase, saltValueBytes);&lt;/p&gt;
&lt;p&gt;	sma.IV = initVectorBytes;&lt;/p&gt;
&lt;p&gt;	using ( MemoryStream msEncrypt = new MemoryStream() )&lt;/p&gt;
&lt;p&gt;	using ( CryptoStream encStream = new CryptoStream(msEncrypt, sma.CreateEncryptor(), CryptoStreamMode.Write))&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		encStream.Write(data, 0, data.Length);&lt;/p&gt;
&lt;p&gt;		encStream.FlushFinalBlock();&lt;/p&gt;
&lt;p&gt;		return msEncrypt.ToArray();&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;As far as I can tell, that works fine.&lt;/p&gt;
&lt;p&gt;Now, the other side is what's giving me fits.&lt;/p&gt;
&lt;p&gt;Here's the code to decrypt the xml&lt;/p&gt;
&lt;p&gt;string myXML = myReader.GetString(1);&lt;/p&gt;
&lt;p&gt;myReader.Close();&lt;/p&gt;
&lt;p&gt;byte[] baEncryptedData = Convert.FromBase64String(myXML);&lt;/p&gt;
&lt;p&gt;byte[] baClearData = blCryptography.DecryptByte(baEncryptedData); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;	&lt;/p&gt;
&lt;p&gt;MemoryStream myStream = new MemoryStream(baClearData);&lt;/p&gt;
&lt;p&gt;myDS.ReadXml(myStream, XmlReadMode.IgnoreSchema);&lt;/p&gt;
&lt;p&gt;And finally, again the call to blCryptography is the decrypt method:&lt;/p&gt;
&lt;p&gt;public static byte[] DecryptByte(byte[] data)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	if ( data == null || data.Length == 0 )&lt;/p&gt;
&lt;p&gt;		throw new CryptoException(&amp;quot;Data: Cannot use null data&amp;quot;); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;	byte[] initVectorBytes = Encoding.ASCII.GetBytes(InitVector);&lt;/p&gt;
&lt;p&gt;	byte[] saltValueBytes &amp;nbsp;= Encoding.ASCII.GetBytes(Salt);&lt;/p&gt;
&lt;p&gt;	SymmetricAlgorithm sma = blCryptography.CreateRijndael(PassPhrase, saltValueBytes);&lt;/p&gt;
&lt;p&gt;	sma.IV = initVectorBytes;&lt;/p&gt;
&lt;p&gt;	sma.Padding = PaddingMode.None;&lt;/p&gt;
&lt;p&gt;	using ( MemoryStream msDecrypt = new MemoryStream(data) )&lt;/p&gt;
&lt;p&gt;	using ( CryptoStream csDecrypt = new CryptoStream(msDecrypt, sma.CreateDecryptor(), CryptoStreamMode.Read) )&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		// Decrypted bytes will always be less then encrypted bytes, so len of encrypted data will be big enouph for buffer.&lt;/p&gt;
&lt;p&gt;		byte[] fromEncrypt = new byte[data.Length]; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Read as many bytes as possible.&lt;/p&gt;
&lt;p&gt;		int read = csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);&lt;/p&gt;
&lt;p&gt;		if ( read &amp;lt; fromEncrypt.Length )&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			// Return a byte array of proper size.&lt;/p&gt;
&lt;p&gt;			byte[] clearBytes = new byte[read];&lt;/p&gt;
&lt;p&gt;			Buffer.BlockCopy(fromEncrypt, 0, clearBytes, 0, read);&lt;/p&gt;
&lt;p&gt;			return clearBytes;&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;		return fromEncrypt;&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;I hope that's readable. The only other wrinkle I can think of is that there is an image field in the XML file and it is a BASE64 encoded string. I'm wondering what happens if you base64encode a string that already is! Or vice versa!&lt;/p&gt;
&lt;p&gt;The problem I get is an invalid character message when I try to read the XML into the dataset.&lt;/p&gt;
&lt;p&gt;Can anyone help me figure this out?&lt;/p&gt;
&lt;p&gt;Thanks very much!&lt;/p&gt;
&lt;p&gt;--Jason&lt;/p&gt;</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#2346066</link><pubDate>Tue, 01 May 2007 02:38:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2346066</guid><dc:creator>sergeda</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;Somebody managed to use base64?&lt;/p&gt;
&lt;p&gt;I have used:&lt;/p&gt;
&lt;p&gt;Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)&lt;/p&gt;
&lt;p&gt;Now I have replaced it with:&lt;/p&gt;
&lt;p&gt;Dim inputInBytes() As Byte = Convert.FromBase64String(plainText)&lt;/p&gt;
&lt;p&gt;But now I've got another error: &amp;quot;Invalid length for a Base-64 char array&amp;quot; in this string. Please help me with this.&lt;/p&gt;</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#2466461</link><pubDate>Mon, 07 May 2007 21:35:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2466461</guid><dc:creator>shawnfa</dc:creator><description>&lt;p&gt;Hi Sergeda,&lt;/p&gt;
&lt;p&gt;You'll want to use Convert.ToBase64String() here, since you're trying to create a base64 string.&lt;/p&gt;
&lt;p&gt;-Shawn&lt;/p&gt;
</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#2466852</link><pubDate>Mon, 07 May 2007 22:01:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2466852</guid><dc:creator>shawnfa</dc:creator><description>&lt;p&gt;Hi Jason,&lt;/p&gt;
&lt;p&gt;These lines of code jump out at me:&lt;/p&gt;
&lt;p&gt;byte[] initVectorBytes = Encoding.ASCII.GetBytes(InitVector);&lt;/p&gt;
&lt;p&gt;byte[] saltValueBytes &amp;nbsp;= Encoding.ASCII.GetBytes(Salt);&lt;/p&gt;
&lt;p&gt;are InitVector and Salt both real ASCII strings?&lt;/p&gt;
&lt;p&gt;-Shawn&lt;/p&gt;
</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#9384834</link><pubDate>Fri, 30 Jan 2009 18:17:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9384834</guid><dc:creator>David</dc:creator><description>&lt;p&gt;Hmm if you call Convert.FromBase64String(plaintext) with a small string (such as &amp;quot;hello&amp;quot; I receive: &amp;quot;Invalid length for a Base-64 char array.&amp;quot;. &amp;nbsp;What am I doing wrong!?&lt;/p&gt;</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#9385030</link><pubDate>Fri, 30 Jan 2009 20:45:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9385030</guid><dc:creator>shawnfa</dc:creator><description>&lt;p&gt;FromBase64String takes a base64 string as input, not a plaintext string. &amp;nbsp;You're looking for ToBase64String to convert your &amp;quot;hello&amp;quot; string into base64. &amp;nbsp;(You'll also need to convert it to a byte array -- so something to the effect of Convert.ToBase64String(Encoding.UTF8.GetBytes(&amp;quot;hello&amp;quot;))&lt;/p&gt;
</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#9441634</link><pubDate>Mon, 23 Feb 2009 21:48:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9441634</guid><dc:creator>TheAgent</dc:creator><description>&lt;p&gt;Could someone please tell me if my code is suffering from the problem discussed here? I'm in a hurry and need to fix this ASAP. Here is my code:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Public Shared Function Encrypt(ByVal text As String, Optional ByVal additionalKey As String = &amp;quot;&amp;quot;) As String&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If text Is Nothing Then text = String.Empty&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tripleDes.Key = TruncateHash(additionalKey &amp;amp; m_key, tripleDes.KeySize \ 8)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tripleDes.IV = TruncateHash(&amp;quot;&amp;quot;, tripleDes.BlockSize \ 8)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(text)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim ms As New System.IO.MemoryStream&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim encStream As New CryptoStream(ms, tripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;encStream.Write(plaintextBytes, 0, plaintextBytes.Length)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;encStream.FlushFinalBlock()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;encStream.Dispose()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Return Convert.ToBase64String(ms.ToArray)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Function&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Public Shared Function Decrypt(ByVal encryptedText As String, Optional ByVal additionalKey As String = &amp;quot;&amp;quot;) As String&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tripleDes.Key = TruncateHash(additionalKey &amp;amp; m_key, tripleDes.KeySize \ 8)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tripleDes.IV = TruncateHash(&amp;quot;&amp;quot;, tripleDes.BlockSize \ 8)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim ms As New System.IO.MemoryStream&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim decStream As New CryptoStream(ms, tripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;decStream.Write(encryptedBytes, 0, encryptedBytes.Length)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Try&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;decStream.FlushFinalBlock()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Catch ex As Exception&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Finally&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;decStream.Dispose()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End Try&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Return System.Text.Encoding.Unicode.GetString(ms.ToArray) 'Convert.ToBase64String(ms.ToArray)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;End Function&lt;/p&gt;
&lt;p&gt;Thank you really. I don't have the time to read the post carefully.&lt;/p&gt;</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#9522028</link><pubDate>Tue, 31 Mar 2009 11:56:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9522028</guid><dc:creator>Odys</dc:creator><description>&lt;p&gt;That's solve my problem&lt;/p&gt;
&lt;p&gt;Thanks!!&lt;/p&gt;</description></item><item><title>re: Don't Roundtrip Ciphertext Via a String Encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#9634685</link><pubDate>Fri, 22 May 2009 07:02:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9634685</guid><dc:creator>Nidhi</dc:creator><description>&lt;p&gt;Hi, This is what I am using in my decrypt method..but i m getting the error of bad data Can anyone help me out:&lt;/p&gt;
&lt;p&gt;public static string DecryptString(string strEncData, string strKey, string strIV)&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			ICryptoTransform ct;&lt;/p&gt;
&lt;p&gt;			MemoryStream ms;&lt;/p&gt;
&lt;p&gt;			CryptoStream cs;&lt;/p&gt;
&lt;p&gt;			byte[] byt;&lt;/p&gt;
&lt;p&gt;			SymmetricAlgorithm mCSP=SymmetricAlgorithm.Create();&lt;/p&gt;
&lt;p&gt;			mCSP = new TripleDESCryptoServiceProvider();&lt;/p&gt;
&lt;p&gt;			mCSP.Key = Convert.FromBase64String(strKey);&lt;/p&gt;
&lt;p&gt;			mCSP.IV = Convert.FromBase64String(strIV);&lt;/p&gt;
&lt;p&gt;			ct = mCSP.CreateDecryptor(mCSP.Key,mCSP.IV);&lt;/p&gt;
&lt;p&gt;			byt = Convert.FromBase64String(strEncData);&lt;/p&gt;
&lt;p&gt;			ms = new MemoryStream();&lt;/p&gt;
&lt;p&gt;			cs = new CryptoStream(ms,ct, CryptoStreamMode.Write);&lt;/p&gt;
&lt;p&gt;			cs.Write(byt,0,byt.Length);&lt;/p&gt;
&lt;p&gt;			cs.FlushFinalBlock();&lt;/p&gt;
&lt;p&gt;			cs.Close();&lt;/p&gt;
&lt;p&gt;			return Encoding.UTF8.GetString(ms.ToArray());&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;</description></item><item><title>Strange encoding</title><link>http://blogs.msdn.com/shawnfa/archive/2005/11/10/491431.aspx#9835986</link><pubDate>Thu, 16 Jul 2009 22:36:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9835986</guid><dc:creator>Pete</dc:creator><description>&lt;p&gt;I hope this gets read:&lt;/p&gt;
&lt;p&gt;The signature to compare is (2) concatenated base64 encoded strings with a comma delimiter between them. Client is using OsterMiller Java utilities which they claim ignores bad characters (the comma) - so when I go to decode the string to verifyData against the signature in XML, it fails. If I replace the comma with nothing it fails because of invalid characters but If I just write it to the window without the comma it has no invalid characters, just (2) paddings, which I suppose is wrong too. What do I do?&lt;/p&gt;</description></item></channel></rss>