protected internal virtual Asn1Encodable GenerateAsn1Parameters(string encryptionOid, byte[] encKeyBytes)
        {
            Asn1Encodable result = null;

            try
            {
                if (encryptionOid.Equals(CmsEnvelopedGenerator.RC2Cbc))
                {
                    byte[] array = new byte[8];
                    this.rand.NextBytes(array);
                    int num = encKeyBytes.Length * 8;
                    int parameterVersion;
                    if (num < 256)
                    {
                        parameterVersion = (int)CmsEnvelopedGenerator.rc2Table[num];
                    }
                    else
                    {
                        parameterVersion = num;
                    }
                    result = new RC2CbcParameter(parameterVersion, array);
                }
                else
                {
                    result = ParameterUtilities.GenerateParameters(encryptionOid, this.rand);
                }
            }
            catch (SecurityUtilityException)
            {
            }
            return(result);
        }
Esempio n. 2
0
        protected internal virtual Asn1Encodable GenerateAsn1Parameters(
            string encryptionOid,
            byte[]  encKeyBytes)
        {
            Asn1Encodable asn1Params = null;

            try
            {
                if (encryptionOid.Equals(RC2Cbc))
                {
                    byte[] iv = new byte[8];
                    rand.NextBytes(iv);

                    // TODO Is this detailed repeat of Java version really necessary?
                    int effKeyBits = encKeyBytes.Length * 8;
                    int parameterVersion;

                    if (effKeyBits < 256)
                    {
                        parameterVersion = rc2Table[effKeyBits];
                    }
                    else
                    {
                        parameterVersion = effKeyBits;
                    }

                    asn1Params = new RC2CbcParameter(parameterVersion, iv);
                }
                else
                {
                    asn1Params = ParameterUtilities.GenerateParameters(encryptionOid, rand);
                }
            }
            catch (SecurityUtilityException)
            {
                // No problem... no parameters generated
            }

            return(asn1Params);
        }
Esempio n. 3
0
    protected internal virtual Asn1Encodable GenerateAsn1Parameters(string encryptionOid, byte[] encKeyBytes)
    {
        Asn1Encodable result = null;

        try
        {
            if (!encryptionOid.Equals(RC2Cbc))
            {
                result = ParameterUtilities.GenerateParameters(encryptionOid, rand);
                return(result);
            }
            byte[] array = new byte[8];
            rand.NextBytes(array);
            int num = encKeyBytes.Length * 8;
            int parameterVersion = (num >= 256) ? num : rc2Table[num];
            result = new RC2CbcParameter(parameterVersion, array);
            return(result);
        }
        catch (SecurityUtilityException)
        {
            return(result);
        }
    }
        /// <summary>
        /// Generate an enveloped object that contains a CMS Enveloped Data
        /// object using the passed in key generator.
        /// </summary>
        private CmsEnvelopedData Generate(
            CmsProcessable content,
            string encryptionOid,
            CipherKeyGenerator keyGen)
        {
            AlgorithmIdentifier encAlgId = null;
            KeyParameter        encKey   = null;
            Asn1OctetString     encContent;

            try
            {
                IBufferedCipher cipher = CipherUtilities.GetCipher(encryptionOid);

                byte[] encKeyBytes = keyGen.GenerateKey();
                encKey = ParameterUtilities.CreateKeyParameter(encryptionOid, encKeyBytes);

                Asn1Encodable asn1Params = null;

                try
                {
                    if (encryptionOid.Equals(RC2Cbc))
                    {
                        // mix in a bit extra...
                        rand.SetSeed(DateTime.Now.Ticks);

                        byte[] iv = rand.GenerateSeed(8);

                        // TODO Is this detailed repeat of Java version really necessary?
                        int effKeyBits = encKeyBytes.Length * 8;
                        int parameterVersion;

                        if (effKeyBits < 256)
                        {
                            parameterVersion = rc2Table[effKeyBits];
                        }
                        else
                        {
                            parameterVersion = effKeyBits;
                        }

                        asn1Params = new RC2CbcParameter(parameterVersion, iv);
                    }
                    else
                    {
                        asn1Params = ParameterUtilities.GenerateParameters(encryptionOid, rand);
                    }
                }
                catch (SecurityUtilityException)
                {
                    // No problem... no parameters generated
                }


                Asn1Object        asn1Object;
                ICipherParameters cipherParameters;

                if (asn1Params != null)
                {
                    asn1Object       = asn1Params.ToAsn1Object();
                    cipherParameters = ParameterUtilities.GetCipherParameters(
                        encryptionOid, encKey, asn1Object);
                }
                else
                {
                    asn1Object       = DerNull.Instance;
                    cipherParameters = encKey;
                }


                encAlgId = new AlgorithmIdentifier(
                    new DerObjectIdentifier(encryptionOid),
                    asn1Object);


                cipher.Init(true, cipherParameters);

                MemoryStream bOut = new MemoryStream();
                CipherStream cOut = new CipherStream(bOut, null, cipher);

                content.Write(cOut);

                cOut.Close();

                encContent = new BerOctetString(bOut.ToArray());
            }
            catch (SecurityUtilityException e)
            {
                throw new CmsException("couldn't create cipher.", e);
            }
            catch (InvalidKeyException e)
            {
                throw new CmsException("key invalid in message.", e);
            }
            catch (IOException e)
            {
                throw new CmsException("exception decoding algorithm parameters.", e);
            }


            Asn1EncodableVector recipientInfos = new Asn1EncodableVector();

            foreach (RecipientInf recipient in recipientInfs)
            {
                try
                {
                    recipientInfos.Add(recipient.ToRecipientInfo(encKey));
                }
                catch (IOException e)
                {
                    throw new CmsException("encoding error.", e);
                }
                catch (InvalidKeyException e)
                {
                    throw new CmsException("key inappropriate for algorithm.", e);
                }
                catch (GeneralSecurityException e)
                {
                    throw new CmsException("error making encrypted content.", e);
                }
            }

            EncryptedContentInfo eci = new EncryptedContentInfo(
                PkcsObjectIdentifiers.Data,
                encAlgId,
                encContent);

            Asn1.Cms.ContentInfo contentInfo = new Asn1.Cms.ContentInfo(
                PkcsObjectIdentifiers.EnvelopedData,
                new EnvelopedData(null, new DerSet(recipientInfos), eci, null));

            return(new CmsEnvelopedData(contentInfo));
        }
