Encrypt/Decrypt with varying key size

R

Raj

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
 
J

Jason Keats

Raj said:
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
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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.

True.

But I think the term passphrase need to be replaced by
the term passbook for that to have practical importance.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top