PC Review


Reply
Thread Tools Rate Thread

DESCryptoServiceProvider, CryptoStream and InitialisationVector

 
 
John Young
Guest
Posts: n/a
 
      5th Jul 2004
Hi,

I have written a couple of routines which encrypt and decrypt data using
DESCryptoServiceProvider. Currently, the user has to pass in the string to
encrypt, the key and the IV to the routine. Is there a good way of creating
the IV randomly so that the user does not need to create one themselves? I
will pass the IV back to the user using an out parameter.
Would I use some kind of random generator?

Thanks in advance.

John Young


 
Reply With Quote
 
 
 
 
Rob Teixeira [MVP]
Guest
Posts: n/a
 
      5th Jul 2004
VB.NET
Dim iv As Byte() = New Byte(CInt(d.BlockSize / 8) - 1) {}
Dim rng As RandomNumberGenerator = RandomNumberGenerator.Create()
rng.GetNonZeroBytes(iv)


C#
byte[] iv = new byte[d.BlockSize / 8];
RandomNumberGenerator rng = RandomNumberGenerator.Create();
rng.GetNonZeroBytes(iv);

You'll need to import the System.Security.Cryptography namespace to get the
RandomNumberGenerator class.

-Rob Teixeira [MVP]

"John Young" <polomint77ATAThotmail.com> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I have written a couple of routines which encrypt and decrypt data using
> DESCryptoServiceProvider. Currently, the user has to pass in the string

to
> encrypt, the key and the IV to the routine. Is there a good way of

creating
> the IV randomly so that the user does not need to create one themselves?

I
> will pass the IV back to the user using an out parameter.
> Would I use some kind of random generator?
>
> Thanks in advance.
>
> John Young
>
>



 
Reply With Quote
 
John Young
Guest
Posts: n/a
 
      5th Jul 2004
Thank you very much for that. I'll return the iv back to the user using
'out'.

John


"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:%(E-Mail Removed)...
> VB.NET
> Dim iv As Byte() = New Byte(CInt(d.BlockSize / 8) - 1) {}
> Dim rng As RandomNumberGenerator = RandomNumberGenerator.Create()
> rng.GetNonZeroBytes(iv)
>
>
> C#
> byte[] iv = new byte[d.BlockSize / 8];
> RandomNumberGenerator rng = RandomNumberGenerator.Create();
> rng.GetNonZeroBytes(iv);
>
> You'll need to import the System.Security.Cryptography namespace to get
> the
> RandomNumberGenerator class.
>
> -Rob Teixeira [MVP]
>
> "John Young" <polomint77ATAThotmail.com> wrote in message
> news:(E-Mail Removed)...
>> Hi,
>>
>> I have written a couple of routines which encrypt and decrypt data
>> using
>> DESCryptoServiceProvider. Currently, the user has to pass in the string

> to
>> encrypt, the key and the IV to the routine. Is there a good way of

> creating
>> the IV randomly so that the user does not need to create one themselves?

> I
>> will pass the IV back to the user using an out parameter.
>> Would I use some kind of random generator?
>>
>> Thanks in advance.
>>
>> John Young
>>
>>

>
>



 
Reply With Quote
 
John Young
Guest
Posts: n/a
 
      5th Jul 2004
Hmm, what is 'd.BlockSize'?

Thanks

John


"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:%(E-Mail Removed)...
> VB.NET
> Dim iv As Byte() = New Byte(CInt(d.BlockSize / 8) - 1) {}
> Dim rng As RandomNumberGenerator = RandomNumberGenerator.Create()
> rng.GetNonZeroBytes(iv)
>
>
> C#
> byte[] iv = new byte[d.BlockSize / 8];
> RandomNumberGenerator rng = RandomNumberGenerator.Create();
> rng.GetNonZeroBytes(iv);
>
> You'll need to import the System.Security.Cryptography namespace to get
> the
> RandomNumberGenerator class.
>
> -Rob Teixeira [MVP]
>
> "John Young" <polomint77ATAThotmail.com> wrote in message
> news:(E-Mail Removed)...
>> Hi,
>>
>> I have written a couple of routines which encrypt and decrypt data
>> using
>> DESCryptoServiceProvider. Currently, the user has to pass in the string

> to
>> encrypt, the key and the IV to the routine. Is there a good way of

> creating
>> the IV randomly so that the user does not need to create one themselves?

> I
>> will pass the IV back to the user using an out parameter.
>> Would I use some kind of random generator?
>>
>> Thanks in advance.
>>
>> John Young
>>
>>

>
>



 
Reply With Quote
 
