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

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Bcpg{
  /// <remarks>Basic packet for a PGP secret key.</remarks>
    public class SecretKeyPacket
        : ContainedPacket //, PublicKeyAlgorithmTag
    {
    public const int UsageNone = 0x00;
    public const int UsageChecksum = 0xff;
    public const int UsageSha1 = 0xfe;

    private PublicKeyPacket pubKeyPacket;
        private readonly byte[] secKeyData;
    private int s2kUsage;
    private SymmetricKeyAlgorithmTag encAlgorithm;
        private S2k s2k;
        private byte[] iv;

    internal SecretKeyPacket(
            BcpgInputStream bcpgIn)
        {
      if (this is SecretSubkeyPacket)
      {
        pubKeyPacket = new PublicSubkeyPacket(bcpgIn);
      }
      else
      {
        pubKeyPacket = new PublicKeyPacket(bcpgIn);
      }

      s2kUsage = bcpgIn.ReadByte();

      if (s2kUsage == UsageChecksum || s2kUsage == UsageSha1)
            {
                encAlgorithm = (SymmetricKeyAlgorithmTag) bcpgIn.ReadByte();
                s2k = new S2k(bcpgIn);
            }
            else
            {
                encAlgorithm = (SymmetricKeyAlgorithmTag) s2kUsage;
      }

      if (!(s2k != null && s2k.Type == S2k.GnuDummyS2K && s2k.ProtectionMode == 0x01))
            {
        if (s2kUsage != 0)
        {
                    if (((int) encAlgorithm) < 7)
                    {
                        iv = new byte[8];
                    }
                    else
                    {
                        iv = new byte[16];
                    }
                    bcpgIn.ReadFully(iv);
                }
            }

      secKeyData = bcpgIn.ReadAll();
        }

    public SecretKeyPacket(
            PublicKeyPacket        pubKeyPacket,
            SymmetricKeyAlgorithmTag  encAlgorithm,
            S2k              s2k,
            byte[]            iv,
            byte[]            secKeyData)
        {
            this.pubKeyPacket = pubKeyPacket;
            this.encAlgorithm = encAlgorithm;

      if (encAlgorithm != SymmetricKeyAlgorithmTag.Null)
      {
        this.s2kUsage = UsageChecksum;
      }
      else
      {
        this.s2kUsage = UsageNone;
      }

      this.s2k = s2k;
      this.iv = Arrays.Clone(iv);
      this.secKeyData = secKeyData;
        }

    public SecretKeyPacket(
      PublicKeyPacket        pubKeyPacket,
      SymmetricKeyAlgorithmTag  encAlgorithm,
      int              s2kUsage,
      S2k              s2k,
      byte[]            iv,
      byte[]            secKeyData)
    {
      this.pubKeyPacket = pubKeyPacket;
      this.encAlgorithm = encAlgorithm;
      this.s2kUsage = s2kUsage;
      this.s2k = s2k;
      this.iv = Arrays.Clone(iv);
      this.secKeyData = secKeyData;
    }

    public SymmetricKeyAlgorithmTag EncAlgorithm
        {
      get { return encAlgorithm; }
        }

    public int S2kUsage
    {
      get { return s2kUsage; }
    }

    public byte[] GetIV()
        {
            return Arrays.Clone(iv);
        }

    public S2k S2k
        {
      get { return s2k; }
        }

    public PublicKeyPacket PublicKeyPacket
        {
      get { return pubKeyPacket; }
        }

    public byte[] GetSecretKeyData()
        {
            return secKeyData;
        }

    public byte[] GetEncodedContents()
        {
            MemoryStream bOut = new MemoryStream();
            BcpgOutputStream pOut = new BcpgOutputStream(bOut);

            pOut.Write(pubKeyPacket.GetEncodedContents());

      pOut.WriteByte((byte) s2kUsage);

      if (s2kUsage == UsageChecksum || s2kUsage == UsageSha1)
            {
                pOut.WriteByte((byte) encAlgorithm);
                pOut.WriteObject(s2k);
            }

      if (iv != null)
            {
                pOut.Write(iv);
            }

            if (secKeyData != null && secKeyData.Length > 0)
            {
                pOut.Write(secKeyData);
            }

            return bOut.ToArray();
        }

        public override void Encode(
            BcpgOutputStream bcpgOut)
        {
            bcpgOut.WritePacket(PacketTag.SecretKey, GetEncodedContents(), true);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.