Generate Asymmetric Key Pair C Rating: 8,0/10 47 votes

Chilkat • HOME • Android™ • Classic ASP • C • C++ • C# • Mono C# • .NET Core C# • C# UWP/WinRT • DataFlex • Delphi ActiveX • Delphi DLL • Visual FoxPro • Java • Lianja • MFC • Objective-C • Perl • PHP ActiveX • PHP Extension • PowerBuilder • PowerShell • PureBasic • CkPython • Chilkat2-Python • Ruby • SQL Server • Swift 2 • Swift 3/4 • Tcl • Unicode C • Unicode C++ • Visual Basic 6.0 • VB.NET • VB.NET UWP/WinRT • VBScript • Xojo Plugin • Node.js • Excel • Go

  1. Symmetric Key Encryption
  2. Symmetric Key

The next section shows a full example of what each key file should look like. The Generated Key Files. The generated files are base64-encoded encryption keys in plain text format. If you select a password for your private key, its file will be encrypted with your password. Be sure to remember this password or the key pair becomes useless. Generate a public / private key pair. Export the public and private key BLOBs to separate files. Load up the public key and encrypt some simple text. Attempt to decrypt the same encrypted text using the public key (I expected it to fail here except for when I'm using the private key - yet both work). Oct 30, 2017  How does public-key cryptography work? What is a private key and a public key? Why is asymmetric encryption different from symmetric encryption? I'll explain all. Generating Public/Private Keys In C# And.NET. How to generate public/private key in C#. Asymmetric cryptography also known as public-key encryption uses a public/private key pair to encrypt and decrypt data. In.NET, the RSACryptoServiceProvider and DSACryptoServiceProvider classes are used for asymmetric encryption. Walkthrough: Creating a Cryptographic Application.; 18 minutes to read +6. Creates an asymmetric public and private key value pair and assigns it a key container name. 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. Generate an RSA public / private key pair. Iterate PBKDF2 N times to create a symmetric key based on the user's password and salt. Use a symmetric encryption algorithm to encrypt the private key. Upload the unencrypted public key and encrypted private key to the server. The following example creates the asymmetric key PacificSales19 from a key pair stored in a file, and assigns ownership of the asymmetric key to user Christina. The private key is protected by the database master key, which must be created prior to creating the asymmetric key.

Web API Categories
ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks

Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

Objective-C example code showing how to generate an RSA public/private key pair and export to PEM files.

Chilkat Objective-C Library Downloads

© 2000-2020 Chilkat Software, Inc. All Rights Reserved.

-->

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:

  • Use the RijndaelManaged class, a symmetric algorithm, to encrypt and decrypt data by using its automatically generated Key and IV.

  • Use the RSACryptoServiceProvider, an asymmetric algorithm, to encrypt and decrypt the key to the data encrypted by RijndaelManaged. Asymmetric algorithms are best used for smaller amounts of data, such as a key.

    Note

    If you want to protect data on your computer instead of exchanging encrypted content with other people, consider using the ProtectedData or ProtectedMemory classes.

The following table summarizes the cryptographic tasks in this topic.

TaskDescription
Creating a Windows Forms applicationLists the controls that are required to run the application.
Declaring global objectsDeclares string path variables, the CspParameters, and the RSACryptoServiceProvider to have global context of the Form class.
Creating an asymmetric keyCreates an asymmetric public and private key value pair and assigns it a key container name.
Encrypting a fileDisplays a dialog box to select a file for encryption and encrypts the file.
Decrypting a fileDisplays a dialog box to select an encrypted file for decryption and decrypts the file.
Getting a private keyGets the full key pair using the key container name.
Exporting a public keySaves the key to an XML file with only public parameters.
Importing a public keyLoads the key from an XML file into the key container.
Testing the applicationLists procedures for testing this application.

Prerequisites

You need the following components to complete this walkthrough:

  • References to the System.IO and System.Security.Cryptography namespaces.

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.

ControlNameText property (as needed)
ButtonbuttonEncryptFileEncrypt File
ButtonbuttonDecryptFileDecrypt File
ButtonbuttonCreateAsmKeysCreate Keys
ButtonbuttonExportPublicKeyExport Public Key
ButtonbuttonImportPublicKeyImport Public Key
ButtonbuttonGetPrivateKeyGet Private Key
Labellabel1Key not set
OpenFileDialogopenFileDialog1
OpenFileDialogopenFileDialog2
Generate

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).

Asymmetric

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:

  1. Creates a RijndaelManaged symmetric algorithm to encrypt the content.

  2. Creates an RSACryptoServiceProvider object to encrypt the RijndaelManaged key.

  3. Uses a CryptoStream object to read and encrypt the FileStream of the source file, in blocks of bytes, into a destination FileStream object for the encrypted file.

  4. Determines the lengths of the encrypted key and IV, and creates byte arrays of their length values.

  5. Writes the Key, IV, and their length values to the encrypted package.

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. Key features of first generation computer.

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:

  1. Creates a RijndaelManaged symmetric algorithm to decrypt the content.

  2. Reads the first eight bytes of the FileStream of the encrypted package into byte arrays to obtain the lengths of the encrypted key and the IV.

  3. Extracts the key and IV from the encryption package into byte arrays.

  4. Creates an RSACryptoServiceProvider object to decrypt the RijndaelManaged key.

  5. Uses a CryptoStream object to read and decrypt the cipher text section of the FileStream encryption package, in blocks of bytes, into the FileStream object for the decrypted file. When this is finished, the decryption is completed.

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 decrypt

  1. Click the Create Keys button. The label displays the key name and shows that it is a full key pair.

  2. Click the Export Public Key button. Note that exporting the public key parameters does not change the current key.

  3. Click the Encrypt File button and select a file.

  4. Click the Decrypt File button and select the file just encrypted.

  5. Examine the file just decrypted.

  6. Close the application and restart it to test retrieving persisted key containers in the next scenario.

To encrypt using the public key

  1. Click the Import Public Key button. The label displays the key name and shows that it is public only.

  2. Click the Encrypt File button and select a file.

  3. Click the Decrypt File button and select the file just encrypted. This will fail because you must have the private key to decrypt.

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.

Symmetric Key Encryption

To decrypt using the private key

  1. Click the Get Private Key button. The label displays the key name and shows whether it is the full key pair.

  2. Click the Decrypt File button and select the file just encrypted. This will be successful because you have the full key pair to decrypt.

Symmetric Key

See also