07.11.2014 Views

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

210<br />

<strong>Enterprise</strong> <strong>Library</strong> <strong>Test</strong> <strong>Guide</strong><br />

ID Scenario <strong>Test</strong> code<br />

23 Encrypt and decrypt<br />

plaintext with<br />

RijndaelManaged<br />

protected void btnEncryptAndDecrypt_Click(object sender,<br />

EventArgs e)<br />

{<br />

provider.<br />

byte[] key1;<br />

byte[] byteArrayEncryptedText;<br />

byte[] byteArrayDecryptedText;<br />

byte[] byteArrayPlainText;<br />

UnicodeEncoding textConverter = new UnicodeEncoding();<br />

ICryptoTransform encryptor, decryptor;<br />

RijndaelManaged myRijndealManaged = new RijndaelManaged();<br />

key1 =<br />

Convert.FromBase64String<br />

("zKqffFs392WuxGMhFkfhYj/HtDDIjSiXnYqMlMMzrNc=");<br />

myRijndealManaged.GenerateIV();<br />

// Convert the data to a byte array.<br />

byteArrayPlainText =<br />

textConverter.GetBytes(txtBoxPlainText.Text);<br />

encryptor = myRijndealManaged.<br />

CreateEncryptor(key1,<br />

myRijndealManaged.IV);<br />

byteArrayEncryptedText =<br />

encryptor.TransformFinalBlock(byteArrayPlainTe<br />

xt, 0,<br />

byteArrayPlainText.Length);<br />

// Get a decryptor that uses the same key<br />

// and IV as the encryptor.<br />

decryptor = myRijndealManaged.<br />

CreateDecryptor(key1,<br />

myRijndealManaged.IV);<br />

// Now decrypt the previously encrypted<br />

// message using the decryptor<br />

// obtained in the above step.<br />

byteArrayDecryptedText = new<br />

byte[byteArrayEncryptedText.Length];<br />

byteArrayDecryptedText =<br />

decryptor.TransformFinalBlock(byteArrayEncrypte<br />

dText, 0,<br />

byteArrayEncryptedText.Length);<br />

}<br />

// Convert the byte array back into a string.<br />

txtBoxPlainText.Text =<br />

textConverter.GetString(byteArrayDecryptedText);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!