This walkthrough demonstrates how to encrypt and decrypt content. The code examples are designed for a Windows Forms application. This application does not demonstrate real world scenarios, such as using smart cards. Instead, it demonstrates the fundamentals of encryption and decryption.
-->
This walkthrough uses the following guidelines for encryption:
The public key is used to encrypt data. The private key however is private. It is only ever stored on user’s device. The private key is used to decrypt data. Public key is used to convert the message to an unreadable form. Private key is used to convert the received message back to the original message.
The following table summarizes the cryptographic tasks in this topic.
Prerequisites
You need the following components to complete this walkthrough:
Creating a Windows Forms Application
Most of the code examples in this walkthrough are designed to be event handlers for button controls. The following table lists the controls required for the sample application and their required names to match the code examples.
Double-click the buttons in the Visual Studio designer to create their event handlers.
Declaring Global Objects
Add the following code to the Form's constructor. Edit the string variables for your environment and preferences.
Creating an Asymmetric Key
This task creates an asymmetric key that encrypts and decrypts the RijndaelManaged key. This key was used to encrypt the content and it displays the key container name on the label control.
Add the following code as the
Click event handler for the Create Keys button (buttonCreateAsmKeys_Click ).
![]() Encrypting a File
This task involves two methods: the event handler method for the
Encrypt File button (buttonEncryptFile_Click ) and the EncryptFile method. The first method displays a dialog box for selecting a file and passes the file name to the second method, which performs the encryption.
The encrypted content, key, and IV are all saved to one FileStream, which is referred to as the encryption package.
The
EncryptFile method does the following:
The encryption package uses the following format:
You can use the lengths of the key and IV to determine the starting points and lengths of all parts of the encryption package, which can then be used to decrypt the file.
Add the following code as the
Click event handler for the Encrypt File button (buttonEncryptFile_Click ).
Add the following
EncryptFile method to the form.
Decrypting a File
This task involves two methods, the event handler method for the
Decrypt File button (buttonDecryptFile_Click ), and the DecryptFile method. The first method displays a dialog box for selecting a file and passes its file name to the second method, which performs the decryption.
The
Decrypt method does the following:
Add the following code as the
Click event handler for the Decrypt File button.
Add the following
DecryptFile method to the form.
Exporting a Public Key
This task saves the key created by the
Create Keys button to a file. It exports only the public parameters.
This task simulates the scenario of Alice giving Bob her public key so that he can encrypt files for her. He and others who have that public key will not be able to decrypt them because they do not have the full key pair with private parameters.
Add the following code as the
Click event handler for the Export Public Key button (buttonExportPublicKey_Click ).
Importing a Public Key
This task loads the key with only public parameters, as created by the
Export Public Key button, and sets it as the key container name.
This task simulates the scenario of Bob loading Alice's key with only public parameters so he can encrypt files for her.
Add the following code as the
Click event handler for the Import Public Key button (buttonImportPublicKey_Click ).
Getting a Private Key
This task sets the key container name to the name of the key created by using the
Create Keys button. The key container will contain the full key pair with private parameters.
This task simulates the scenario of Alice using her private key to decrypt files encrypted by Bob.
Add the following code as the
Click event handler for the Get Private Key button (buttonGetPrivateKey_Click ).
Testing the Application
After you have built the application, perform the following testing scenarios.
To create keys, encrypt, and decryptSelf-generated-certificate Public Key Encryption Without Pairing Key
To encrypt using the public key
This scenario demonstrates having only the public key to encrypt a file for another person. Typically that person would give you only the public key and withhold the private key for decryption.
To decrypt using the private key
Self-generated-certificate Public Key Encryption Without Pairing SoftwareSee alsoComments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |