且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过 RSA 生成唯一的公钥和私钥

更新时间:2023-01-18 09:15:10

我最终做的是在需要创建时基于当前日期时间 (DateTime.Now.Ticks.ToString()) 创建一个新的 KeyContainer 名称一个新的密钥并将容器名称和公钥保存到数据库中.此外,每当我创建一个新密钥时,我都会执行以下操作:

What I ended up doing is create a new KeyContainer name based off of the current DateTime (DateTime.Now.Ticks.ToString()) whenever I need to create a new key and save the container name and public key to the database. Also, whenever I create a new key I would do the following:

public static string ConvertToNewKey(string oldPrivateKey)
{

    // get the current container name from the database...

    rsa.PersistKeyInCsp = false;
    rsa.Clear();
    rsa = null;

    string privateKey = AssignNewKey(true); // create the new public key and container name and write them to the database...

       // re-encrypt existing data to use the new keys and write to database...

    return privateKey;
}
public static string AssignNewKey(bool ReturnPrivateKey){
     string containerName = DateTime.Now.Ticks.ToString();
     // create the new key...
     // saves container name and public key to database...
     // and returns Private Key XML.
}

在创建新密钥之前.