示例#1
0
        public DSACryptoServiceProvider(int dwKeySize, CspParameters parameters)
        {
            byte[] key;
            int    result;

            // The Microsoft documentation is a little fuzzy as to
            // when this class retrieves a key, generates a new key,
            // or simply waits for the program to supply parameters
            // using "ImportParameters".
            //
            // If we are given a key container name, we ask the runtime
            // engine for the corresponding key.  The engine can either
            // return the key, say that there is no key, reject access
            // because the user doesn't want the program to use the key,
            // or tell the code to generate a new key for the user.
            //
            // If we are not given a key container name, we assume that
            // the program will be supplying the parameters later.
            //
            if (parameters != null && parameters.KeyContainerName != null)
            {
                // Attempt to get a DSA key from the user's keychain.
                key = CryptoMethods.GetKey(CryptoMethods.DSASign,
                                           parameters.KeyContainerName,
                                           parameters.Flags,
                                           out result);
                if (key != null)
                {
                    // The "ASN1ToPublic" method will determine if
                    // the key is X.509, bare public, or private.
                    dsaParams.ASN1ToPublic(key, 0, key.Length);
                    Array.Clear(key, 0, key.Length);
                    persistKey = true;
                }
                else if (result == CryptoMethods.UnknownKey)
                {
                    throw new CryptographicException
                              (_("Crypto_UnknownKey"),
                              parameters.KeyContainerName);
                }
                else if (result == CryptoMethods.NotPermitted)
                {
                    throw new CryptographicException
                              (_("Crypto_NoKeyAccess"),
                              parameters.KeyContainerName);
                }
                else if (result == CryptoMethods.GenerateKey)
                {
                    // Generate a new key for the user.
                    // TODO
                }
            }
        }
示例#2
0
        public RSACryptoServiceProvider(int dwKeySize, CspParameters parameters)
        {
            byte[] key;
            int    result;

            // See "DSACryptoServiceProvider" for explainatory comments.
            if (parameters != null && parameters.KeyContainerName != null)
            {
                // Attempt to get an RSA key from the user's keychain.
                key = CryptoMethods.GetKey(CryptoMethods.RSAEncrypt,
                                           parameters.KeyContainerName,
                                           parameters.Flags,
                                           out result);
                if (key != null)
                {
                    // The "ASN1ToPublic" method will determine if
                    // the key is X.509, bare public, or private.
                    rsaParams.ASN1ToPublic(key, 0, key.Length);
                    Array.Clear(key, 0, key.Length);
                    persistKey = true;
                }
                else if (result == CryptoMethods.UnknownKey)
                {
                    throw new CryptographicException
                              (_("Crypto_UnknownKey"),
                              parameters.KeyContainerName);
                }
                else if (result == CryptoMethods.NotPermitted)
                {
                    throw new CryptographicException
                              (_("Crypto_NoKeyAccess"),
                              parameters.KeyContainerName);
                }
                else if (result == CryptoMethods.GenerateKey)
                {
                    // Generate a new key for the user.
                    // TODO
                }
            }
        }