Click here to Skip to main content
16,011,870 members
Articles / Programming Languages / C#
Article

Encryption/Decryption with .NET

Rate me:
Please Sign up or sign in to vote.
1.21/5 (12 votes)
23 Jun 20021 min read 89K   2.1K   26   6
Encryption/Decryption with .NET

Encryption and Decryption

The System.Security.Cryptographic namespace within the Microsoft .NET Framework provides a variety of tools to aid in encryption and decryption. The CryptoStream class is used here to demonstrate the encryption and decryption with System.Security.Cryptographic.SymmetricAlgorithm, such as DESCryptoServiceProvider, RC2CryptoServiceProvider, and RijndaelManaged classes.

I have searched the Internet for some samples and all I found were based on the Microsoft sample code in KB Article Q307010, which basically uses input/output files as source and destination. I would like to have the encryption and decryption done in memory without having to specify source and destination files, so that I could use the code on a web server or so.

After I posted the original code, I found some interesting things about .NET System.Security.Cryptographic.SymmetricAlgorithm classes that they are not able to handle characters which are special to Url. (Don't know why). Therefore, I modified the source code to make it work. I have received a lot of emails regarding "Bad Data" exceptions, when some special characters are contained in the data or key. I emailed my updated code back, but haven't updated the code posted here. Looks like I have to update the code here as well.

For any questions, please contact me at: fangfrank@hotmail.com and I would be happy to answer them a.s.a.p.

Revision History

26 Jun 2002 - Initial Revision

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralVB Code decrypted strings Pin
Anonymous29-Oct-02 19:08
Anonymous29-Oct-02 19:08 
GeneralRe: VB Code decrypted strings Pin
Anonymous24-Jun-03 23:18
Anonymous24-Jun-03 23:18 
GeneralVB Code that Actually Works Pin
JeromeH25-Oct-02 13:07
JeromeH25-Oct-02 13:07 
GeneralError Pin
Anonymous30-Sep-02 5:16
Anonymous30-Sep-02 5:16 
GeneralYou should net set the key and the IV to the same value Pin
1-Jul-02 3:59
suss1-Jul-02 3:59 
You should never set the key and the IV knowingly to the same value - the IV (Initialization vector) is normally transmitted with the encrypted data whereas the key remains secret - also you should use a different IV each time you encrypt the data whereas you would normally reuse the same key - in this manner you could encrypt the same piece of data (plaintext) twice and get different crypt text however because you transmit the IV with the encrypted data (crypttext) then the recipient can still decrypt the message.

Generalreg. kEY Pin
Anonymous22-Jul-03 1:30
Anonymous22-Jul-03 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.