Ciencia da computação
//Create a file stream to read the encrypted file back. FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); //Create a DES decryptor from the DES instance. 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(sOutputFilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); }
Adicione as seguintes linhas ao procedimento Main () para chamar EncryptFile e DecryptFile:
static void Main() { // Must be 64 bits, 8 bytes. // Distribute this key to the user who will decrypt this file. string sSecretKey; // Get the key for the file to encrypt. sSecretKey = GenerateKey();
// For additional security pin the key. GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned ); // Encrypt the file. EncryptFile(@"C:\MyData.txt", @"C:\Encrypted.txt",