Esempio n. 5
0
        /// <summary>
        /// Generate an enveloped object that contains an CMS Enveloped Data
        /// object using the passed in key generator.
        /// </summary>
        private Stream Open(
            Stream outStream,
            string encryptionOid,
            CipherKeyGenerator keyGen)
        {
            Asn1Encodable asn1Params = null;

            byte[]       encKeyBytes = keyGen.GenerateKey();
            KeyParameter encKey      = ParameterUtilities.CreateKeyParameter(encryptionOid, encKeyBytes);

            try
            {
                if (encryptionOid.Equals(RC2Cbc))
                {
                    // mix in a bit extra...
                    rand.SetSeed(DateTime.Now.Ticks);

                    byte[] iv = rand.GenerateSeed(8);

                    // TODO Is this detailed repeat of Java version really necessary?
                    int effKeyBits = encKeyBytes.Length * 8;
                    int parameterVersion;

                    if (effKeyBits < 256)
                    {
                        parameterVersion = rc2Table[effKeyBits];
                    }
                    else
                    {
                        parameterVersion = effKeyBits;
                    }

                    asn1Params = new RC2CbcParameter(parameterVersion, iv);
                }
                else
                {
                    asn1Params = ParameterUtilities.GenerateParameters(encryptionOid, rand);
                }
            }
            catch (SecurityUtilityException)
            {
                // No problem... no parameters generated
            }


            Asn1EncodableVector recipientInfos = new Asn1EncodableVector();

            foreach (RecipientInf recipient in recipientInfs)
            {
                try
                {
                    recipientInfos.Add(recipient.ToRecipientInfo(encKey));
                }
                catch (IOException e)
                {
                    throw new CmsException("encoding error.", e);
                }
                catch (InvalidKeyException e)
                {
                    throw new CmsException("key inappropriate for algorithm.", e);
                }
                catch (GeneralSecurityException e)
                {
                    throw new CmsException("error making encrypted content.", e);
                }
            }

            return(Open(outStream, encryptionOid, encKey, asn1Params, recipientInfos));
        }