Public key encryption (also termed as asymmetric encryption) has an significant diversity from private key encryption. Public key encryption employs two dissimilar keys: one key for encryption and another key for decryption. Why don’t they merely call this two-key encryption and call private key encryption one-key encryption? While it is widely known that security experts like to create jargon to validate their high consultancy fees, there is also a logical cause for this naming, which lies in the way the two types of encryption are used.

Whereas private key encryption presumes that both the encrypting and decrypting companies already know the private key, public key encryption grants a method to securely issue a key to someone and have that individual send you information that only you can decrypt.

It executes like this: Our system produces a public/private key pair. We send the public key to someone who utilizes it to encrypt a message. He / She send the encrypted message to us, and we decrypt the message with the private key.

Even if an trespasser gains possession of the public key, he cannot exercise it to decrypt the encrypted message because only the private key can decrypt the message, and this is never given away. In distinction with private key encryption, the keys used in public key encryption are more than simple sequences. The key is actually a structure with eight fields: two of the fields are used for encrypting with the public key, and six are used for decrypting with the private key. The public key is obtained by withdrawal from the private key, which is why the private key can be used for both encryption and decryption.

Figure 1-1 demonstrates how public key encryption and decryption work, using the model of a system requesting a credit card number from a user.
Figure :Public key encryption and decryption





Public key encryption is sluggish than private key encryption and cannot process large amounts of data. The RSA algorithm (RSA refers to the initials of the people who developed it: Ron Rivest, Adi Shamir, and Leonard Adleman) can encrypt a message of only 116 bytes (58 unicode characters).

A widespread use for public key encryption is for securely passing a private key, which is then used for encrypting and decrypting other information.
Attach public key encryption to the security library
In this solution, you will add public key encryption functions to your security library.

1. In Visual Studio .NET, open the project CH01_Encryption\EMS\ Start\EMS.sln.

2. Open SecurityLibrary.vb. Add the following code:

3. Namespace PublicKey
Module PublicKey
Function CreateKeyPair() As String
’Create a new random key pair
Dim rsa As New RSACryptoServiceProvider()
CreateKeyPair = rsa.ToXmlString(True)
rsa.Clear()
End Function
Function GetPublicKey(ByVal strPrivateKey As String) As String
’Extract the public key from the
’public/private key pair
Dim rsa As New RSACryptoServiceProvider()
rsa.FromXmlString(strPrivateKey)
Return rsa.ToXmlString(False)
End Function
Function Encrypt(ByVal strPlainText As String, _
ByVal strPublicKey As String) As String
’Encrypt a string using the private or public key
Dim rsa As New RSACryptoServiceProvider()
Dim bytPlainText() As Byte
Dim bytCipherText() As Byte
Dim uEncode As New UnicodeEncoding()
rsa.FromXmlString(strPublicKey)
bytPlainText = uEncode.GetBytes(strPlainText)
bytCipherText = rsa.Encrypt(bytPlainText, False)
Encrypt = Convert.ToBase64String(bytCipherText)
rsa.Clear()
End Function
Function Decrypt(ByVal strCipherText As String, _
ByVal strPrivateKey As String) As String
’Decrypt a string using the private key
Dim rsa As New RSACryptoServiceProvider()
Dim bytPlainText() As Byte
Dim bytCipherText() As Byte
Dim uEncode As New UnicodeEncoding()
rsa.FromXmlString(strPrivateKey)
bytCipherText = Convert.FromBase64String(strCipherText)
bytPlainText = rsa.Decrypt(bytCipherText, False)
Decrypt = uEncode.GetString(bytPlainText)
rsa.Clear()
End Function
End Module
End Namespace