3
$\begingroup$

My web application receives encrypted data from another server encrypted with RSA keys. Where should I store the private key (RSA) in a .NET web application (Windows)? It seems that putting in it in the application data folder (app_data) probably isn't ideal. Is there a 'best practice' for this?

I wasn't sure if it's possible to convert the public/private key pair into another format and perhaps store it in the certificate store, but I'm not sure an RSA public/private key pair is even a "certificate".

If this was a PCI compliance issue, it seems I would not even be allowed to store them on the same server, but I cannot then see how one could implement decryption on the server?

$\endgroup$
  • 3
    $\begingroup$ Top-of-the-line solution: HSMs. $\endgroup$ – SEJPM Jan 16 '17 at 17:40
  • $\begingroup$ You can always build a self signed cert around it if you want it in the certificate store I guess. $\endgroup$ – Maarten - reinstate Monica Jan 17 '17 at 1:27
  • $\begingroup$ I don't know much about .NET applications but usually you put a password on your private key and each time you start your application you're prompted for it. $\endgroup$ – Dreadlockyx Jan 17 '17 at 9:29
2
$\begingroup$

If you are in AWS, well and paranoid enough, you might want to use KMS. Google has a similar service. As for non-cloud environments, you can use either an HSM or a KMIP key server. Both HSMs and KMIP servers can be built in an High Availability way, and Amazon offer a good option to use SafeNet HSMs in the cloud, the service is called CloudHSM.

I am not a .Net expert, any more, but my understanding is that it has some adapters to plug in an PKCS#11 engine (HSM) transparently, so that you don't have to write any code for this to work. But it's probably a lot of configuration ugly work to get it to work.

$\endgroup$
0
$\begingroup$

Look into PKI (Public Key Infrastructure).

Consider if your web server scales into a web farm, for High Availability or Load Balancing. You need a distribution method for new and existing keys to these additional servers.

Consider if the keys are stored on the web server and it were to go down. What backup of the keys do you have?

A PKI solution can enhance your organization beyond a single web application.

If you're dealing with PCI, then you should not be using self-signed certificates. Then access to the certificates also needs to be guarded.

$\endgroup$
0
$\begingroup$

There are existing hardwares to store private key on web servers. For example, you could search the term "lunasa hsm" on google. Use of hsm equipments are common in enterprises. That's why, this hardwares are expensive. If you couldn't afford this kind of hardwares, you could store public key in a xml file and only admin users have access to read this xml file. However, admins could see the private key.

Alternatively, I strongly suggest you to investigate about homomorphic encryption. In this approach, you do not have to store private keys anywhere. Some mathematical calculations could be done even private keys would not store anywhere. Only the ones who own the data could see decrypted calculations.

$\endgroup$
0
$\begingroup$

IN Windows, it can be stored in DPAPI protected storage, which can be in a user profile ( like for service account only) or computer profile. This storage is protected with acl, and when someone just resets the password, there is no access any more.

Creating a key pair in C#, named "Key_Name"

var cp = new CspParameters
    {
        KeyContainerName = Key_Name,
        Flags = CspProviderFlags.NoPrompt | CspProviderFlags.UseArchivableKey,
        KeyNumber = 1
    };
if (useMachineStore) cp.Flags |= CspProviderFlags.UseMachineKeyStore;
rp = new RSACryptoServiceProvider(1024, cp);

The RSA key itself can be imported using the ImportCspBlob functin

public void ImportCspBlob (byte[] keyBlob);

this key can be exported to xml, and imported in another machine. below "kf" is de exported keyfile in xml

var cp = new CspParameters
                    {
                        Flags = CspProviderFlags.NoFlags,
                        KeyContainerName = Key_Name,
                        KeyNumber = 1
                    };
                    if (useMachineStore) cp.Flags |= CspProviderFlags.UseMachineKeyStore;
                    rp = new RSACryptoServiceProvider(cp);
                    rp.FromXmlString(System.IO.File.ReadAllText(kf));
                    rp.PersistKeyInCsp = true;
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.