Encryption/Decryption Using C# | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Encryption/Decryption Using C#

Thursday

Encryption/Decryption of a file/string using C#:
                                                   You can use the following code for Encryption/Decryption of a file or  a string. Here you can Encrypt /Decrypt a file  or a string by using the following methods. You just pass parameters as input in to that methods and you got suitable output.

Decrypt File-to-File
Encrypt File-to-File
Encrypt String-to-File
Decrypt File-to-String


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&#91;] fileBuffer;
            using 
(FileStream inputStream = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read))
            {
                fileBuffer 
= new byte&#91;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;
        
}
    }
}

1 comments:

Pravesh Singh said...

Very informative post. You made it very simple and understandable. It helped me as beginner as well as developer. Thanks for sharing with us.
There are some article I was found at the time of searching which are also explained very well about Encription and Decryption of file in C#. So I would like to share those article links.... check it.
http://sharpertutorials.com/simple-string-encryption-and-decryption/#comments and
http://mindstick.com/Articles/371797d3-16fe-4722-aa16-ae7a08a21992/?Encrypting%20and%20Decrypting%20Files%20using%20C%20sharp

Thanks Everyone for your valuable post.

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets