Click here to Skip to main content
15,916,042 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi!
I need an encryption/decryption algorithm for my project. It should encrypt a given value which may contain both alphabetic as well as numberic characters and should also be able to decrypt.

Which algorithm should I use?

Thanks for your help!
Posted
Updated 24-Oct-11 3:45am
v2

You should probably read some introductory material. A starting point might be NetAction's Guide to Using Encryption Software[^].
 
Share this answer
 
Hi Keyrun

plz copy and paste algo written below and call it. will work fine

any problem contact on
REMOVED@gmail.com
regards,

satan singh

C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace MSIS.SPH.Common
{


    public class EncryptionDecryption
    {
       

        private static string Encrypt(string plainText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
        {

            // Convert strings into byte arrays. 
            // Let us assume that strings only contain ASCII codes. 
            // If strings include Unicode characters, use Unicode, UTF7, or UTF8 
            // encoding. 
            byte[] initVectorBytes = null;
            initVectorBytes = Encoding.ASCII.GetBytes(initVector);

            byte[] saltValueBytes = null;
            saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

            // Convert our plaintext into a byte array. 
            // Let us assume that plaintext contains UTF8-encoded characters. 
            byte[] plainTextBytes = null;
            plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            
            PasswordDeriveBytes password = default(PasswordDeriveBytes);
            password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);

            // Use the password to generate pseudo-random bytes for the encryption 
            // key. Specify the size of the key in bytes (instead of bits). 
            byte[] keyBytes = null;
            keyBytes = password.GetBytes(keySize / 8);

            // Create uninitialized Rijndael encryption object. 
            RijndaelManaged symmetricKey = default(RijndaelManaged);
            symmetricKey = new RijndaelManaged();

            // It is reasonable to set encryption mode to Cipher Block Chaining 
            // (CBC). Use default options for other symmetric key parameters. 
            symmetricKey.Mode = CipherMode.CBC;

            // Generate encryptor from the existing key bytes and initialization 
            // vector. Key size will be defined based on the number of the key 
            // bytes. 
            ICryptoTransform encryptor = default(ICryptoTransform);
            encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);

            // Define memory stream which will be used to hold encrypted data. 
            MemoryStream memoryStream = default(MemoryStream);
            memoryStream = new MemoryStream();

            // Define cryptographic stream (always use Write mode for encryption). 
            CryptoStream cryptoStream = default(CryptoStream);
            cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            // Start encrypting. 
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

            // Finish encrypting. 
            cryptoStream.FlushFinalBlock();

            // Convert our encrypted data from a memory stream into a byte array. 
            byte[] cipherTextBytes = null;
            cipherTextBytes = memoryStream.ToArray();

            // Close both streams. 
            memoryStream.Close();
            cryptoStream.Close();

            // Convert encrypted data into a base64-encoded string. 
            string cipherText = null;
            cipherText = Convert.ToBase64String(cipherTextBytes);

            // Return encrypted string. 
            return cipherText;
        }

        
        private static string Decrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
        {

            // Convert strings defining encryption key characteristics into byte 
            // arrays. Let us assume that strings only contain ASCII codes. 
            // If strings include Unicode characters, use Unicode, UTF7, or UTF8 
            // encoding. 
            byte[] initVectorBytes = null;
            initVectorBytes = Encoding.ASCII.GetBytes(initVector);

            byte[] saltValueBytes = null;
            saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

            // Convert our ciphertext into a byte array. 
            byte[] cipherTextBytes = null;
            cipherTextBytes = Convert.FromBase64String(cipherText);

            // First, we must create a password, from which the key will be 
            // derived. This password will be generated from the specified 
            // passphrase and salt value. The password will be created using 
            // the specified hash algorithm. Password creation can be done in 
            // several iterations. 
            PasswordDeriveBytes password = default(PasswordDeriveBytes);
            password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);

            // Use the password to generate pseudo-random bytes for the encryption 
            // key. Specify the size of the key in bytes (instead of bits). 
            byte[] keyBytes = null;
            keyBytes = password.GetBytes(keySize / 8);

            // Create uninitialized Rijndael encryption object. 
            RijndaelManaged symmetricKey = default(RijndaelManaged);
            symmetricKey = new RijndaelManaged();

            // It is reasonable to set encryption mode to Cipher Block Chaining 
            // (CBC). Use default options for other symmetric key parameters. 
            symmetricKey.Mode = CipherMode.CBC;

            // Generate decryptor from the existing key bytes and initialization 
            // vector. Key size will be defined based on the number of the key 
            // bytes. 
            ICryptoTransform decryptor = default(ICryptoTransform);
            decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);

            // Define memory stream which will be used to hold encrypted data. 
            MemoryStream memoryStream = default(MemoryStream);
            memoryStream = new MemoryStream(cipherTextBytes);

            // Define memory stream which will be used to hold encrypted data. 
            CryptoStream cryptoStream = default(CryptoStream);
            cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);

            // Since at this point we don't know what the size of decrypted data 
            // will be, allocate the buffer long enough to hold ciphertext; 
            // plaintext is never longer than ciphertext. 
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];


            int decryptedByteCount = 0;
            decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);



            // Close both streams. 
            memoryStream.Close();
            cryptoStream.Close();

            // Convert decrypted data into a string. 
            // Let us assume that the original plaintext string was UTF8-encoded. 
            string plainText = null;
            plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);

            // Return decrypted string. 
            return plainText;
        }

        public static string EncryptValue(string strString)
        {
            if (string.IsNullOrEmpty(strString.Trim())) return "";
            string strplainText = null;
            string strcipherText = null;

            string strpassPhrase = null;
            string strsaltValue = null;
            string strhashAlgorithm = null;
            int intpasswordIterations = 0;
            string strinitVector = null;
            int intkeySize = 0;

            strplainText = strString;
            //"Hello, World! - abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890-=!@#$%^&*()_+[];,./<>?:'{}|\" ' original plaintext 

            strpassPhrase = "Pas5pr@se";
            // can be any string 
            strsaltValue = "s@1tValue";
            // can be any string 
            strhashAlgorithm = "SHA1";
            // can be "MD5" 
            intpasswordIterations = 2;
            // can be any number 
            strinitVector = "@1B2c3D4e5F6g7H8";
            // must be 16 bytes 
            intkeySize = 256;
            // can be 192 or 128 

            //Response.Write(String.Format("Plaintext : {0}", plainText)) 
            //Response.Write("<br>") 
            strcipherText = Encrypt(strplainText, strpassPhrase, strsaltValue, strhashAlgorithm, intpasswordIterations, strinitVector, intkeySize);

            //Response.Write(String.Format("Encrypted : {0}", cipherText)) 
            //Response.Write("<br>") 


            return strcipherText;
        }

        public static string DecryptValue(string strString)
        {
            if (string.IsNullOrEmpty(strString.Trim())) return "";
            string strplainText = null;
            string strcipherText = null;

            string strpassPhrase = null;
            string strsaltValue = null;
            string strhashAlgorithm = null;
            int intpasswordIterations = 0;
            string strinitVector = null;
            int intkeySize = 0;

            strplainText = strString;
            //"Hello, World! - abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890-=!@#$%^&*()_+[];,./<>?:'{}|\" ' original plaintext 

            strpassPhrase = "Pas5pr@se";
            // can be any string 
            strsaltValue = "s@1tValue";
            // can be any string 
            strhashAlgorithm = "SHA1";
            // can be "MD5" 
            intpasswordIterations = 2;
            // can be any number 
            strinitVector = "@1B2c3D4e5F6g7H8";
            // must be 16 bytes 
            intkeySize = 256;
            // can be 192 or 128 

            //Response.Write(String.Format("Plaintext : {0}", plainText)) 
            //Response.Write("<br>") 
            strcipherText = Decrypt(strplainText, strpassPhrase, strsaltValue, strhashAlgorithm, intpasswordIterations, strinitVector, intkeySize);

            //Response.Write(String.Format("Encrypted : {0}", cipherText)) 
            //Response.Write("<br>") 


            return strcipherText;
        }

    }


}</br></br></br></br>
 
Share this answer
 
v2
Comments
RaisKazi 24-Oct-11 9:43am    
1) Please use "<pre>" tag for code block.
2) Do not provide your personal Email Address in public forum, this may invite spams.
BobJanova 24-Oct-11 9:49am    
My 3: providing a large pre-cooked example is not that helpful, and particularly if it is a homework question does not help the questioner's learning.
fjdiewornncalwe 24-Oct-11 9:55am    
I agree with Bob, please don't provide complete answers to other people's homework questions. No one learns anything from that which is the entire point to education and homework.
Anil Honey 206 25-Oct-11 0:22am    
What Marcus said is correct.
satan singh 25-Oct-11 2:51am    
Thanks, i will take care what u people said.
Pretty much any encryption scheme can handle this requirement. What is it for? If it's for a local or password-based encryption (i.e. the key or keyholder is always present), you should use a symmetric scheme like TripleDES. If it's for transmission, you should use a public key scheme like RSA to encrypt a symmetric encryption key, which is then used for the bulk of the data (unless the data is always small in which case the cost of RSA encryption may be small enough you don't care). If it's for homework (as appears to be the case) you should use something which has been mentioned in your course as a good encryption scheme, unless you feel like explaining to your teacher why what you have chosen is more appropriate.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900