By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,501 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,501 IT Pros & Developers. It's quick & easy.

DESCryptoServiceProvider and decrypting

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Joe wrote:
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.

--
Rob Schieber
Nov 17 '05 #2

P: n/a
Joe
Thanks Rob. Do you know of an example of a stream cipher?

"Rob Schieber" <sc******@hotmail.com> wrote in message
news:eL**************@TK2MSFTNGP10.phx.gbl...
Joe wrote:
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.

--
Rob Schieber

Nov 17 '05 #3

P: n/a
Joe wrote:
Thanks Rob. Do you know of an example of a stream cipher?

"Rob Schieber" <sc******@hotmail.com> wrote in message
news:eL**************@TK2MSFTNGP10.phx.gbl...
Joe wrote:
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.

--
Rob Schieber



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.
--
Rob Schieber
Nov 17 '05 #4

P: n/a
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

"Rob Schieber" <sc******@hotmail.com> wrote in message
news:eL**************@TK2MSFTNGP10.phx.gbl...
Joe wrote:
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.

--
Rob Schieber

Nov 17 '05 #5

P: n/a


Joe wrote:
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:
<#r**************@TK2MSFTNGP12.phx.gbl> in the thread "Encrypt and
Decrypt in C#".

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.