Re: TripleDESCryptoServiceProvider UPDATED
From: Joe Kaplan \(MVP - ADSI\) (joseph.e.kaplan_at_removethis.accenture.com)
Date: 09/22/05
- Next message: Tim Wallace: "Re: TripleDESCryptoServiceProvider UPDATED"
- Previous message: Tim Wallace: "Re: TripleDESCryptoServiceProvider UPDATED"
- In reply to: Tim Wallace: "Re: TripleDESCryptoServiceProvider UPDATED"
- Next in thread: Tim Wallace: "Re: TripleDESCryptoServiceProvider UPDATED"
- Reply: Tim Wallace: "Re: TripleDESCryptoServiceProvider UPDATED"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Thu, 22 Sep 2005 12:32:23 -0500
Have you read Ivan's sample code and discussion on symmetric encryption? He
has a pretty good "canonical" sample.
http://www.dotnetthis.com/Articles/Crypto.htm
Joe K.
"Tim Wallace" <twallace-ThisDoesNotBelong-AT-emailDOTcom> wrote in message
news:O$Rh3d5vFHA.1032@TK2MSFTNGP12.phx.gbl...
>I have been doing heavy debugging, as I must get this code working, and
>I've found what I believe is a problem with the CryptoStream object. I'm
>creating the MemoryStream with a byte[] backing containing 35 bytes. After
>my encStream.Write call, the _position value (member of the underlying
>MemoryStream class) is 32, not 35 as I would expect. When I look into the
>CryptoStream object, I see that the _InputBufferSize member is 3, which is
>the number of bytes "missing" when the loop ends. somehow, the final three
>bytes are not being written in the Write call. Now, I have tried using
>Flush(), but those three bytes don't get written. If I attempt to do a
>FlushFinalBlock, I get an error: memory stream is not expandable.
>
> None of this makes any sense (it should be working!), and I'm beating my
> head against the wall over the situation. Any help would be greatly
> appreciated.
>
> Tim
>
> "Tim Wallace" <twallace-ThisDoesNotBelong-AT-emailDOTcom> wrote in message
> news:eEQCGgsvFHA.1132@TK2MSFTNGP10.phx.gbl...
>> I'm experiencing something that I am not certain is normal or a problem.
>> I have a byte array that, once run through a method that encrypts using
>> Triple DES, the array now "appears" to be truncated. By this I mean that
>> the final three bytes are now showing 0 (zero) in the debugger. When I
>> decrypt, the string is now missing characters.
>>
>> I encrypt like this (no, that is not the actual method name. yes, I have
>> this hardcoded for testing only and have omitted the key and iv array
>> values in this sample) :
>>
>> public int Encrypt(ref MemoryStream a, ref MemoryStream b)
>> {
>> // the memory streams have a byte[] as the backing source
>> byte[] k = { };
>> byte[] i = { };
>> byte[] encBuffer = new byte[a.Length];
>> int iRead = 0, iTally = 0;
>>
>> TripleDESCryptoServiceProvider tdes = new
>> TripleDESCryptoServiceProvider();
>> tdes.Key = k;
>> tdes.IV = i;
>> CryptoStream encStream = new CryptoStream(b, tdes.CreateEncryptor(),
>> CryptoStreamMode.Write);
>>
>> while ((iRead = a.Read(encBuffer, 0, encBuffer.Length)) > 0)
>> {
>> iTally += iRead;
>> encStream.Write(encBuffer, 0, iTally);
>> }
>>
>> return iTally;
>> }
>>
>> I decrypt like so:
>>
>> public int Decrypt(ref MemoryStream a, ref MemoryStream b)
>> {
>> byte[] k = { };
>> byte[] i = { };
>> byte[] encBuffer = new byte[a.Length + 10];
>> int iRead = 0, iTally = 0;
>>
>> try
>> {
>> TripleDESCryptoServiceProvider tdes = new
>> TripleDESCryptoServiceProvider();
>> CryptoStream decStream = new CryptoStream(b,
>> tdes.CreateDecryptor(k, i), CryptoStreamMode.Write);
>>
>> while ((iRead = a.Read(encBuffer, iTally, (int)a.Length -
>> iRead)) > 0)
>> {
>> iTally += iRead;
>> decStream.Write(encBuffer, 0, iRead);
>> }
>>
>> }
>> catch (Exception ex)
>> {
>> Console.WriteLine("ERROR...bytes read: {0}", iRead);
>> }
>>
>> return iTally;
>> }
>>
>> Any thoughts?
>>
>
>
- Next message: Tim Wallace: "Re: TripleDESCryptoServiceProvider UPDATED"
- Previous message: Tim Wallace: "Re: TripleDESCryptoServiceProvider UPDATED"
- In reply to: Tim Wallace: "Re: TripleDESCryptoServiceProvider UPDATED"
- Next in thread: Tim Wallace: "Re: TripleDESCryptoServiceProvider UPDATED"
- Reply: Tim Wallace: "Re: TripleDESCryptoServiceProvider UPDATED"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|
|