Re: TripleDESCryptoServiceProvider UPDATED

From: Joe Kaplan \(MVP - ADSI\) (joseph.e.kaplan_at_removethis.accenture.com)
Date: 09/22/05


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?
>>
>
>



Relevant Pages

  • Re: Encryption/Decryption Changes File Size
    ... I was able to get rid of the extra bytes in the encryption by changing ... ' Write the byte array to the crypto stream and flush it. ... Dim bufferAs Byte ... ' Read a chunk of data from the MemoryStream ...
    (microsoft.public.dotnet.languages.vb)
  • Re: DES and UUEncoded
    ... If you're using CBC mode encryption, than you need to use an IV by definition. ... You'll need to find some documentation for the DES utility you're ... >> Next you create a MemoryStream that will hold the output of your ...
    (microsoft.public.dotnet.security)
  • Re: Asymettric Encryption using CryptEncrypt
    ... How come we can use the pubblic key in the CryptDecrypt function, ... > Here is some sample code I got working which does asymmetric encryption. ... > HCRYPTPROV hCryptProv; ...
    (microsoft.public.platformsdk.security)
  • Re: Password Encryption/Decryption
    ... Here is some sample code from the SDK that you could use as an example. ... 'Pass false to export the public key information or pass ... I need to take a> string password entered at login, encrypt it and then decrypt it on another> machine over a LAN. ... > I have been able to encrypt a string string, but not able to resverse the> encryption. ...
    (microsoft.public.dotnet.security)
  • Re: Encrypt a string to a string and vice versa
    ... fine (at least I don't get an exception anywhere, if the string is ... correctly encryption is left aside). ... where I'm just creating a MemoryStream by ... The problem is, as soon as I call ReadByte on the CryptoStream, I get the ...
    (microsoft.public.dotnet.languages.csharp)