示例#1
0
        internal static RSA DecodeRSA(byte[] rawPublicKey)
        {
            RSAParameters parameters = default(RSAParameters);

            try
            {
                ASN1 asn = new ASN1(rawPublicKey);
                if (asn.Count == 0)
                {
                    throw new CryptographicException(Locale.GetText("Missing RSA modulus and exponent."));
                }
                ASN1 asn2 = asn[0];
                if (asn2 == null || asn2.Tag != 2)
                {
                    throw new CryptographicException(Locale.GetText("Missing RSA modulus."));
                }
                ASN1 asn3 = asn[1];
                if (asn3.Tag != 2)
                {
                    throw new CryptographicException(Locale.GetText("Missing RSA public exponent."));
                }
                parameters.Modulus  = PublicKey.GetUnsignedBigInteger(asn2.Value);
                parameters.Exponent = asn3.Value;
            }
            catch (Exception inner)
            {
                string text = Locale.GetText("Error decoding the ASN.1 structure.");
                throw new CryptographicException(text, inner);
            }
            int dwKeySize = parameters.Modulus.Length << 3;
            RSA rsa       = new RSACryptoServiceProvider(dwKeySize);

            rsa.ImportParameters(parameters);
            return(rsa);
        }
示例#2
0
        internal static DSA DecodeDSA(byte[] rawPublicKey, byte[] rawParameters)
        {
            DSAParameters parameters = default(DSAParameters);

            try
            {
                ASN1 asn = new ASN1(rawPublicKey);
                if (asn.Tag != 2)
                {
                    throw new CryptographicException(Locale.GetText("Missing DSA Y integer."));
                }
                parameters.Y = PublicKey.GetUnsignedBigInteger(asn.Value);
                ASN1 asn2 = new ASN1(rawParameters);
                if (asn2 == null || asn2.Tag != 48 || asn2.Count < 3)
                {
                    throw new CryptographicException(Locale.GetText("Missing DSA parameters."));
                }
                if (asn2[0].Tag != 2 || asn2[1].Tag != 2 || asn2[2].Tag != 2)
                {
                    throw new CryptographicException(Locale.GetText("Invalid DSA parameters."));
                }
                parameters.P = PublicKey.GetUnsignedBigInteger(asn2[0].Value);
                parameters.Q = PublicKey.GetUnsignedBigInteger(asn2[1].Value);
                parameters.G = PublicKey.GetUnsignedBigInteger(asn2[2].Value);
            }
            catch (Exception inner)
            {
                string text = Locale.GetText("Error decoding the ASN.1 structure.");
                throw new CryptographicException(text, inner);
            }
            DSA dsa = new DSACryptoServiceProvider(parameters.Y.Length << 3);

            dsa.ImportParameters(parameters);
            return(dsa);
        }