Example #1
0
        /// <summary>
        /// 生成RSA密钥对,公钥就是私钥的Modulus
        /// </summary>
        /// <param name="dwKeySize">密钥长度,默认2048</param>
        /// <returns>RSA密钥对(公钥, 私钥)</returns>
        public static (string publicKey, string privateKey) GenRSAKeyPair(int dwKeySize = 2048)
        {
            RSA rsa = RSA.Create();

            rsa.KeySize = dwKeySize;

            RSAParameters rsaParams = rsa.ExportParameters(true);
            // 公钥
            var publicKey = Convert.ToBase64String(rsaParams.Modulus);
            // 私钥
            var privateKey = rsaParams.ToJson();

            return(publicKey, privateKey);
        }