Simple Encryption/Decryption using C#

Discussion in 'C#' started by naimish, Jun 30, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Encryption/Decryption file, string etc using C#

    Code:
    using System;   
    using System.Collections.Generic;   
    using System.Text;   
    using System.Security.Cryptography;   
    using System.IO;   
      
    namespace BHI.Rats   
    {   
        /// <summary>   
        /// RatsEncryptionManager is responsible for encrypting and decrypting the files.   
        /// </summary>   
        public static class  RatsEncryptionManager   
        {   
            private const string passKey = "c0ntr0l1";   
      
            /// <summary>   
            /// Decrypts the input file (strInputFileName) and creates a new decrypted file (strOutputFileName)   
            /// </summary>   
            /// <param name="strInputFileName">input file name</param>   
            /// <param name="strOutputFileName">output file name</param>   
            public static void DecryptFiletoFile(string strInputFileName, string strOutputFileName)   
            {   
                string strFileData = "";   
                using (FileStream inputStream = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read))   
                {   
                    DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();   
      
                    cryptic.Key = ASCIIEncoding.ASCII.GetBytes(passKey);   
                    cryptic.IV = ASCIIEncoding.ASCII.GetBytes(passKey);   
      
                    CryptoStream crStream = new CryptoStream(inputStream, cryptic.CreateDecryptor(), CryptoStreamMode.Read);   
      
                    StreamReader reader = new StreamReader(crStream);   
      
                    strFileData = reader.ReadToEnd();   
      
                    reader.Close();   
                    inputStream.Close();   
                }   
      
                if (File.Exists(strOutputFileName))   
                {   
                    File.Delete(strOutputFileName);   
                }   
                using (StreamWriter outputStream = new StreamWriter(strOutputFileName))   
                {   
                    outputStream.Write(strFileData, 0, strFileData.Length);   
      
                    outputStream.Close();   
                }   
      
            }   
            /// <summary>   
            /// Encrypts the input file(strInputFileName) and creates a new encrypted file(strOutputFileName)   
            /// </summary>   
            /// <param name="strInputFileName">input file name</param>   
            /// <param name="strOutputFileName">output file name</param>   
            public static void EncryptFiletoFile(string strInputFileName, string strOutputFileName)   
            {   
                byte[] fileBuffer;   
      
                using (FileStream inputStream = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read))   
                {   
                    fileBuffer = new byte[inputStream.Length];   
      
                    inputStream.Read(fileBuffer, 0, fileBuffer.GetLength(0));   
      
      
                    inputStream.Close();   
                }   
                if (File.Exists(strOutputFileName))   
                {   
                    File.Delete(strOutputFileName);   
                }   
                using (FileStream outputStream = new FileStream(strOutputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))   
                {   
                    DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();   
      
                    cryptic.Key = ASCIIEncoding.ASCII.GetBytes(passKey);   
                    cryptic.IV = ASCIIEncoding.ASCII.GetBytes(passKey);   
      
                    CryptoStream crStream = new CryptoStream(outputStream, cryptic.CreateEncryptor(), CryptoStreamMode.Write);   
      
                    crStream.Write(fileBuffer, 0, fileBuffer.Length);   
      
                    crStream.Close();   
                }   
            }   
            /// <summary>   
            /// Encrypts the input string and creates a new encrypted file(strOutputFileName)   
            /// </summary>   
            /// <param name="strInputString">input string name</param>   
            /// <param name="strOutputFileName">output file name</param>   
            public static void EncryptStringtoFile(string strInputString, string strOutputFileName)   
            {   
                if (File.Exists(strOutputFileName))   
                {   
                    File.Delete(strOutputFileName);   
                }   
                using (FileStream outputStream = new FileStream(strOutputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))   
                {   
                    DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();   
      
                    cryptic.Key = ASCIIEncoding.ASCII.GetBytes(passKey);   
                    cryptic.IV = ASCIIEncoding.ASCII.GetBytes(passKey);   
      
                    CryptoStream crStream = new CryptoStream(outputStream, cryptic.CreateEncryptor(), CryptoStreamMode.Write);   
      
                    byte[] buffer = ASCIIEncoding.ASCII.GetBytes(strInputString);   
      
                    crStream.Write(buffer, 0, buffer.Length);   
      
                    crStream.Close();   
                }   
            }   
            /// <summary>   
            /// Decrypts the input file (strInputFileName) and creates a new decrypted file (strOutputFileName)   
            /// </summary>   
            /// <param name="strInputFileName">input file name</param>   
            public static string DecryptFiletoString(string strInputFileName)   
            {   
                string strFileData = "";   
                using (FileStream inputStream = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read))   
                {   
                    DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();   
      
                    cryptic.Key = ASCIIEncoding.ASCII.GetBytes(passKey);   
                    cryptic.IV = ASCIIEncoding.ASCII.GetBytes(passKey);   
      
                    CryptoStream crStream = new CryptoStream(inputStream, cryptic.CreateDecryptor(), CryptoStreamMode.Read);   
      
                    StreamReader reader = new StreamReader(crStream);   
      
                    strFileData = reader.ReadToEnd();   
      
                    reader.Close();   
                    inputStream.Close();   
                }   
      
                return strFileData;   
            }   
        }   
    }
     
    Last edited by a moderator: Jun 30, 2009
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    What do you want ?? Did you want to post an article ?
    'cuz it doesn't look like you have a query.
     
  3. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Actually this is a solution to the given problem, I cud have write an article, but I don't know how can I, so posted it here :(

    Any idea hw can I start article here ?
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Near the top of the page, you can see "My Go4Expert", "G4EF Links" etc...

    Click on "G4EF Links" and select "Articles and Tutorials". Then appropriate go to the sub-forum under which the article should be posted (for this one, it's Programming -> C#).
    Then click on "Submit new article" near the top of the page.

    Write your article and hit post. :)

    AND, PLEASE POST YOUR CODE INSIDE CODE-BLOCKS. I did it for you in your first post in this thread. :)
     
  5. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Ohh, got it now, I have clicked on Post New Thread by mistakely :) Thanks for your clarification.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Moved to Articles for you as of now.
     
  7. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Thank you very much Shabbir, could you also accept my invitation :) LinkedIn.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  9. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Ohh, Thanks Shabbir :)
     
  10. satyedra pal

    satyedra pal New Member

    Joined:
    Mar 26, 2010
    Messages:
    93
    Likes Received:
    1
    Trophy Points:
    0
    Encryption and decryption is used in cryptography.The Mean of cryptography is secret writing.Encryption is used to encrypt(convert) the data or file in a secure or pseudo format by sender. Receiver receive it and decrypt it by using a key suggested by sender
     
  11. kien_vn

    kien_vn New Member

    Joined:
    Aug 31, 2010
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    This article is very simple and usefull
     
  12. alternoboy

    alternoboy New Member

    Joined:
    Sep 26, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    how to I post a question?
     
  13. alternoboy

    alternoboy New Member

    Joined:
    Sep 26, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Can anybody help me with this?
    I have a bunch of numbers separated with a space in a text box.
    I want to store them into an array?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice