Пример #1
0
        static void Main(string[] args)
        {
            //RSAKeyFormatConvert.RSAKeyFormatConverter.ConvertKeyPemToXml();
            var key1           = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"primary.txt");
            var privateKeyText = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"primary.txt");//.Replace("\n", "").Replace("-----END PRIVATE KEY-----", "").Replace("-----BEGIN PRIVATE KEY-----", "");
            //var r = new RSACrypto(privateKeyText);
            //var s = r.Encrypt("4716439d5ab0f4b82d526162e8a0c50a40398eaca5adf2fe6161aad8cb52b862");
            var xml  = RSAKeyConvert.RSAPrivateKeyJava2DotNet(privateKeyText);
            var key2 = RSAKeyConvert.RSAPrivateKeyDotNet2Java(xml);

            var data = "4716439d5ab0f4b82d526162e8a0c50a40398eaca5adf2fe6161aad8cb52b862";
            var v    = new RSACryption_New().RSAEncryptByPrivateKey(xml, data);
            var aa   = "DNyn3vyHO2dKzN6pu1oE3VYg0DKkdUe1bySKwLEJ47mUT1M/AyDl+pOkXxCSM3pWNh+mMdf5NhSgoWWNw8AFfRsIbaoBAyaXcYj5UpnZcpOfxQ47xiAofq9kLQKAk9spuYbZgSooIzRIyi4qhLWkdqqFntCgDlXklLVBjgoRkME=";
            var vv   = new RSACryption_New().RSADecryptByPrivateKey(xml, aa);

            if (vv == data)
            {
                Console.WriteLine("验签正确!");
            }
            else
            {
                Console.WriteLine("验签失败!");
            }
            Console.Read();
        }
Пример #2
0
        /// <summary>
        /// 用私钥给数据进行RSA加密(PEM格式)
        /// </summary>
        /// <param name="pemPrivateKey">pem格式私钥</param>
        /// <param name="strEncryptString">加密字符串</param>
        /// <returns></returns>
        public string RSAEncryptByPrivateKeyPem(string pemPrivateKey, string strEncryptString)
        {
            var xmlPrivateKey = RSAKeyConvert.RSAPrivateKeyJava2DotNet(pemPrivateKey.Replace("\n", "").Replace("-----END PRIVATE KEY-----", "").Replace("-----BEGIN PRIVATE KEY-----", "")).Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END ENCRYPTED PRIVATE KEY-----", "");
            //加载私钥
            RSACryptoServiceProvider privateRsa = new RSACryptoServiceProvider();

            privateRsa.FromXmlString(xmlPrivateKey);

            //转换密钥
            AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetKeyPair(privateRsa);

            IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");// 参数与Java中加密解密的参数一致

            //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥
            c.Init(true, keyPair.Private);

            byte[] DataToEncrypt = Encoding.UTF8.GetBytes(strEncryptString);
            byte[] outBytes      = c.DoFinal(DataToEncrypt);//加密
            string strBase64     = Convert.ToBase64String(outBytes);

            return(strBase64);
        }