/ Published in: C#
                    
                                        
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
namespace Kyrathasoft.Cryptography.TripleDES.FileInFileOutUsingPassword {
using System;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms;
class clsKyrCrypt {
// Encrypt a file into another file using a password
public static void Encrypt(string fileIn,
string fileOut, string Password) {
// First we are going to open the file streams
FileMode.Open, FileAccess.Read);
FileMode.OpenOrCreate, FileAccess.Write);
// Then we are going to derive a Key and an IV from the
// Password and create an algorithm
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
// Now create a crypto stream through which we are going
// to be pumping data.
// Our fileOut is going to be receiving the encrypted bytes.
alg.CreateEncryptor(), CryptoStreamMode.Write);
// Now will will initialize a buffer and will be processing
// the input file in chunks.
// This is done to avoid reading the whole file (which can
// be huge) into memory.
int bufferLen = 4096;
int bytesRead;
do {
// read a chunk of data from the input file
bytesRead = fsIn.Read(buffer, 0, bufferLen);
// encrypt it
cs.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
// close everything
// this will also close the unrelying fsOut stream
cs.Close();
fsIn.Close();
}
// Decrypt a file into another file using a password
public static void Decrypt(string fileIn,
string fileOut, string Password) {
// First we are going to open the file streams
FileMode.Open, FileAccess.Read);
FileMode.OpenOrCreate, FileAccess.Write);
// Then we are going to derive a Key and an IV from
// the Password and create an algorithm
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
// Now create a crypto stream through which we are going
// to be pumping data.
// Our fileOut is going to be receiving the Decrypted bytes.
alg.CreateDecryptor(), CryptoStreamMode.Write);
// Now will will initialize a buffer and will be
// processing the input file in chunks.
// This is done to avoid reading the whole file (which can be
// huge) into memory.
int bufferLen = 4096;
int bytesRead;
do {
// read a chunk of data from the input file
bytesRead = fsIn.Read(buffer, 0, bufferLen);
// Decrypt it
cs.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
// close everything
cs.Close(); // this will also close the unrelying fsOut stream
fsIn.Close();
}
}
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                