Encryption/Decryption of a file
Hi All,
I am going to develop a small software which searches from the database.
Since the data is small, I thought of using EXCEL or CSV, but now my problem is when I sell the software, I dont want the users to view the file and data.
My purpose of application is to search the year of a product from its ID...
User enters the product ID and the application returns the year...
Well the data consists of Product ID and Year... only two columns...
But the rows may extend to 5000.
Since its a small application I dont want any big databases like SQL .
But my data has to be secured.
Any idea would be appreciated
Lalit MPosted Nov 20, 2009, 12:24 AM
--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace BHI.Rats
{
///
/// RatsEncryptionManager is responsible for encrypting and decrypting the files.
///
public static class RatsEncryptionManager
{
private const string passKey = "c0ntr0l1";
///
/// Decrypts the input file (strInputFileName) and creates a new decrypted file (strOutputFileName)
///
/// input file name
/// output file name
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();
}
}
///
/// Encrypts the input file(strInputFileName) and creates a new encrypted file(strOutputFileName)
///
/// input file name
/// output file name
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();
}
}
///
/// Encrypts the input string and creates a new encrypted file(strOutputFileName)
///
/// input string name
/// output file name
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();
}
}
///
/// Decrypts the input file (strInputFileName) and creates a new decrypted file (strOutputFileName)
///
/// input file name
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;
}
}
}
How to do step by step
More Exmaple
Example
VenkateshPosted Nov 20, 2009, 2:32 AM
Roei BarPosted Nov 20, 2009, 2:07 AM
the answer is no, once you have decripted the file you can either hold it as a memoryStream , or create a temp file , dont forget that the decryption part is very costly in performance of the application.
VenkateshPosted Nov 20, 2009, 12:46 AM