Click here to Skip to main content
15,885,244 members
Articles / Programming Languages / Visual Basic
Article

Combining Symmetric and Asymmetric Encryption

Rate me:
Please Sign up or sign in to vote.
3.00/5 (17 votes)
25 Oct 20043 min read 95.2K   37   3
Describes symmetric and asymmetric encryption, then shows how to combine them.

Symmetric Encryption

Symmetric encryption uses a secret key value to encrypt and decrypt the data. Both the sender and receiver need the same key in order to encrypt or decrypt. There are two types of symmetric algorithms, stream algorithms and block algorithms. The stream algorithms work on one bit or byte at a time, whereas the block algorithms work on larger blocks of data (typically 64 bits). The drawback to this type of system is that if the key is discovered, all messages can be decrypted.

In the .NET framework, there are classes derived from the System.Security.SymmetricAlgorithm class in the System.Security.Cryptography namespace that equip us to use symmetric encryption. Each of the classes uses a different algorithm for this purpose. These algorithms use a random initialization vector (IV) which makes the encrypted data different even when using the same source data. Generally speaking, the algorithm is more secure the larger its key size. Here are the different classes available:

ClassAlgorithmDefault key size
DESCryptoServiceProviderDES64
TripleDESCryptoServiceProviderTripleDES192
RC2CryptServiceProviderRC2128
RijndaelManagedRijndael256

With .NET, we can wrap a stream of data with the CryptoStream. This gives us a very easy way of using symmetric encryption classes. If you wrap a FileStream with the CryptoStream, it will encrypt data as it's being written and decrypt it as it's being read. The main weakness of this type of system is the vulnerability of the one key.

Example:

  • Declare a new instance of the class:
    VB
    Private Rijndael As New RijndaelManaged()
  • Write a key:
    VB
    Dim keyFile As New FileStream("key.bin", FileMode.CreateNew) 
    keyFile.Write(Rijndael.Key, 0, Rijndael.Key.Length)
    keyFile.Close()
  • Encrypt data:
    VB
    Dim Transform As ICryptoTransform = Rijndael.CreateEncryptor() 
    Dim outFile As New FileStream("crypt.bin", FileMode.Create)
    outFile.Write(Rijndael.IV, 0, Rijndael.IV.Length) 
    Dim cryptStrm As New CryptoStream(outFile, Transform, CryptoStreamMode.Write) 
    Dim writer As New StreamWriter(cryptStrm) 
    writer.Write(txtSource.Text) 
    writer.Flush() 
    cryptStrm.FlushFinalBlock() 
    writer.Close()
  • To decrypt data, the CryptoStream would use a decryptor and it would use read mode instead of write mode.

Asymmetric Encryption

Asymmetric encryption uses a separate key for encryption and decryption. The decryption key is very hard to derive from the encryption key. The encryption key is public so that anyone can encrypt a message. However, the decryption key is private, so that only the receiver is able to decrypt the message. It is common to set up "key-pairs" within a network so that each user has a public and private key. The public key is made available to everyone so that they can send messages, but the private key is only made available to the person it belongs to.

In the .NET framework, there is a RSACryptoServiceProvider class that supports this type of encryption. This class has a default key size of 1024 bits. Because this type of encryption does not use a stream, it is more cumbersome to use. Instead of being able to wrap a FileStream, you have to encrypt data in small blocks. This type of system is generally used to encrypt keys, not entire messages. This is because asymmetric encryption is slow and they are open to "chosen-plaintext" attacks. The "chosen-plaintext" attack is when someone has access to several encrypted messages along with the plaintext, and they choose plaintext that gets encrypted. As regards to the speed of this type of systems, symmetric algorithms are generally 1000 times faster. Asymmetric algorithms also generally produce encrypted files that are much larger than the source files.

Combination Symmetric and Asymmetric Encryption

If we want the benefits of both types of encryption algorithms, the general idea is to create a random symmetric key to encrypt the data, then encrypt that key asymmetrically. Once the key is asymmetrically encrypted, we add it to the encrypted message. The receiver gets the key, decrypts it with their private key, and uses it to decrypt the message.

Example:

  • Declare a new instance of the class:
    VB
    Private RSA As New RSACryptoServiceProvider()
  • Create key:
    VB
    Dim keyFile As New FileStream("key.bin", FileMode.CreateNew)
    Dim writer As New StreamWriter(keyFile)
    writer.Write(RSA.ToXmlString(True))
    writer.Flush()
    keyFile.Close()
  • Encrypt:
    VB
    Dim outFile As New FileStream("crypt.bin", FileMode.Create)
    Dim Rijndael As New RijndaelManaged()
    Dim EncryptedKey() As Byte = RSA.Encrypt(Rijndael.Key, False)
    Dim EncryptedIV() As Byte = RSA.Encrypt(Rijndael.IV, False)
    outFile.Write(EncryptedKey, 0, EncryptedKey.Length)
    outFile.Write(EncryptedIV, 0, EncryptedIV.Length)
    Dim Transform As ICryptoTransform = Rijndael.CreateEncryptor()
    Dim cryptStrm As New CryptoStream(outFile, Transform, CryptoStreamMode.Write)
    Dim writer As New StreamWriter(cryptStrm)
    writer.Write(txtSource.Text)
    writer.Flush()
    cryptStrm.FlushFinalBlock()
    writer.Close()

Conclusion

Cryptography is a very robust field. This article tries to point out the advantages of combining different systems into one. In the current state of cryptography, the keys are the most important tools in keeping data secure. Keeping the private keys secure and large enough will make it very difficult to crack an encryption system.

Resources:

  • "Applied Cryptography", Bruce Schneier.
  • "Visual Basic .NET Programmers Cookbook", Matthew MacDonald.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I have a BSCS and am a member of the IEEE.
My current job title is IT Director.

Comments and Discussions

 
GeneralRefrence or article of code Pin
mohsin98852-Mar-11 6:06
mohsin98852-Mar-11 6:06 
NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 7:00
Tony Selke27-Sep-07 7:00 
QuestionAre keys not generated? Pin
Eugene Mayeski13-Aug-07 21:31
professionalEugene Mayeski13-Aug-07 21:31 
In the encryption code snippet the Rijndael keys are not generated. MSDN says that you should generate the key and IV yourself (i.e. they are not generated in constructor).

With best regards,
Eugene Mayevski
http://www.eldos.com - Security and low-level system components for your applications

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.