예제 #1
0
        /// <summary>
        /// 用公钥给数据进行RSA解密
        /// </summary>
        /// <param name="xmlPublicKey"> 公钥(XML格式字符串) </param>
        /// <param name="strDecryptString"> 要解密数据 </param>
        /// <returns> 解密后的数据 </returns>
        public static string PublicKeyDecrypt(string xmlPublicKey, string strDecryptString)
        {
            RSAHelper rs  = new RSAHelper(RSAType.RSA, Encoding.UTF8, "", xmlPublicKey);
            var       xml = rs._publicKeyRsaProvider.ToXmlString(false);

            MAX_DECRYPT_BLOCK = rs._publicKeyRsaProvider.KeySize / 8;

            //加载公钥
            RSACryptoServiceProvider publicRsa = new RSACryptoServiceProvider();

            publicRsa.FromXmlString(xml);
            RSAParameters rp = publicRsa.ExportParameters(false);

            //转换密钥
            AsymmetricKeyParameter pbk = DotNetUtilities.GetRsaPublicKey(rp);

            IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");

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

            byte[] DataToDecrypt = Convert.FromBase64String(strDecryptString);
            //byte[] outBytes = c.DoFinal(DataToDecrypt);//解密

            //string strDec = Encoding.UTF8.GetString(outBytes);

            byte[] cache;
            int    time     = 0;//次数
            int    inputLen = DataToDecrypt.Length;
            int    offSet   = 0;

            using (MemoryStream outStream = new MemoryStream())
            {
                while (inputLen - offSet > 0)
                {
                    if (inputLen - offSet > MAX_DECRYPT_BLOCK)
                    {
                        cache = c.DoFinal(DataToDecrypt, offSet, MAX_DECRYPT_BLOCK);
                    }
                    else
                    {
                        cache = c.DoFinal(DataToDecrypt, offSet, inputLen - offSet);
                    }
                    //写入
                    outStream.Write(cache, 0, cache.Length);

                    time++;
                    offSet = time * MAX_DECRYPT_BLOCK;
                }
                byte[] resData = outStream.ToArray();

                string strDec = Encoding.UTF8.GetString(resData);
                return(strDec);
            }
        }
예제 #2
0
        /// <summary>
        /// 用私钥给数据进行RSA加密
        /// </summary>
        /// <param name="xmlPrivateKey"></param>
        /// <param name="strEncryptString"></param>
        /// <returns></returns>
        public static string PrivateKeyEncrypt(string xmlPrivateKey, string strEncryptString)
        {
            RSAHelper rs  = new RSAHelper(RSAType.RSA, Encoding.UTF8, xmlPrivateKey);
            var       xml = rs._privateKeyRsaProvider.ToXmlString(true);

            MAX_ENCRYPT_BLOCK = rs._privateKeyRsaProvider.KeySize / 8 - 11;
            //加载私钥
            RSACryptoServiceProvider privateRsa = new RSACryptoServiceProvider();

            privateRsa.FromXmlString(xml);

            //转换密钥
            AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetKeyPair(privateRsa);
            IBufferedCipher         c       = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding"); //使用RSA/ECB/PKCS1Padding格式

            c.Init(true, keyPair.Private);                                                       //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥
            byte[] DataToEncrypt = Encoding.UTF8.GetBytes(strEncryptString);                     //获取字节

            byte[] cache;
            int    time     = 0;//次数
            int    inputLen = DataToEncrypt.Length;
            int    offSet   = 0;

            using (MemoryStream outStream = new MemoryStream())
            {
                while (inputLen - offSet > 0)
                {
                    if (inputLen - offSet > MAX_ENCRYPT_BLOCK)
                    {
                        cache = c.DoFinal(DataToEncrypt, offSet, MAX_ENCRYPT_BLOCK);
                    }
                    else
                    {
                        cache = c.DoFinal(DataToEncrypt, offSet, inputLen - offSet);
                    }
                    //写入
                    outStream.Write(cache, 0, cache.Length);

                    time++;
                    offSet = time * MAX_ENCRYPT_BLOCK;
                }
                byte[] resData = outStream.ToArray();

                string strBase64 = Convert.ToBase64String(resData);
                return(strBase64);
            }
        }
예제 #3
0
        /// <summary>
        /// 公钥验签
        /// </summary>
        /// <param name="str">待验证的字符串</param>
        /// <param name="sign">加签之后的字符串</param>
        /// <param name="publicKey">公钥</param>
        /// <param name="signAlgType">hash算法</param>
        /// <returns>签名是否符合</returns>
        public static bool Verify(string str, string sign, string publicKey, SignAlgType signAlgType)
        {
            byte[] bt      = Encoding.UTF8.GetBytes(str);
            byte[] rgbHash = null;
            switch (signAlgType)
            {
            case SignAlgType.SHA1:
            {
                var csp = SHA1.Create();
                rgbHash = csp.ComputeHash(bt);
            }
            break;

            case SignAlgType.SHA256:
            {
                var csp = SHA256.Create();
                rgbHash = csp.ComputeHash(bt);
            }
            break;

            case SignAlgType.MD5:
            {
                var csp = MD5.Create();
                rgbHash = csp.ComputeHash(bt);
            }
            break;

            default:
                break;
            }
            RSACryptoServiceProvider key = new RSACryptoServiceProvider();
            RSAHelper rs  = new RSAHelper(RSAType.RSA, Encoding.UTF8, "", publicKey);
            var       xml = rs._publicKeyRsaProvider.ToXmlString(false);

            key.FromXmlString(xml);
            RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);

            deformatter.SetHashAlgorithm(signAlgType.ToString());
            byte[] rgbSignature = Convert.FromBase64String(sign);
            if (deformatter.VerifySignature(rgbHash, rgbSignature))
            {
                return(true);
            }
            return(false);
        }
예제 #4
0
        /// <summary>
        /// 私钥签名
        /// </summary>
        /// <param name="str">需签名的数据</param>
        /// <param name="privateKey">私钥</param>
        /// <param name="signAlgType">hash算法</param>
        /// <returns>签名后的值</returns>
        public static string Sign(string str, string privateKey, SignAlgType signAlgType)
        {
            //根据需要加签时的哈希算法转化成对应的hash字符节
            byte[] bt      = Encoding.UTF8.GetBytes(str);
            byte[] rgbHash = null;
            switch (signAlgType)
            {
            case SignAlgType.SHA1:
            {
                var csp = SHA1.Create();
                rgbHash = csp.ComputeHash(bt);
            }
            break;

            case SignAlgType.SHA256:
            {
                var csp = SHA256.Create();
                rgbHash = csp.ComputeHash(bt);
            }
            break;

            case SignAlgType.MD5:
            {
                var csp = MD5.Create();
                rgbHash = csp.ComputeHash(bt);
            }
            break;

            default:
                break;
            }
            RSACryptoServiceProvider key = new RSACryptoServiceProvider();

            RSAHelper rs  = new RSAHelper(RSAType.RSA, Encoding.UTF8, privateKey);
            var       xml = rs._privateKeyRsaProvider.ToXmlString(true);

            key.FromXmlString(xml);
            RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);

            formatter.SetHashAlgorithm(signAlgType.ToString());//此处是你需要加签的hash算法,需要和上边你计算的hash值的算法一致,不然会报错。
            byte[] inArray = formatter.CreateSignature(rgbHash);
            return(Convert.ToBase64String(inArray));
        }