Click here to Skip to main content
12,239,290 members (52,275 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
I'm using SQLite database in my windows application which has to copy a backup of the database file to a flash drive. I would like to encrypt this file but all the solutions i found online would not work at all but the codes shown below would work on text files but would corrupt the database file ones it encrypts it. Please i need your help to figure this out. Thanks.
using System ;
 
using System . Collections . Generic ;
 
using System . ComponentModel ;
 
using System . Data ;
 
using System . Drawing ;
 
using System . Linq ;
 
using System . Text ;
 
using System . Windows . Forms ;
 
using System . IO ;
 
using System . Security . Cryptography ;namespace  DES
 
{
 
         public partial class Form1 : Form
 
         {
 
                 static string  sKey ;
 
                 public Form1 ()
 
                 {
 
                         InitializeComponent ();
 
                 }
 
                
 
                 private void  button1_Click ( object  sender , EventArgs  e )
 
                 {
 
                        sKey  = GenerateKey ();
 
                        
 
                         if ( openFileDialog1 . ShowDialog () == DialogResult . OK )
 
                         {
 
                                 string  source  =  openFileDialog1 . FileName ;
 
                                saveFileDialog1 . Filter = "des files |*.des" ;
 
                                 if ( saveFileDialog1 . ShowDialog () == DialogResult . OK )
 
                                 {
 
                                         string  destination  =  saveFileDialog1 . FileName ;
 
                                         EncryptFile ( source ,  destination ,  sKey );                                 
 
                                 }
 
                                
 
                         }
 
                         MessageBox . Show ( "Succesfully Encrypted!" );
 
                        
 
                 }
 
                 private void EncryptFile ( string  source , string  destination , string  sKey )
 
                 {
 
                         FileStream  fsInput  = new FileStream ( source ,
 
                                 FileMode . Open ,
 
                                 FileAccess . Read );
 
                         FileStream  fsEncrypted  = new FileStream ( destination ,
 
                                                         FileMode . Create ,
 
                                                         FileAccess . Write );
 
                         DESCryptoServiceProvider  DES  = new DESCryptoServiceProvider ();
 
                        DES . Key = ASCIIEncoding . ASCII . GetBytes ( sKey );
 
                        DES . IV  = ASCIIEncoding . ASCII . GetBytes ( sKey );
 
                         ICryptoTransform  desencrypt  =  DES . CreateEncryptor ();
 
                         CryptoStream  cryptostream  = new CryptoStream ( fsEncrypted ,
 
                                                                desencrypt ,
 
                                                                 CryptoStreamMode . Write );
 
                         byte []  bytearrayinput  = new byte [ fsInput . Length - 1 ];
 
                        fsInput . Read ( bytearrayinput , 0 ,  bytearrayinput . Length );
 
                        cryptostream . Write ( bytearrayinput , 0 ,  bytearrayinput . Length );
 
                        cryptostream . Close ();
 
                        fsInput . Close ();
 
                        fsEncrypted . Close ();
 
                 }
 
                 //function to generate a 64 bit key

                 private string GenerateKey ()
 
                 {
 
                         // Create an instance of Symetric Algorithm. Key and IV is generated automatically.

                         DESCryptoServiceProvider  desCrypto  = ( DESCryptoServiceProvider ) DESCryptoServiceProvider . Create ();
 
                         // Use the Automatically generated key for Encryption. 

                         return ASCIIEncoding . ASCII . GetString ( desCrypto . Key );
 
                 }
 
                 private void  button2_Click ( object  sender , EventArgs  e )
 
                 {
 
                        openFileDialog1 . Filter = "des files |*.des" ;
 
                         if ( openFileDialog1 . ShowDialog () == DialogResult . OK )
 
                         {
 
                                 string  source  =  openFileDialog1 . FileName ;                               
 
                                 if ( saveFileDialog1 . ShowDialog () == DialogResult . OK )
 
                                 {
 
                                         string  destination  =  saveFileDialog1 . FileName ;
 
                                         DecryptFile ( source ,  destination ,  sKey );                                 
 
                                 }
 
                                
 
                         }
 
                         MessageBox . Show ( "Succesfully Dencrypted!" );
 
                 }
 
                 private void DecryptFile ( string  source , string  destination , string  sKey )
 
                 {
 
                         DESCryptoServiceProvider  DES  = new DESCryptoServiceProvider ();
 
                         //A 64 bit key and IV is required for this provider.

                         //Set secret key For DES algorithm.

                        DES . Key = ASCIIEncoding . ASCII . GetBytes ( sKey );
 
                         //Set initialization vector.

                        DES . IV  = ASCIIEncoding . ASCII . GetBytes ( sKey );
 
                         //Create a file stream to read the encrypted file back.

                         FileStream  fsread  = new FileStream ( source ,
 
                                                                                    FileMode . Open ,
ICryptoTransform  desdecrypt  =  DES . CreateDecryptor ();
 
                         //Create crypto stream set to read and do a 

                         //DES decryption transform on incoming bytes.

                         CryptoStream  cryptostreamDecr  = new CryptoStream ( fsread ,
 
                                                                                                                 desdecrypt ,
 
                                                                                                                  CryptoStreamMode . Read );
 
                         //Print the contents of the decrypted file.

                         StreamWriter  fsDecrypted  = new StreamWriter ( destination );
 
                        fsDecrypted . Write ( new StreamReader ( cryptostreamDecr ). ReadToEnd ());
 
                        fsDecrypted . Flush ();
 
                        fsDecrypted . Close ();
 
                 }
 
                
 
         }
 
}
Posted 16-Jan-13 12:09pm
Edited 16-Jan-13 12:12pm
v2
Comments
PIEBALDconsult 16-Jan-13 17:25pm
   
I don't know, but wouldn't BitLocker be a better idea?
Sergey Alexandrovich Kryukov 16-Jan-13 17:28pm
   
Who knows? As OP does not ask a question, does not explain any problem, does not explain the goal...
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I would use the sample code MSDN provides[^].
  Permalink  
Comments
Sergey Alexandrovich Kryukov 16-Jan-13 17:27pm
   
Fair enough, a 5.
—SA
CPallini 16-Jan-13 17:37pm
   
Than you.
Emejulu JVT 16-Jan-13 17:48pm
   
That still encrypts only text files, but when passed something like a picture, it gets corrupt on decryption.
CPallini 16-Jan-13 17:55pm
   
Your are wrong. Correctly used it must (if MSDN is not wrong) provide a correct roundtrip for a binary buffer. Probably you are misusing it or experiencing another issue.
Emejulu JVT 16-Jan-13 17:58pm
   
maybe i mixed something up somewhere, i'll try again. Thanks alot
Espen Harlinn 17-Jan-13 16:54pm
   
Good point :-D
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

System.Data.SQLite itself can encrypt the database.
var conn = new System.Data.SQLite.SQLiteConnection();
conn.ConnectionString = "data source=C:\\db;password=1234";
conn.Open();
conn.Close();
Change password:
var conn = new System.Data.SQLite.SQLiteConnection();
conn.ConnectionString = "data source=C:\\db;password=1234";
conn.Open();
conn.ChangePassword("5678");
conn.Close();
  Permalink  
Comments
Emejulu JVT 17-Jan-13 19:47pm
   
i never thought of this option. Thanks a lot. I had a little problem opening the database file after encrytion but i kind of figured it out. I appreciate the help

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100