DESCryptoServiceProvider and decrypting

J

Joe

I'm trying to figure out how to remove the characters padded to the end of
my string without setting the Padding = PaddingMode.None.

My original string passed in is 'passwordTest' and the resulting decrypted
string is 'passwordTestAAAAAAAAAA=='

I would like to use for both strings and files.

private static string DoEncryption(byte []data)
{
DES des = new DESCryptoServiceProvider();

des.Key = key;
des.IV = des.Key;
des.Padding = PaddingMode.PKCS7;

System.IO.MemoryStream ms = new System.IO.MemoryStream();

ICryptoTransform desencrypt = des.CreateEncryptor();
CryptoStream stream = new CryptoStream(ms, desencrypt,
CryptoStreamMode.Write);

stream.Write(data, 0, data.Length);
stream.FlushFinalBlock();
ms.Position = 0;

string encrypted = string.Empty;

encrypted = Convert.ToBase64String(ms.ToArray() );

stream.Close();

return encrypted;
}

private static string DoDecryption(byte []data)
{
DES des = new DESCryptoServiceProvider();

des.Key = key;
des.IV = des.Key;
des.Padding = PaddingMode.PKCS7;

System.IO.MemoryStream ms = new System.IO.MemoryStream(data);

CryptoStream stream = new CryptoStream(ms, des.CreateDecryptor(),
CryptoStreamMode.Read);

byte []fromEncrypt = new byte[data.Length];

stream.Read(fromEncrypt, 0, fromEncrypt.Length);

stream.Close();

return Convert.ToBase64String(fromEncrypt);
}

}

Thanks,
Joe
 
R

Rob Schieber

Joe said:
I'm trying to figure out how to remove the characters padded to the end of
my string without setting the Padding = PaddingMode.None.
.....

Joe,

You'll need to know what bytes are the Pad characters, and also
calcualate how many bytes will be padded based on the block size of your
cipher. i.e. If block length is 64 bits and last block is 40 bits, 24
bits will be padded.

I wouldn't reccomend using a block cipher for encrypting strings though
as block ciphers are just good for encrypting large chunks of data i.e.
files. For strings, I would encrourage a stream cipher, where you wont
have to deal with leftover bytes.
 
R

Rob Schieber

Joe said:
Thanks Rob. Do you know of an example of a stream cipher?

Interestingly, it doesn't look like the .net framework exposes stream
ciphers. Not sure why that is. I think I should clarify myself though,
theres nothing wrong with using block ciphers to encrypt strings/text,
its just that stream ciphers tend to be a little faster at the expense
of a little less security.

Theres a decent example of using RC4 with c#, which is a stream cipher,
here...
http://www.codeproject.com/csharp/rc4csharp.asp.

Hope that helps.
 
J

Joe

Rob,
I'm a little confused about your suggestion on figuring out the block
length. If I don't know the original string that was encrypted, how can I
figure this out?

-Joe
 
H

Helge Jensen

Joe said:
I'm trying to figure out how to remove the characters padded to the end of
my string without setting the Padding = PaddingMode.None.

Why? what's wrong with the padding?

I doubt that the code you have sent is the code you tested:

- there are references to "key" which is not in scope
- the encryption returns a string but the decryption accepts byte[]
- the Base64 decoding for decryption is done *after* decryption...

I posted some comments and an example on how to do simple encryption in
a secure way a few weeks ago, Message-ID:
<#[email protected]> in the thread "Encrypt and
Decrypt in C#".
 

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