Exemple #1
0
        /// <summary>
        /// 生成 Base64 编码的公钥和私钥。
        /// </summary>
        /// <returns></returns>
        public static SignInfo CreateSignInfo()
        {
            SignInfo info = new SignInfo();

            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            info.PrivateKey = Convert.ToBase64String(RSAalg.ExportCspBlob(true));
            info.PublicKey  = Convert.ToBase64String(RSAalg.ExportCspBlob(false));

            return(info);
        }
Exemple #2
0
        /// <summary>
        /// 控制台输出测试信息
        /// </summary>
        /// <param name="dataToSign"></param>
        public static void ConsoleDebug(string dataToSign)
        {
            try
            {
                //string dataToSign = @"Data to Sign!Data to Sign!Data to Sign!";
                App.Info("原文:" + dataToSign);
                App.Info("长度:" + dataToSign.Length.ToString());
                App.Info("");

                SignInfo info            = CreateSignInfo();
                string   str_Private_Key = info.PrivateKey;
                string   str_Public_Key  = info.PublicKey;;
                App.Info("公钥:" + str_Public_Key);
                App.Info("");
                App.Info("私钥:" + str_Private_Key);
                App.Info("");

                string str_SignedData = RSASign.HashAndSign(dataToSign, str_Private_Key);// Hash and sign the data.
                App.Info("签名数据:" + str_SignedData);
                App.Info("");

                if (RSASign.VerifySignedHash(dataToSign, str_SignedData, str_Public_Key))
                {
                    App.Info("验证签名OK.");
                }
                else
                {
                    App.Info("签名不匹配!");
                }

                App.Info("");
            }
            catch (Exception)
            {
                App.Info("The data was not signed or verified");
            }
        }