Pkcs12Utilities.cs :  » PDF » iTextSharp » Org » BouncyCastle » Pkcs » 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 » Pkcs » Pkcs12Utilities.cs
using System;
using System.IO;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Pkcs{
  /**
   * Utility class for reencoding PKCS#12 files to definite length.
   */
  public class Pkcs12Utilities
  {
    /**
     * Just re-encode the outer layer of the PKCS#12 file to definite length encoding.
     *
     * @param berPKCS12File - original PKCS#12 file
     * @return a byte array representing the DER encoding of the PFX structure
     * @throws IOException
     */
    public static byte[] ConvertToDefiniteLength(
      byte[] berPkcs12File)
    {
      Pfx pfx = new Pfx(Asn1Sequence.GetInstance(Asn1Object.FromByteArray(berPkcs12File)));

      return pfx.GetEncoded(Asn1Encodable.Der);
    }

    /**
    * Re-encode the PKCS#12 structure to definite length encoding at the inner layer
    * as well, recomputing the MAC accordingly.
    *
    * @param berPKCS12File - original PKCS12 file.
    * @param provider - provider to use for MAC calculation.
    * @return a byte array representing the DER encoding of the PFX structure.
    * @throws IOException on parsing, encoding errors.
    */
    public static byte[] ConvertToDefiniteLength(
      byte[]  berPkcs12File,
      char[]  passwd)
    {
      Pfx pfx = new Pfx(Asn1Sequence.GetInstance(Asn1Object.FromByteArray(berPkcs12File)));

      ContentInfo info = pfx.AuthSafe;

      Asn1OctetString content = Asn1OctetString.GetInstance(info.Content);
      Asn1Object obj = Asn1Object.FromByteArray(content.GetOctets());

      info = new ContentInfo(info.ContentType, new DerOctetString(obj.GetEncoded(Asn1Encodable.Der)));

      MacData mData = pfx.MacData;

      try
      {
        int itCount = mData.IterationCount.IntValue;
        byte[] data = Asn1OctetString.GetInstance(info.Content).GetOctets();
        byte[] res = Pkcs12Store.CalculatePbeMac(
          mData.Mac.AlgorithmID.ObjectID, mData.GetSalt(), itCount, passwd, false, data);

        AlgorithmIdentifier algId = new AlgorithmIdentifier(
          mData.Mac.AlgorithmID.ObjectID, DerNull.Instance);
        DigestInfo dInfo = new DigestInfo(algId, res);

        mData = new MacData(dInfo, mData.GetSalt(), itCount);
      }
      catch (Exception e)
      {
        throw new IOException("error constructing MAC: " + e.ToString());
      }

      pfx = new Pfx(info, mData);

      return pfx.GetEncoded(Asn1Encodable.Der);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.