twreid
twreid

Reputation: 1453

How to Encrypt a file for transport in C#?

I am trying to encrypt a file when I save it to disk and I have looked at the Crypto namespace in C#, but am unsure how I should do it. Basically I need the ability for my program to be able to both encrypt and decrypt a file. The file is just an xml file that is serialized by my program, but it can contain sensitive data like connection strings for SQL servers. My clients want the ability to email these profiles to others and open them in our application to apply the settings to their system.

I tried the AES classes in the Crypto namespace, but I don't know where to store the IV and the key so that my program on another machine will be able to decrypt it.

Upvotes: 2

Views: 255

Answers (1)

PhonicUK
PhonicUK

Reputation: 13864

In a typical scenario, the flow for handling something like this would go:

  • The IV is static and known to the client
  • The end machine generates an RSA keypair, and gives the public key only to the party sending the data (the XML file)
  • Your AES key is generated, and encrypted using the RSA public key and sent to the client, now only the client is able to obtain that AES key using the private key it generated previously.
  • You encrypt the data using the AES key you securely sent to the client earlier

This means that even if someone captured the complete data stream, they wouldn't be able to decrypt your data because they don't have the private key required to obtain the AES key.

Upvotes: 2

Related Questions