Exemplo n.º 1
0
        public static RSAParameters ImportRSAPublicKey(string buf)
        {
            RSAParameters rsaParameters = new RSACryptoServiceProvider().ExportParameters(false);

            byte[]       buffer       = Convert.FromBase64String(string.Join(string.Empty, ((IEnumerable <string>)buf.Split(Environment.NewLine.ToArray <char>())).Where <string>(line => !line.Trim().StartsWith("-")).ToArray <string>()));
            MemoryStream memoryStream = new MemoryStream();

            memoryStream.Write(buffer, 0, buffer.Length);
            memoryStream.Seek(0L, SeekOrigin.Begin);
            using (BinaryReader stream = new BinaryReader(memoryStream))
            {
                if (stream.ReadByte() != 48)
                {
                    return(rsaParameters);
                }

                UASecurity.DecodeLength(stream);
                byte[] numArray = UASecurity.DecodeIntBigEndian(stream);
                if (numArray.Length != 1 || numArray[0] > 0)
                {
                    return(rsaParameters);
                }

                rsaParameters.Modulus  = UASecurity.DecodeIntBigEndian(stream);
                rsaParameters.Exponent = UASecurity.DecodeIntBigEndian(stream);
                UASecurity.DecodeIntBigEndian(stream);
                UASecurity.DecodeIntBigEndian(stream);
                UASecurity.DecodeIntBigEndian(stream);
                UASecurity.DecodeIntBigEndian(stream);
                UASecurity.DecodeIntBigEndian(stream);
                UASecurity.DecodeIntBigEndian(stream);
            }
            return(rsaParameters);
        }
Exemplo n.º 2
0
        private static byte[] DecodeIntBigEndian(BinaryReader stream)
        {
            if (stream.ReadByte() != 2)
            {
                return(null);
            }

            int length = UASecurity.DecodeLength(stream);

            if (length < 0)
            {
                return(null);
            }

            byte[] numArray = new byte[length];
            for (int index = 0; index < length; ++index)
            {
                numArray[index] = stream.ReadByte();
            }

            return(numArray);
        }