From: Raj on
I am using the following code snippet to encrypt/decrypt using DES algorithm.

protected string Encrypt(byte[] key, string originalString)
{
if (String.IsNullOrEmpty(originalString))
{
throw new ArgumentNullException
("The string which needs to be encrypted can not be
null.");
}
DESCryptoServiceProvider cryptoProvider = new
DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateEncryptor(key, key), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(originalString);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0,
(int)memoryStream.Length);
}

protected string Decrypt(byte[] key, string cryptedString)
{
if (String.IsNullOrEmpty(cryptedString))
{
throw new ArgumentNullException("The string which needs to
be decrypted can not be null.");
}

DESCryptoServiceProvider cryptoProvider = new
DESCryptoServiceProvider();
MemoryStream memoryStream = new
MemoryStream(Convert.FromBase64String(cryptedString));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateDecryptor(key, key), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);

return reader.ReadToEnd();
}

I would like to know, how to encrypt/decrypt using varying number of key
size. The above works only if the key size is 8 bytes. May want to provide
key of size 64 bytes

Any help would be appreciated

Thank you

Regards
Raj
From: Jason Keats on
Raj wrote:
> I am using the following code snippet to encrypt/decrypt using DES algorithm.
>
> protected string Encrypt(byte[] key, string originalString)
> {
> if (String.IsNullOrEmpty(originalString))
> {
> throw new ArgumentNullException
> ("The string which needs to be encrypted can not be
> null.");
> }
> DESCryptoServiceProvider cryptoProvider = new
> DESCryptoServiceProvider();
> MemoryStream memoryStream = new MemoryStream();
> CryptoStream cryptoStream = new CryptoStream(memoryStream,
> cryptoProvider.CreateEncryptor(key, key), CryptoStreamMode.Write);
> StreamWriter writer = new StreamWriter(cryptoStream);
> writer.Write(originalString);
> writer.Flush();
> cryptoStream.FlushFinalBlock();
> writer.Flush();
> return Convert.ToBase64String(memoryStream.GetBuffer(), 0,
> (int)memoryStream.Length);
> }
>
> protected string Decrypt(byte[] key, string cryptedString)
> {
> if (String.IsNullOrEmpty(cryptedString))
> {
> throw new ArgumentNullException("The string which needs to
> be decrypted can not be null.");
> }
>
> DESCryptoServiceProvider cryptoProvider = new
> DESCryptoServiceProvider();
> MemoryStream memoryStream = new
> MemoryStream(Convert.FromBase64String(cryptedString));
> CryptoStream cryptoStream = new CryptoStream(memoryStream,
> cryptoProvider.CreateDecryptor(key, key), CryptoStreamMode.Read);
> StreamReader reader = new StreamReader(cryptoStream);
>
> return reader.ReadToEnd();
> }
>
> I would like to know, how to encrypt/decrypt using varying number of key
> size. The above works only if the key size is 8 bytes. May want to provide
> key of size 64 bytes
>
> Any help would be appreciated
>
> Thank you
>
> Regards
> Raj

If the code you found on codeproject.com is not good enough, and you're
looking for an alternative, then why not try Google?

There are plenty of alternatives.

Here's one:
http://msdn.microsoft.com/en-us/magazine/cc164055.aspx

HTH
From: rossum on
On Sat, 12 Jun 2010 04:04:25 -0700, Raj
<Raj(a)discussions.microsoft.com> wrote:

>I would like to know, how to encrypt/decrypt using varying number of key
>size. The above works only if the key size is 8 bytes. May want to provide
>key of size 64 bytes
1 DES is obsolete. Use AES/Rijndael instead.

2 Do not use the user's key directly, insteaad pass the key the user
supplies through a cryptographic hash function, such as SHA-256.
Whatever size key the user supplies the output of the hash function is
always the same size, 256 bits in the case of SHA-256. Use the output
of the hash function to key AES.

User key --> SHA-256 --> AES-256 encryption.

rossum

From: Arne Vajhøj on
On 12-06-2010 09:15, rossum wrote:
> On Sat, 12 Jun 2010 04:04:25 -0700, Raj
> <Raj(a)discussions.microsoft.com> wrote:
>> I would like to know, how to encrypt/decrypt using varying number of key
>> size. The above works only if the key size is 8 bytes. May want to provide
>> key of size 64 bytes
> 1 DES is obsolete. Use AES/Rijndael instead.
>
> 2 Do not use the user's key directly, insteaad pass the key the user
> supplies through a cryptographic hash function, such as SHA-256.
> Whatever size key the user supplies the output of the hash function is
> always the same size, 256 bits in the case of SHA-256. Use the output
> of the hash function to key AES.
>
> User key --> SHA-256 --> AES-256 encryption.

But it is very important to understand that the
password security does not depend on the number of
possible values of the hash but of the number of
possible values of the user key.

Arne
From: rossum on
On Sat, 12 Jun 2010 21:17:23 -0400, Arne Vajhøj <arne(a)vajhoej.dk>
wrote:

>On 12-06-2010 09:15, rossum wrote:
>> On Sat, 12 Jun 2010 04:04:25 -0700, Raj
>> <Raj(a)discussions.microsoft.com> wrote:
>>> I would like to know, how to encrypt/decrypt using varying number of key
>>> size. The above works only if the key size is 8 bytes. May want to provide
>>> key of size 64 bytes
>> 1 DES is obsolete. Use AES/Rijndael instead.
>>
>> 2 Do not use the user's key directly, insteaad pass the key the user
>> supplies through a cryptographic hash function, such as SHA-256.
>> Whatever size key the user supplies the output of the hash function is
>> always the same size, 256 bits in the case of SHA-256. Use the output
>> of the hash function to key AES.
>>
>> User key --> SHA-256 --> AES-256 encryption.
>
>But it is very important to understand that the
>password security does not depend on the number of
>possible values of the hash but of the number of
>possible values of the user key.
>
>Arne
I prefer

overallEntropy = min(keyEntropy, hashEntropy)

The hash imposes an upper limit, though I will agree that most
passwords/passphrases will not usually reach that limit.

rossum