views:

435

answers:

1

I'm just getting started with Visual Basic .NET and I'm currently stuck on the following problem: how can I encrypt/decrypt a file with asymmetric encryption?

Essentially, I'm trying to figure out how I can write the following pseudocode in VB:

Function EncryptFile(path_to_file_to_encrypt, public_key)
  file = ReadFile(path_to_file_to_encrypt)
  encrypted_file = Encrypt(file, public_key)
  SaveToDisk(encrypted_file, "C:\Encrypted\encryptedfile.xxx")
End Function

Function DecryptFile(path_to_encrypted_file, private_key)
  encrypted_file = ReadFile(path_to_encrypted_file)
  file = Decrypt(file, private_key)
  SaveToDisk(file, "C:\Decrypted\file.xxx")
End Function

The file I'm encrypting/decrypting is an Access database file (i.e. binary), if that makes any difference.

I understand there are containers for private keys, but it looks like the MSDN tutorial is sufficient for me to figure this bit out. I assume I can hard-code the public key in my code (it won't be changing).

Any help would be appreciated!

+1  A: 

Usually, an RSA "key encryption key" is used to encrypt a "content encryption key" for a symmetric algorithm. That content encryption key is used to encrypt the file.

Protocols like SSL, S/MIME, and PGP can use this approach (sometimes called key transport). Asymmetric cryptography is very, very slow compared to symmetric algorithms.

Something like Chilkat's S/MIME library for VB.NET could handle this task.

erickson
Ok, this might do it. Specifically, what I want to do is encrypt a database file with the public key and email it to the person with the private key. With key transport, I could symmetrically encrypt the database file, asymmetrically encrypt the "content encryption key," and email the encrypted symmetric key and the encrypted database. Right? Assuming the symmetric key was strong, would it be just as secure as doing it all asymmetrically?
Max Masnick
Yes, that is exactly the right approach; and, of course, S/MIME does it just like you outline. As a bonus, consider that the recipient of the email is probably already running a mail client that supports S/MIME (like Outlook or Thunderbird). All they need to do is get a key pair installed in their client (load a p12 or pfx file). Finally, there is no compromise on security here. This is a protocol designed, studied, reviewed, and implemented by cryptographers.
erickson