Decrypting Information

Decrypting the file is simply the reverse process. You again create an implementation instance, and then read the real initialization vector and key from the file and replace the random values that were generated by the call to Create() . With these in place, create a crypto stream and wrap it around the file stream, and then read from it. The information is decrypted as you read.

Here's snippet from a console application that reads secret.txt and decrypts it:

 Rijndael* rijndael2 = Rijndael::Create(); FileStream* fs2 = new FileStream("secret.txt",FileMode::Open); unsigned char  realIV __gc[] = new unsigned char __gc[rijndael2->IV->Length]; fs2->Read(realIV,0,rijndael2->IV->Length); rijndael2->IV = realIV; unsigned char  realKey __gc[] = new unsigned char __gc[rijndael2->Key->Length]; fs2->Read(realKey,0,rijndael2->Key->Length); rijndael2->Key = realKey; ICryptoTransform* transform2 = rijndael2->CreateDecryptor(); CryptoStream* cs2 = new CryptoStream(fs2,transform2,CryptoStreamMode::Read); StreamReader* sr = new StreamReader(cs2); String* plaintext2 = sr->ReadToEnd(); Console::WriteLine(plaintext2); sr->Close(); cs2->Close(); fs2->Close(); 

Here are the differences between this code and the code presented in the previous section:

  • The file stream is created with a FileMode of Open, because this code reads the file.

  • Two garbage-collected arrays of unsigned characters are created, one for the key and one for the initialization vector, and memory is allocated for them with new .

  • The key and initialization are read directly from the file and then used to change properties of the implementation instance.

  • The transform is generated by CreateDecryptor() , because the information is being decrypted as it is read.

  • The crypto stream is in read mode rather than write mode.

  • A stream reader is used to simplify the job of reading the stream into a string.

This code takes advantage of the fact the key and initialization vectors are always the same length in the Rijndael algorithm. As a result the length of the random key and initialization vector generated by Create() can be used to allocate space for the real values to be read from the file.

Test this code yourself. Use the sample from the encryption section to fill secret.txt with an initialization vector, a key, and some encrypted text. Then run this code to read secret.txt and write out the original text. You should see the sentence you originally entered.

If you changed the way the first sample saved the initialization vector and key, make sure you make the corresponding changes to this code.

Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory © 2008-2017.
If you may any questions please contact us: