473,382 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

DESCryptoServiceProvider and decrypting

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
5 8959
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
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
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ondrej Sevecek | last post by:
Hello, would you please provide me with some simple sample of how to use the DESCryptoServiceProvider to encrypt a buffer byte buffer; with key byte key;
3
by: JW | last post by:
I can encrypt the contents of a memory stream with no problems, however when decrypting( I'm using the same key an IV ) I get an exception stating bad data. I'm at a lost. I'm actually creating...
2
by: Salman | last post by:
When I run the below method with a 64 character string as input such as: sr.Encrypt("1234567890123456789012345678901234567890123456789012345678904444"); I get square boxes as my output, which...
2
by: Tom | last post by:
Hi experts, I have the following code, which works fine: ######################### C# snippet ######################################## string k = "12345678"; string input = "ABCDEFGH"; ...
3
by: wolf | last post by:
Can I de-crypt the data by java which is crypt by DESCryptoServiceProvider? The following is my code to crypt data: string text = "This is My source data"; byte source =...
2
by: PaulN | last post by:
I'm working on an inherited app that decrypts an XML file ftp'd from a web site and then fills a dataset with it and then loads it into a database. The app as written decrypts the xml into a...
4
by: Sam | last post by:
Hi, I have a bunch of xml files that are used by my vb application. Those files were written by hand (ie not generated). Do you know a tool to encrypt them, so that no one can read their content...
5
by: Harb247 | last post by:
Hi Guys, First Post. Need some help decrypting a String / DB The company i manage recently had an argument with their lead programmer for their data base. However he has decided to have nothing...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.