What's the easiest way to encrypt a file in c#?

后端 未结 5 622
醉话见心
醉话见心 2020-12-19 20:13

Beforehand :

I have read indeed the other topics on SO, but I can\'t find an answer in them.
(The others are about config-files, or a list of techniques)

<
相关标签:
5条回答
  • 2020-12-19 20:40

    Encryption is trivial with modern libraries: the hard part is securing the key(s).

    So you need to look at what you're trying to secure, and what threats you are trying to secure against.

    To encrypt a file so only the current user can see it on a client workstation, File.Encrypt is a good choice, or DPAPI with the CurrentUser scope.

    For a configuration file on a single server, DPAPI using the LocalMachine scope is a good choice. You then need to make sure only authorized users are able to log in to the server. Here you're essentially delegating key management to Windows.

    For a configuration file on a server farm, you need to share the key between the servers. RsaProtectedConfigurationProvide is a good choice, but you have more work ensuring that all servers have access to the same key, and that it is protected against unauthorized access (e.g. using a DACL).

    0 讨论(0)
  • 2020-12-19 20:43

    I recommend the Cryptography Application block in Enterprise Library. Very easy, very flexible.

    0 讨论(0)
  • 2020-12-19 20:44

    Don't believe you have any security just because you encrypt a config file. If someone has access to the encrypted config file, and your executable, containing the password, it's likely to be possible to decrypt your configfile. It's just a little harder.

    And say your config file contains passwords to database connections, it might be possible to get those passwords looking at the network packets.

    0 讨论(0)
  • 2020-12-19 20:49

    Data Protection API in C#

    0 讨论(0)
  • 2020-12-19 20:50

    File.Encrypt is pretty simple - one call (with one parameter).

    Of course, it really depends on what you want the encryption for. File.Encrypt encrypts to the current account, which isn't much use if you're passing the file around. But, given your spec - i.e. easiest way to encrypt a file - it has to be a candidate!

    0 讨论(0)
提交回复
热议问题