DKIM.OpenSslKey.DecodeRSAPrivateKey C# (CSharp) Method

DecodeRSAPrivateKey() private method

private DecodeRSAPrivateKey ( [ privkey ) : RSACryptoServiceProvider
privkey [
return System.Security.Cryptography.RSACryptoServiceProvider
        public static RSACryptoServiceProvider DecodeRSAPrivateKey([NotNull]byte[] privkey)
        {
            if (privkey == null)
            {
                throw new ArgumentNullException("privkey");
            }

            byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;

            // ---------  Set up stream to decode the asn.1 encoded RSA private key  ------
            //var mem = new MemoryStream(privkey);
            using (var binr = new BinaryReader(new MemoryStream(privkey)))    //wrap Memory Stream with BinaryReader for easy reading
            {

                ushort twobytes = binr.ReadUInt16();
                if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
                    binr.ReadByte(); //advance 1 byte
                else if (twobytes == 0x8230)
                    binr.ReadInt16(); //advance 2 bytes
                else
                    return null;

                twobytes = binr.ReadUInt16();
                if (twobytes != 0x0102) //version number
                    return null;
                byte bt = binr.ReadByte();
                if (bt != 0x00)
                    return null;


                //------  all private key components are Integer sequences ----
                int elems = GetIntegerSize(binr);
                MODULUS = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                E = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                D = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                P = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                Q = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                DP = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                DQ = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                IQ = binr.ReadBytes(elems);




                // ------- create RSACryptoServiceProvider instance and initialize with public key -----
                var RSA = new RSACryptoServiceProvider();
                var RSAparams = new RSAParameters
                                    {
                                        Modulus = MODULUS,
                                        Exponent = E,
                                        D = D,
                                        P = P,
                                        Q = Q,
                                        DP = DP,
                                        DQ = DQ,
                                        InverseQ = IQ
                                    };
                RSA.ImportParameters(RSAparams);
                return RSA;

            }
        }

Usage Example

Exemplo n.º 1
0
        public byte[] Sign(byte[] data)
        {
            using (var rsa = OpenSslKey.DecodeRSAPrivateKey(_key))
            {
                byte[] signature = rsa.SignData(data, _algorithmInfo.SigningAlgorithm == SigningAlgorithm.RSASha1 ? "SHA1" : "SHA256");

                return(signature);
            }
        }
All Usage Examples Of DKIM.OpenSslKey::DecodeRSAPrivateKey