CMSEnvelopedHelper.cs :  » PDF » iTextSharp » Org » BouncyCastle » Cms » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Cms » CMSEnvelopedHelper.cs
using System;
using System.Collections;
using System.IO;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;

namespace Org.BouncyCastle.Cms{
  class CmsEnvelopedHelper
  {
    internal static readonly CmsEnvelopedHelper Instance = new CmsEnvelopedHelper();

    private static readonly IDictionary KeySizes = new Hashtable();
    private static readonly IDictionary BaseCipherNames = new Hashtable();

    static CmsEnvelopedHelper()
    {
      KeySizes.Add(CmsEnvelopedGenerator.DesEde3Cbc, 192);
      KeySizes.Add(CmsEnvelopedGenerator.Aes128Cbc, 128);
      KeySizes.Add(CmsEnvelopedGenerator.Aes192Cbc, 192);
      KeySizes.Add(CmsEnvelopedGenerator.Aes256Cbc, 256);

      BaseCipherNames.Add(CmsEnvelopedGenerator.DesEde3Cbc,  "DESEDE");
      BaseCipherNames.Add(CmsEnvelopedGenerator.Aes128Cbc,  "AES");
      BaseCipherNames.Add(CmsEnvelopedGenerator.Aes192Cbc,  "AES");
      BaseCipherNames.Add(CmsEnvelopedGenerator.Aes256Cbc,  "AES");
    }

    private string GetAsymmetricEncryptionAlgName(
      string encryptionAlgOid)
    {
      if (Asn1.Pkcs.PkcsObjectIdentifiers.RsaEncryption.Id.Equals(encryptionAlgOid))
      {
        return "RSA/ECB/PKCS1Padding";
      }

      return encryptionAlgOid;    
    }

    internal IBufferedCipher CreateAsymmetricCipher(
      string encryptionOid)
    {
      try
      {
        return CipherUtilities.GetCipher(encryptionOid);
      }
      catch (SecurityUtilityException)
      {
        return CipherUtilities.GetCipher(GetAsymmetricEncryptionAlgName(encryptionOid));
      }
    }

    internal IWrapper CreateWrapper(
      string encryptionOid)
    {
      try
      {
        return WrapperUtilities.GetWrapper(encryptionOid);
      }
      catch (SecurityUtilityException)
      {
        return WrapperUtilities.GetWrapper(GetAsymmetricEncryptionAlgName(encryptionOid));
      }
    }

    internal string GetRfc3211WrapperName(
      string oid)
    {
      if (oid == null)
        throw new ArgumentNullException("oid");

      string alg = (string) BaseCipherNames[oid];

      if (alg == null)
        throw new ArgumentException("no name for " + oid, "oid");

      return alg + "RFC3211Wrap";
    }

    internal int GetKeySize(
      string oid)
    {
      if (!KeySizes.Contains(oid))
      {
        throw new ArgumentException("no keysize for " + oid, "oid");
      }

      return (int) KeySizes[oid];
    }

    internal static IList ReadRecipientInfos(
      Asn1Set        recipientInfos,
      byte[]        contentOctets,
      AlgorithmIdentifier  encAlg,
      AlgorithmIdentifier  macAlg,
      AlgorithmIdentifier  authEncAlg)
    {
      IList infos = new ArrayList();
      foreach (Asn1Encodable ae in recipientInfos)
            {
                RecipientInfo info = RecipientInfo.GetInstance(ae);
        MemoryStream contentStream = new MemoryStream(contentOctets, false);

        ReadRecipientInfo(infos, info, contentStream, encAlg, macAlg, authEncAlg);
      }
      return infos;
    }
    
    internal static IList ReadRecipientInfos(
      IEnumerable      recipientInfoIter,
      Stream        contentStream,
      AlgorithmIdentifier  encAlg,
      AlgorithmIdentifier  macAlg,
      AlgorithmIdentifier  authEncAlg)
    {
      IList infos = new ArrayList();
      foreach (RecipientInfo info in recipientInfoIter)
      {
        ReadRecipientInfo(infos, info, contentStream, encAlg, macAlg, authEncAlg);
      }
      return infos;
    }

    private static void ReadRecipientInfo(
      IList        infos,
      RecipientInfo    info,
      Stream        contentStream,
      AlgorithmIdentifier  encAlg,
      AlgorithmIdentifier  macAlg,
      AlgorithmIdentifier  authEncAlg)
    {
      Asn1Encodable recipInfo = info.Info;
      if (recipInfo is KeyTransRecipientInfo)
      {
        infos.Add(new KeyTransRecipientInformation(
          (KeyTransRecipientInfo)recipInfo, encAlg, macAlg, authEncAlg, contentStream));
      }
      else if (recipInfo is KekRecipientInfo)
      {
        infos.Add(new KekRecipientInformation(
          (KekRecipientInfo)recipInfo, encAlg, macAlg, authEncAlg, contentStream));
      }
      else if (recipInfo is KeyAgreeRecipientInfo)
      {
        KeyAgreeRecipientInformation.ReadRecipientInfo(infos,
          (KeyAgreeRecipientInfo)recipInfo, encAlg, macAlg, authEncAlg, contentStream);
      }
      else if (recipInfo is PasswordRecipientInfo)
      {
        infos.Add(new PasswordRecipientInformation(
          (PasswordRecipientInfo)recipInfo, encAlg, macAlg, authEncAlg, contentStream));
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.