PasswordRecipientInformation.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 » PasswordRecipientInformation.cs
using System;
using System.IO;

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

namespace Org.BouncyCastle.Cms{
  /**
   * the RecipientInfo class for a recipient who has been sent a message
   * encrypted using a password.
   */
  public class PasswordRecipientInformation
    : RecipientInformation
  {
    private readonly PasswordRecipientInfo  info;

    [Obsolete]
    public PasswordRecipientInformation(
      PasswordRecipientInfo  info,
      AlgorithmIdentifier    encAlg,
      Stream          data)
      : this(info, encAlg, null, null, data)
    {
    }

    [Obsolete]
    public PasswordRecipientInformation(
      PasswordRecipientInfo  info,
      AlgorithmIdentifier    encAlg,
      AlgorithmIdentifier    macAlg,
      Stream          data)
      : this(info, encAlg, macAlg, null, data)
    {
    }

    public PasswordRecipientInformation(
      PasswordRecipientInfo  info,
      AlgorithmIdentifier    encAlg,
      AlgorithmIdentifier    macAlg,
      AlgorithmIdentifier    authEncAlg,
      Stream          data)
      : base(encAlg, macAlg, authEncAlg, info.KeyEncryptionAlgorithm, data)
    {
      this.info = info;
      this.rid = new RecipientID();
    }

    /**
     * return the object identifier for the key derivation algorithm, or null
     * if there is none present.
     *
     * @return OID for key derivation algorithm, if present.
     */
    public virtual AlgorithmIdentifier KeyDerivationAlgorithm
    {
      get { return info.KeyDerivationAlgorithm; }
    }

    /**
     * decrypt the content and return an input stream.
     */
    public override CmsTypedStream GetContentStream(
      ICipherParameters key)
    {
      try
      {
        AlgorithmIdentifier kekAlg = AlgorithmIdentifier.GetInstance(info.KeyEncryptionAlgorithm);
        Asn1Sequence        kekAlgParams = (Asn1Sequence)kekAlg.Parameters;
        byte[]              encryptedKey = info.EncryptedKey.GetOctets();
        string              kekAlgName = DerObjectIdentifier.GetInstance(kekAlgParams[0]).Id;
        string        cName = CmsEnvelopedHelper.Instance.GetRfc3211WrapperName(kekAlgName);
        IWrapper      keyWrapper = WrapperUtilities.GetWrapper(cName);

        byte[] iv = Asn1OctetString.GetInstance(kekAlgParams[1]).GetOctets();

        ICipherParameters parameters = ((CmsPbeKey)key).GetEncoded(kekAlgName);
        parameters = new ParametersWithIV(parameters, iv);

        keyWrapper.Init(false, parameters);

        AlgorithmIdentifier aid = GetActiveAlgID();

        string alg = aid.ObjectID.Id;

        KeyParameter sKey = ParameterUtilities.CreateKeyParameter(
          alg, keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length));

        return GetContentFromSessionKey(sKey);
      }
      catch (SecurityUtilityException e)
      {
        throw new CmsException("couldn't create cipher.", e);
      }
      catch (InvalidKeyException e)
      {
        throw new CmsException("key invalid in message.", e);
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.