DESCryptoServiceProvider, CryptoStream and InitialisationVector

  • Thread starter Thread starter John Young
  • Start date Start date
J

John Young

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
 
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]
 
Thank you very much for that. I'll return the iv back to the user using
'out'.

John


Rob Teixeira said:
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 said:
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
 
Hmm, what is 'd.BlockSize'?

Thanks

John


Rob Teixeira said:
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 said:
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
 
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 said:
Hmm, what is 'd.BlockSize'?

Thanks

John


Rob Teixeira said:
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 said:
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
 
Thank you for that... I noticed it eventually....

John


Rob Teixeira said:
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 said:
Hmm, what is 'd.BlockSize'?

Thanks

John


Rob Teixeira said:
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
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
 
Back
Top