コード例 #1
0
        /// <summary>根据私钥生成RSA
        /// </summary>
        private static RSA CreateRsaFromPrivateKey(string privateKey)
        {
            RSAParameters rsaParams = RsaUtil.ReadPrivateKeyInfo(privateKey);
            var           rsa       = RSA.Create();

            rsa.ImportParameters(rsaParams);
            return(rsa);

            #region 原方法
            //var privateKeyBits = Convert.FromBase64String(privateKey);
            //var rsa = RSA.Create();
            //var rsaParams = new RSAParameters();

            //using (var binr = new BinaryReader(new MemoryStream(privateKeyBits)))
            //{
            //    byte bt = 0;
            //    ushort twobytes = 0;
            //    twobytes = binr.ReadUInt16();
            //    if (twobytes == 0x8130)
            //    {
            //        binr.ReadByte();
            //    }
            //    else if (twobytes == 0x8230)
            //    {
            //        binr.ReadInt16();
            //    }
            //    else
            //    {
            //        throw new Exception("Unexpected value read binr.ReadUInt16()");
            //    }
            //    twobytes = binr.ReadUInt16();
            //    if (twobytes != 0x0102)
            //    {
            //        throw new Exception("Unexpected version");
            //    }
            //    bt = binr.ReadByte();
            //    if (bt != 0x00)
            //    {
            //        throw new Exception("Unexpected value read binr.ReadByte()");
            //    }
            //    rsaParams.Modulus = binr.ReadBytes(GetIntegerSize(binr));
            //    rsaParams.Exponent = binr.ReadBytes(GetIntegerSize(binr));
            //    rsaParams.D = binr.ReadBytes(GetIntegerSize(binr));
            //    rsaParams.P = binr.ReadBytes(GetIntegerSize(binr));
            //    rsaParams.Q = binr.ReadBytes(GetIntegerSize(binr));
            //    rsaParams.DP = binr.ReadBytes(GetIntegerSize(binr));
            //    rsaParams.DQ = binr.ReadBytes(GetIntegerSize(binr));
            //    rsaParams.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
            //}
            //rsa.ImportParameters(rsaParams);
            //return rsa;
            #endregion
        }
コード例 #2
0
        /// <summary>根据公钥生成RSA
        /// </summary>
        private static RSA CreateRsaFromPublicKey(string publicKey)
        {
            var rsaParams = RsaUtil.ReadPublicKeyInfo(publicKey);
            var rsa       = RSA.Create();

            rsa.ImportParameters(rsaParams);
            return(rsa);

            #region 原方法
            ////1.2.840.113549.1.1.1
            //byte[] seqOid = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
            //var x509Key = Convert.FromBase64String(publicKey);
            //using (var mem = new MemoryStream(x509Key))
            //{
            //    using (var binr = new BinaryReader(mem))
            //    {
            //        byte bt = 0;
            //        ushort twobytes = 0;

            //        twobytes = binr.ReadUInt16();
            //        if (twobytes == 0x8130)
            //        {
            //            binr.ReadByte();
            //        }
            //        else if (twobytes == 0x8230)
            //        {
            //            binr.ReadInt16();
            //        }
            //        else
            //        {
            //            return null;
            //        }
            //        var seq = binr.ReadBytes(15);
            //        if (!CompareByteArray(seq, seqOid))
            //        {
            //            return null;
            //        }

            //        twobytes = binr.ReadUInt16();
            //        if (twobytes == 0x8103)
            //        {
            //            binr.ReadByte();
            //        }
            //        else if (twobytes == 0x8203)
            //        {
            //            binr.ReadInt16();
            //        }
            //        else
            //        {
            //            return null;
            //        }
            //        bt = binr.ReadByte();
            //        if (bt != 0x00)
            //        {
            //            return null;
            //        }
            //        twobytes = binr.ReadUInt16();
            //        if (twobytes == 0x8130)
            //        {
            //            binr.ReadByte();
            //        }
            //        else if (twobytes == 0x8230)
            //        {
            //            binr.ReadInt16();
            //        }
            //        else
            //        {
            //            return null;
            //        }
            //        twobytes = binr.ReadUInt16();
            //        byte lowbyte = 0x00;
            //        byte highbyte = 0x00;

            //        if (twobytes == 0x8102)
            //        {
            //            lowbyte = binr.ReadByte();
            //        }
            //        else if (twobytes == 0x8202)
            //        {
            //            highbyte = binr.ReadByte();
            //            lowbyte = binr.ReadByte();
            //        }
            //        else
            //        {
            //            return null;
            //        }
            //        byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
            //        int modsize = BitConverter.ToInt32(modint, 0);

            //        int firstbyte = binr.PeekChar();
            //        if (firstbyte == 0x00)
            //        {
            //            binr.ReadByte();
            //            modsize -= 1;
            //        }
            //        byte[] modulus = binr.ReadBytes(modsize);
            //        if (binr.ReadByte() != 0x02)
            //        {
            //            return null;
            //        }
            //        int expbytes = (int)binr.ReadByte();
            //        byte[] exponent = binr.ReadBytes(expbytes);
            //        var rsa = RSA.Create();
            //        var rsaKeyInfo = new RSAParameters
            //        {
            //            Modulus = modulus,
            //            Exponent = exponent
            //        };
            //        rsa.ImportParameters(rsaKeyInfo);
            //        return rsa;
            //    }

            //}
            #endregion
        }