Rob Teixeira [MVP]
Guest
Posts: n/a
 
      5th Jul 2004
Woops, i should have left that bit of code in the sample

d is a variable that represents the instance of DESCryptoServiceProvider.
All symmetric cipher classes have a BlockSize property, and the IV size is
equal to the block size.
The BlockSize property is specified in bits, so you have to divide by 8.

-Rob Teixeira [MVP]


"John Young" <polomint77ATAThotmail.com> wrote in message
news:u5f$(E-Mail Removed)...
> Hmm, what is 'd.BlockSize'?
>
> Thanks
>
> John
>
>
> "Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
> news:%(E-Mail Removed)...
> > VB.NET
> > Dim iv As Byte() = New Byte(CInt(d.BlockSize / 8) - 1) {}
> > Dim rng As RandomNumberGenerator = RandomNumberGenerator.Create()
> > rng.GetNonZeroBytes(iv)
> >
> >
> > C#
> > byte[] iv = new byte[d.BlockSize / 8];
> > RandomNumberGenerator rng = RandomNumberGenerator.Create();
> > rng.GetNonZeroBytes(iv);
> >
> > You'll need to import the System.Security.Cryptography namespace to get
> > the
> > RandomNumberGenerator class.
> >
> > -Rob Teixeira [MVP]
> >
> > "John Young" <polomint77ATAThotmail.com> wrote in message
> > news:(E-Mail Removed)...
> >> Hi,
> >>
> >> I have written a couple of routines which encrypt and decrypt data
> >> using
> >> DESCryptoServiceProvider. Currently, the user has to pass in the

string
> > to
> >> encrypt, the key and the IV to the routine. Is there a good way of

> > creating
> >> the IV randomly so that the user does not need to create one

themselves?
> > I
> >> will pass the IV back to the user using an out parameter.
> >> Would I use some kind of random generator?
> >>
> >> Thanks in advance.
> >>
> >> John Young
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
John Young
Guest
Posts: n/a
 
      6th Jul 2004
Thank you for that... I noticed it eventually....

John


"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:(E-Mail Removed)...
> Woops, i should have left that bit of code in the sample
>
> d is a variable that represents the instance of DESCryptoServiceProvider.
> All symmetric cipher classes have a BlockSize property, and the IV size is
> equal to the block size.
> The BlockSize property is specified in bits, so you have to divide by 8.
>
> -Rob Teixeira [MVP]
>
>
> "John Young" <polomint77ATAThotmail.com> wrote in message
> news:u5f$(E-Mail Removed)...
>> Hmm, what is 'd.BlockSize'?
>>
>> Thanks
>>
>> John
>>
>>
>> "Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
>> news:%(E-Mail Removed)...
>> > VB.NET
>> > Dim iv As Byte() = New Byte(CInt(d.BlockSize / 8) - 1) {}
>> > Dim rng As RandomNumberGenerator =
>> > RandomNumberGenerator.Create()
>> > rng.GetNonZeroBytes(iv)
>> >
>> >
>> > C#
>> > byte[] iv = new byte[d.BlockSize / 8];
>> > RandomNumberGenerator rng = RandomNumberGenerator.Create();
>> > rng.GetNonZeroBytes(iv);
>> >
>> > You'll need to import the System.Security.Cryptography namespace to get
>> > the
>> > RandomNumberGenerator class.
>> >
>> > -Rob Teixeira [MVP]
>> >
>> > "John Young" <polomint77ATAThotmail.com> wrote in message
>> > news:(E-Mail Removed)...
>> >> Hi,
>> >>
>> >> I have written a couple of routines which encrypt and decrypt data
>> >> using
>> >> DESCryptoServiceProvider. Currently, the user has to pass in the

> string
>> > to
>> >> encrypt, the key and the IV to the routine. Is there a good way of
>> > creating
>> >> the IV randomly so that the user does not need to create one

> themselves?
>> > I
>> >> will pass the IV back to the user using an out parameter.
>> >> Would I use some kind of random generator?
>> >>
>> >> Thanks in advance.
>> >>
>> >> John Young
>> >>
>> >>
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DESCryptoServiceProvider Ondrej Sevecek Microsoft Dot NET 1 2nd Mar 2005 06:57 PM
about DESCryptoServiceProvider creative Microsoft Dot NET Framework 1 30th Dec 2004 09:07 AM
DESCryptoServiceProvider Microsoft C# .NET 1 27th Jan 2004 01:45 PM
DESCryptoServiceProvider Microsoft Dot NET 1 27th Jan 2004 01:45 PM
DESCryptoServiceProvider Microsoft Dot NET Framework 1 27th Jan 2004 01:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:37 PM.