How to Generate Unique Public and Private Key via RSA

后端 未结 3 1675
旧巷少年郎
旧巷少年郎 2020-11-29 15:37

I am building a custom shopping cart where CC numbers and Exp date will be stored in a database until processing (then deleted). I need to encrypt this data (obviously).

3条回答
  •  遥遥无期
    2020-11-29 16:05

    When you use a code like this:

    using (var rsa = new RSACryptoServiceProvider(1024))
    {
       // Do something with the key...
       // Encrypt, export, etc.
    }
    

    .NET (actually Windows) stores your key in a persistent key container forever. The container is randomly generated by .NET

    This means:

    1. Any random RSA/DSA key you have EVER generated for the purpose of protecting data, creating custom X.509 certificate, etc. may have been exposed without your awareness in the Windows file system. Accessible by anyone who has access to your account.

    2. Your disk is being slowly filled with data. Normally not a big concern but it depends on your application (e.g. it might generates hundreds of keys every minute).

    To resolve these issues:

    using (var rsa = new RSACryptoServiceProvider(1024))
    {
       try
       {
          // Do something with the key...
          // Encrypt, export, etc.
       }
       finally
       {
          rsa.PersistKeyInCsp = false;
       }
    }
    

    ALWAYS

提交回复
热议问题