Max Masnick
Max Masnick

Reputation: 3287

Encrypting a file with RSA in Visual Basic .NET

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!

Upvotes: 0

Views: 3997

Answers (1)

erickson
erickson

Reputation: 269667

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.

Upvotes: 1

Related Questions