Click here to Skip to main content
12,623,511 members (30,115 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.9K views
7 bookmarked
Posted

Encryption/Decryption for the Beginner's Level

, 21 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Focuses on beginner's level Encryption

Introduction

It often comes to the task list for the College/University level students that they have to store passwords, pass the Query String with URL or other stuff in the Encrypted way. So this trick would focus on the same to provide an easy to understand way of performing this task with minimal time and effort.

Using the Code

I have been doing some coding tasks, when this thing comes into my play, and after searching on GOOGLE, I found the following methods for both Encryption and Decryption.

Hence, I am sharing here for others to get the tasks done in an understandable way.

First of All, I would be telling about the namespaces that are to be included for this.

NameSpaces

using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.IO;
using System.Security.Cryptography; 

Encryption

private string Encrypt(string clearText)
   {
       string EncryptionKey = "KEY"; // See NOTE at end of TIP
       byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
       using (Aes encryptor = Aes.Create())
       {
           Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[]
           { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
           encryptor.Key = pdb.GetBytes(32);
           encryptor.IV = pdb.GetBytes(16);
           using (MemoryStream ms = new MemoryStream())
           {
               using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
               {
                   cs.Write(clearBytes, 0, clearBytes.Length);
                   cs.Close();
               }
               clearText = Convert.ToBase64String(ms.ToArray());
           }
       }
       return clearText;
   }

Decryption

private string Decrypt(string cipherText)
    {
        string EncryptionKey = "KEY"; //See Note at the End of TIP
        cipherText = cipherText.Replace(" ", "+");
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] 
            { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    } 

NOTE: In place of KEY, you have to provide the key, based on which both Encryption and Decryption will be done. So, the same key would be provided in both methods.

Points of Interest

While I was performing this tip myself, it taught me about the way encryption and decryption works and how they can be implemented in web application.

Comments, positive criticism and advice are heartily welcome. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

VICK
Software Developer
Pakistan Pakistan
Working as an Associate Software Engineer in LMKT


Visit My Blog!

Contact Me!

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 13192321-Mar-14 4:52
memberMember 13192321-Mar-14 4:52 
GeneralRe: My vote of 1 Pin
VICK26-Mar-14 22:22
professional VICK26-Mar-14 22:22 
GeneralMy vote of 1 Pin
OriginalGriff20-Mar-14 23:39
protectorOriginalGriff20-Mar-14 23:39 
GeneralRe: My vote of 1 Pin
VICK20-Mar-14 23:49
professional VICK20-Mar-14 23:49 
GeneralRe: My vote of 1 Pin
OriginalGriff21-Mar-14 0:10
protectorOriginalGriff21-Mar-14 0:10 
GeneralRe: My vote of 1 Pin
VICK21-Mar-14 0:33
professional VICK21-Mar-14 0:33 
GeneralRe: My vote of 1 Pin
Manfred R. Bihy21-Mar-14 0:52
memberManfred R. Bihy21-Mar-14 0:52 
GeneralRe: My vote of 1 Pin
OriginalGriff21-Mar-14 1:02
protectorOriginalGriff21-Mar-14 1:02 
QuestionDownload Source Pin
Amir Mohammad Nasrollahi20-Mar-14 21:39
professionalAmir Mohammad Nasrollahi20-Mar-14 21:39 
GeneralRe: Download Source Pin
VICK20-Mar-14 22:22
professional VICK20-Mar-14 22:22 
QuestionMIT? Pin
Ravi Bhavnani20-Mar-14 11:36
professionalRavi Bhavnani20-Mar-14 11:36 
AnswerRe: MIT? Pin
VICK20-Mar-14 19:54
professional VICK20-Mar-14 19:54 
QuestionThis has been here over and over Pin
Kees van Spelde20-Mar-14 5:20
professionalKees van Spelde20-Mar-14 5:20 
AnswerRe: This has been here over and over Pin
VICK20-Mar-14 19:56
professional VICK20-Mar-14 19:56 
QuestionSalt ? Pin
Rage20-Mar-14 3:47
professionalRage20-Mar-14 3:47 
GeneralRe: Salt ? Pin
VICK20-Mar-14 19:53
professional VICK20-Mar-14 19:53 
GeneralRe: Salt ? Pin
CS201121-Mar-14 0:04
professionalCS201121-Mar-14 0:04 
GeneralRe: Salt ? Pin
VICK21-Mar-14 0:34
professional VICK21-Mar-14 0:34 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 21 Mar 2014
Article Copyright 2014 by VICK
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid