任何文件格式的加密和解密 [英] Encryption and Decryption of any file format

查看:132
本文介绍了任何文件格式的加密和解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Windows应用程序中使用SQLite数据库,它必须将数据库文件的备份复制到闪存驱动器。我想加密这个文件,但我在网上找到的所有解决方案根本不起作用,但下面显示的代码可以用于文本文件,但会破坏加密它的数据库文件。请我帮你解决这个问题。谢谢。

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 ();

                 }

                

         }

}

推荐答案

我会使用 MSDN提供的示例代码 [ ^ ]。


System.Data.SQLite 本身可以加密数据库。

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();


这篇关于任何文件格式的加密和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