I am using sql server express 2005 for my project along with visual studio
i have created databse file for my project, i wnt to encrypt dthose files using
cryptography technology available in .net framework..i knw it can be done, but dont knw how to do it
if anybody know how to encrypt file, please give me detailes step or link to any such article
thanks in advance
Loading
Jorge L FernandezPosted Nov 6, 2009, 11:59 AM
I have corrected both method to make them work. Try this
public void EncryptFile(string input, string output, RijndaelManaged alg1)
{
FileStream inputStream = new FileStream(input,FileMode.Open,FileAccess.Read);
FileStream outputStream = new FileStream(output,FileMode.OpenOrCreate,FileAccess.Write);
SymmetricAlgorithm alg = new RijndaelManaged();
alg.Key = alg1.Key;
alg.IV = alg1.IV;
// READ THE DB FILE
byte[] data = new byte[inputStream.Length];
inputStream.Read(data,0,(int)data.Length);
ICryptoTransform encryptor = alg.CreateEncryptor();
CryptoStream stream = new CryptoStream(outputStream,encryptor,CryptoStreamMode.Write);
// WRITE THE DATA ENCRYPTING
stream.Write(data,0,data.Length);
// CLOSE SEGMENT
stream.Close();
inputStream.Close();
outputStream.Close();
}
public void DecryptFile(string input, string output, RijndaelManaged alg1)
{
FileStream inputStream = new FileStream(input,FileMode.Open,FileAccess.Read);
FileStream outputStream = new FileStream(output,FileMode.OpenOrCreate,FileAccess.Write);
SymmetricAlgorithm alg = new RijndaelManaged();
alg.Key = alg1.Key;
alg.IV = alg1.IV;
ICryptoTransform decryptor = alg.CreateDecryptor();
CryptoStream stream = new CryptoStream(inputStream,decryptor,CryptoStreamMode.Read);
// READ THE ECRYPTED FILE
byte[] data = new byte[inputStream.Length];
// DECRYPT DATA
stream.Read(data,0,(int)inputStream.Length);
// WRITE DECRYTED DATA
outputStream.Write(data,0,(int)inputStream.Length);
// CLOSE SEGMENT
stream.Close();
inputStream.Close();
outputStream.Close();
}
Create a file called plain.txt in your exe folder just for testing. Bellow is a test code to test those functions
RijndaelManaged alg1 = new RijndaelManaged();
EncryptFile("plain.txt","encrypted.txt",alg1);
DecryptFile("encrypted.txt","decrypted.txt",alg1);
kassimPosted Nov 19, 2009, 9:33 AM
i just encrypted imp fields of db rather dan db file
thanks once again
cheers!!!
Jorge L FernandezPosted Nov 6, 2009, 9:20 AM
You are right. In the code the "output" variable should be used in here
FileStream outputStream = new FileStream(output,FileMode.OpenOrCreate,FileAccess.Write);
"output" is the path to save the encrypted file.
The key is exaclty that, a key that both party (encryptor and decryptor) should know. For example
// Encrypt
"Plain text" -> encrypt using key="my key" -> encrypted text = "lkjljlkjlkjlkjl"
// Decrypt
"lkjljlkjlkjlkjl" -> decrypt using key="my key" -> "Plain text"
if the key is other than "my key" then we would not be able to get the result back. The longer the key the more difficult to hack due to more possible combinations.
Please, in decrypt method you should also change the use of output variable. Usually the key is a string converted to a byte array. This both method are usefull to encrypt and decrypt files. You should consider what you really want, if fields of databases encrypted or if you want the db files encrypted itself. If you want to encrypt for example a field that stores passwords then you may consider a Hash Algorithm rather than encrypt the whole db file.
Hope this helps.
kassimPosted Nov 6, 2009, 2:45 AM
in encrypt method u dint used output and key variable, which is a parameter
and what is the role of key in this encryption, and waht value i should to use as a key
so whenevr i want to load db in appliction i hav to call Decrypt method evrytime???
Amit ChoudharyPosted Nov 6, 2009, 1:33 AM
Here is some basic code for encrypting data by using a symmetric key in SQL Server 2005.
Sam HobbsPosted Nov 5, 2009, 4:39 PM
Jorge L FernandezPosted Nov 5, 2009, 4:39 PM
Since you need to encrypt and decrypt the file representing your DB you may use a symmetric encryption algorithm. The .NET framework provides classes representing such algorithms see System.Security.Cryptography namespace. Basically you will have an input (will by the byte[] representing your file), a key (requiered to decrypt in a latter time) and the byte[] output than can be stored in as a file.
check this sample code. It should solve your problem.
// ENCRYPT
public void EncryptFile(string input, string output, byte[] key)
{
FileStream inputStream = new FileStream(input,FileMode.Open,FileAccess.Read);
FileStream outputStream = new FileStream(input,FileMode.OpenOrCreate,FileAccess.Write);
SymmetricAlgorithm alg = new RijndaelManaged();
alg.Key = key;
// READ THE DB FILE
byte[] data = new byte[inputStream.Length];
inputStream.Read(data,0,(int)data.Length);
ICryptoTransform encryptor = alg.CreateEncryptor();
CryptoStream stream = new CryptoStream(outputStream,encryptor,CryptoStreamMode.Write);
// WRITE THE DATA ENCRYPTING
stream.Write(data,0,data.Length);
// CLOSE SEGMENT
stream.Close();
inputStream.Close();
outputStream.Close();
}
// DECRYPT
public void DecryptFile(string input, string output, byte[] key)
{
FileStream inputStream = new FileStream(input,FileMode.Open,FileAccess.Read);
FileStream outputStream = new FileStream(input,FileMode.OpenOrCreate,FileAccess.Write);
SymmetricAlgorithm alg = new RijndaelManaged();
alg.Key = key;
// READ THE ECRYPTED FILE
byte[] data = new byte[inputStream.Length];
inputStream.Read(data,0,(int)data.Length);
ICryptoTransform decryptor = alg.CreateDecryptor();
CryptoStream stream = new CryptoStream(inputStream,decryptor,CryptoStreamMode.Read);
// WRITE THE DATA ENCRYPTING
stream.Read(data,0,data.Length);
// CLOSE SEGMENT
stream.Close();
inputStream.Close();
outputStream.Close();
}
If this solve your problem please mark this question as answered by this reply. Keep coding and we keep learning. Thanks