Click here to Skip to main content
Click here to Skip to main content

Encryption/Decryption with .NET

By , 14 Mar 2002
 

Introduction

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.

If you have any questions, please email to: fangfrank@hotmail.com

Frank Fang

Source Code

using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace FangHome_Crypto
{
    /// <summary>
    /// SymmCrypto is a wrapper of System.Security.Cryptography.SymmetricAlgorithm classes
    /// and simplifies the interface. It supports customized SymmetricAlgorithm as well.
    /// </summary>
    public class SymmCrypto
    {
        /// <remarks>
        /// Supported .Net intrinsic SymmetricAlgorithm classes.
        /// </remarks>
        public enum SymmProvEnum : int
        {
            DES, RC2, Rijndael
        }

        private SymmetricAlgorithm mobjCryptoService;

        /// <remarks>
        /// Constructor for using an intrinsic .Net SymmetricAlgorithm class.
        /// </remarks>
        public SymmCrypto(SymmProvEnum NetSelected)
        {
            switch (NetSelected)
            {
                case SymmProvEnum.DES:
                    mobjCryptoService = new DESCryptoServiceProvider();
                    break;
                case SymmProvEnum.RC2:
                    mobjCryptoService = new RC2CryptoServiceProvider();
                    break;
                case SymmProvEnum.Rijndael:
                    mobjCryptoService = new RijndaelManaged();
                    break;
            }
        }

        /// <remarks>
        /// Constructor for using a customized SymmetricAlgorithm class.
        /// </remarks>
        public SymmCrypto(SymmetricAlgorithm ServiceProvider)
        {
            mobjCryptoService = ServiceProvider;
        }

        /// <remarks>
        /// Depending on the legal key size limitations of a specific CryptoService provider
        /// and length of the private key provided, padding the secret key with space character
        /// to meet the legal size of the algorithm.
        /// </remarks>
        private byte[] GetLegalKey(string Key)
        {
            string sTemp;
            if (mobjCryptoService.LegalKeySizes.Length > 0)
            {
                int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
                // key sizes are in bits
                while (Key.Length * 8 > moreSize)
                {
                    lessSize = moreSize;
                    moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;
                }
                sTemp = Key.PadRight(moreSize / 8, ' ');
            }
            else
                sTemp = Key;

            // convert the secret key to byte array
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }

        public string Encrypting(string Source, string Key)
        {
            byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
            // create a MemoryStream so that the process can be done without I/O files
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            byte[] bytKey = GetLegalKey(Key);

            // set the private key
            mobjCryptoService.Key = bytKey;
            mobjCryptoService.IV = bytKey;

            // create an Encryptor from the Provider Service instance
            ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();

            // create Crypto Stream that transforms a stream using the encryption
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

            // write out encrypted content into MemoryStream
            cs.Write(bytIn, 0, bytIn.Length);
            cs.FlushFinalBlock();
            
            // get the output and trim the '\0' bytes
            byte[] bytOut = ms.GetBuffer();
            int i = 0;
            for (i = 0; i < bytOut.Length; i++)
                if (bytOut[i] == 0)
                    break;
                    
            // convert into Base64 so that the result can be used in xml
            return System.Convert.ToBase64String(bytOut, 0, i);
        }

        public string Decrypting(string Source, string Key)
        {
            // convert from Base64 to binary
            byte[] bytIn = System.Convert.FromBase64String(Source);
            // create a MemoryStream with the input
            System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);

            byte[] bytKey = GetLegalKey(Key);

            // set the private key
            mobjCryptoService.Key = bytKey;
            mobjCryptoService.IV = bytKey;

            // create a Decryptor from the Provider Service instance
            ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
 
            // create Crypto Stream that transforms a stream using the decryption
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);

            // read out the result from the Crypto Stream
            System.IO.StreamReader sr = new System.IO.StreamReader( cs );
            return sr.ReadToEnd();
        }
    }
}

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

About the Author

Frank Fang
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Base64Stringsmemberdlwiii22 Aug '08 - 7:26 
You use Convert.ToBase64String to get a nice ASCII version of the password:
 

byte [] bCode = new Byte[encryptedmemoryStream.Length];
encryptedmemoryStream.Position =0;
encryptedmemoryStream.Read(bCode, 0, (int)encryptedmemoryStream.Length));
string textCode = Convert.ToBase64String( bCode );
 
Cheers,
Daniel
 
@ Daniel Williams, PhD, MCSD
@ Sr Integration Engineer
@ AniWorld, Inc.
@ www.aniworld.com

GeneralInvalid LengthsussAnonymous16 Aug '04 - 16:58 
I'm also getting this error message.
 
Invalid length for Base-64 char array.
GeneralRe: Invalid Lengthmembervipinjosea2 Aug '05 - 18:17 
i think you are trying Smile | :) to Decrypt data that not Encrypted or Encrypted with same key
 
thanks codeCool | :cool:
GeneralInvalid lenthsussK.Vetter14 Jul '04 - 23:18 
Hi all,
 
I've worked with the original code from the project and the posted code from the forum.
 
By decoding of an Rijndael encoded string I get a few times an "Invalid length for a Base-64 char array" exception from the decrypting method.
 
This exception occured in both versions.
 
Is there a fix for this problem?
 
Thanks
 
Kristian
GeneralRe: Invalid lenthsussAnonymous9 Sep '04 - 23:30 
Hi,
I faced the same but i found the problme
just reduce the lenght of key to < 7 and u ll get it done correctly
Wink | ;)
GeneralRe: Invalid lenthsussAnonymous3 Jan '05 - 6:58 
I don't know if I am ever going to get an answer now.
Anyway, I am using DESCryptoServiceProvider and I also get "Invalid Length Exception". If I reduce the Key-lenght to 7, then I get an error that key-size is not valid for this algorithm.
 
Any solutions????
GeneralRe: Invalid lenthmemberJohn Storer II5 Jul '06 - 2:31 
Yeah, use UTF8Encoding instead of ASCIIEncoding. ASCIIEncoding 7, UTF8Encoding 8.
 
Earth.USA.Indiana.People["Storer"]["John"][2].PrintHello("Hi!");
GeneralRe: Invalid lenthmembershuchi agarwal28 Jul '06 - 3:14 
Hi John,
 
Thanks for the suggestion . I am also facing the same problem .
In the Encrypting Function as given in RC2 Crypo i am using :
 
System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
 
Even after using as suggested by you the same error persists :
 
System.Text.UTF8Encoding .ASCII.GetBytes(Source);
 
Please tell what should i use instead of this ..
 
TIA
shuchi.
 

 


GeneralThe bug in encryption found...memberNikolai Serdiuk25 Feb '04 - 23:32 
I try to use this class with the key: ~!@#$%^& and discover that encryption bug exists. The following code is wrong:
 
int i= 0;
for (i= 0; i< byteOut.Length; i++)
if (byteOut[i] == 0)
break;
 
return System.Convert.ToBase64String(byteOut, 0, i);
 
The encrypted byte array can contain a zero code character (my case). You should use other construction:
 
byte[] byteOut = streamMemory.GetBuffer();
return System.Convert.ToBase64String(byteOut, 0, (int) streamMemory.Length);

 
Nikolai Serdiuk
GeneralRe: The bug in encryption found...sussAnonymous31 May '04 - 12:43 
Nikolai,
When I was testing my function and I specified your string, I got error message
"Specified key is a known weak key for TripleDES and cannot be used"
 
Does this mean, its too easy for users to guess your key and we need to change it?
Manmohan

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 15 Mar 2002
Article Copyright 2002 by Frank Fang
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid