Armor.cs :  » Network-Clients » SharpPrivacyLibrary » SharpPrivacy » OpenPGP » 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 » Network Clients » SharpPrivacyLibrary 
SharpPrivacyLibrary » SharpPrivacy » OpenPGP » Armor.cs
//
// This file is part of the source code distribution of SharpPrivacy.
// SharpPrivacy is an Open Source OpenPGP implementation and can be 
// found at http://www.sharpprivacy.net
// It is released under Gnu General Public License and can be used 
// and modified as long as the result is released under GPL too. 
// For a copy of the GPL, please go to www.gnu.org/copyleft/gpl.html 
//
// Armor.cs: 
//   Class for handling the openPGP ascii armor.
//
// Author:
//  Daniel Fabian (df@sharpprivacy.net)
//
//
// Version: 0.1.0 (initial release)
//
// Changelog:
//  - 11.03.2003: Created this file.
//  - 01.06.2003: Added this header for the first beta release.
//
// (C) 2003, Daniel Fabian
//
using System;

namespace SharpPrivacy.OpenPGP{
  
  public class Armor {
  
    public static string RemoveClearSignatureArmor(string strMessage, ref ArmorTypes atType, ref string strSignature) {
      string[] strLines = strMessage.Split('\n');
      string strReturn = "";
      
      /* Codes for Positions:
       *  0: Not in an OpenPGP Message
       *  1: Armor Headers
       *  2: OpenPGP Message
       *  3: PGP Signature
       */
      int iPosition = 0;
      
      for (int i=0; i<strLines.Length; i++) {
        if ((strLines[i].Length > 10) && (strLines[i].Substring(0, 2).Trim() == "--") && (iPosition == 2)) {
          strLines[i] = strLines[i].Trim();
          iPosition = 0;
          switch (strLines[i]) {
            case "-----BEGIN PGP SIGNATURE-----":
              string[] strRestLines = new string[strLines.Length - i];
              Array.Copy(strLines, i, strRestLines, 0, strLines.Length - (i+1));
              string strRest = String.Join("\n", strRestLines);
              strSignature = Armor.RemoveArmor(strRest, ref atType, ref strRest);
              return strReturn.Substring(0, strReturn.Length - 2);
              
            default:
              iPosition = 2;
              break;
          }
        }
        
        if (iPosition == 2) {
          strReturn += strLines[i].TrimEnd(null) + "\r\n";
        }
        
        if ((iPosition == 1) && (strLines[i].Trim().Length == 0)) {
          iPosition = 2;
        }
        
        if ((strLines[i].Length > 10) && (strLines[i].Trim().Substring(0, 2) == "--") && (iPosition == 0)) {
          strLines[i] = strLines[i].Trim();
          iPosition = 1;
          switch (strLines[i]) {
            case "-----BEGIN PGP SIGNED MESSAGE-----":
              atType = ArmorTypes.OpenPGPSignature;
              break;
            
            default:
              iPosition = 0;
              break;
          }
        }
      }
      
      return "";
    }
    
    public static string RemoveArmor(string strMessage, ref ArmorTypes atType, ref string strRest) {
      string[] strLines = strMessage.Split('\n');
      string strReturn = "";
      bool foundSignedMessage = false;
      
      /* Codes for Positions:
       *  0: Not in an OpenPGP Message
       *  1: Armor Headers
       *  2: OpenPGP Message
       */
      int iPosition = 0;
      
      for (int i=0; i<strLines.Length; i++) {
        if ((strLines[i].Length > 10) && (strLines[i].Substring(0, 2).Trim() == "--") && (iPosition == 2)) {
          strLines[i] = strLines[i].Trim();
          iPosition = 0;
          switch (strLines[i]) {
            case "-----END PGP MESSAGE-----":
            case "-----END PGP PUBLIC KEY BLOCK-----":
            case "-----END PGP PRIVATE KEY BLOCK-----":
            case "-----END PGP SIGNATURE-----":
              string[] strRestLines = new string[strLines.Length - (i+1)];
              Array.Copy(strLines, i+1, strRestLines, 0, strLines.Length - (i+1));
              strRest = String.Join("\n", strRestLines);
              return strReturn;
              
            default:
              iPosition = 2;
              break;
          }
        }
        
        if (iPosition == 2) {
          strReturn += strLines[i];
        }
        
        if ((iPosition == 1) && (strLines[i].Trim().Length == 0)) {
          iPosition = 2;
        }
        
        if ((strLines[i].Length > 10) && (strLines[i].Trim().Substring(0, 2) == "--") && (iPosition == 0)) {
          strLines[i] = strLines[i].Trim();
          iPosition = 1;
          switch (strLines[i]) {
            case "-----BEGIN PGP MESSAGE-----":
              atType = ArmorTypes.OpenPGPMessage;
              break;
            
            case "-----BEGIN PGP PUBLIC KEY BLOCK-----":
              atType = ArmorTypes.PublicKeyBlock;
              break;
            
            case "-----BEGIN PGP PRIVATE KEY BLOCK-----":
              atType = ArmorTypes.PrivateKeyBlock;
              break;
            
            case "-----BEGIN PGP SIGNATURE-----":
              if(!foundSignedMessage)
              {
                atType = ArmorTypes.OpenPGPSignature;
              }
              break;

            case "-----BEGIN PGP SIGNED MESSAGE-----":
              atType = ArmorTypes.OpenPGPSignedMessage;
              iPosition = 0;
              foundSignedMessage = true;
              break;
            
            default:
              iPosition = 0;
              break;
          }
        }
      }
      
      return "";
      
    }
    
    /// <summary>
    /// Armors an Radix64 encoded secret key.
    /// </summary>
    /// <param name="strKey">The secret key encoded in radix64.</param>
    /// <returns>Returns the armored secret key.</returns>
    public static string WrapPrivateKey(string strKey) {
      string strReturn = "-----BEGIN PGP PRIVATE KEY BLOCK-----\r\n";
      strReturn += "Version: " + SharpPrivacyLibrary.ApplicationVersionInfos + "\r\n\r\n";
      strReturn += strKey;
      strReturn += "-----END PGP PRIVATE KEY BLOCK-----\r\n\r\n";
      
      return strReturn;
    }
    
    /// <summary>
    /// Armors an OpenPGP encoded secret key.
    /// </summary>
    /// <param name="bKey">The secret key encoded in OpenPGP format.</param>
    /// <returns>Returns the armored secret key.</returns>
    public static string WrapPrivateKey(byte[] bKey) {
      return WrapPrivateKey(Radix64.Encode(bKey, true));
    }
    
    /// <summary>
    /// Armors an Radix64 encoded public key.
    /// </summary>
    /// <param name="strKey">The public key encoded in radix64.</param>
    /// <returns>Returns the armored public key.</returns>
    public static string WrapPublicKey(string strKey) {
      string strReturn = "-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n";
      strReturn += "Version: " + SharpPrivacyLibrary.ApplicationVersionInfos + "\r\n\r\n";
      strReturn += strKey;
      strReturn += "-----END PGP PUBLIC KEY BLOCK-----\r\n\r\n";
      
      return strReturn;
    }
    
    /// <summary>
    /// Armors an OpenPGP encoded public key.
    /// </summary>
    /// <param name="bKey">The public key encoded in OpenPGP format.</param>
    /// <returns>Returns the armored public key.</returns>
    public static string WrapPublicKey(byte[] bKey) {
      return WrapPublicKey(Radix64.Encode(bKey, true));
    }

    /// <summary>
    /// Armors an Radix64 encoded OpenPGP message.
    /// </summary>
    /// <param name="strKey">The OpenPGP message encoded in radix64.</param>
    /// <returns>Returns the armored OpenPGP message.</returns>
    public static string WrapMessage(string strMessage) {
      string strReturn = "-----BEGIN PGP MESSAGE-----\r\n";
      strReturn += "Version: " + SharpPrivacyLibrary.ApplicationVersionInfos + "\r\n\r\n";
      strReturn += strMessage;
      strReturn += "-----END PGP MESSAGE-----\r\n";
      
      return strReturn;
    }
    
    /// <summary>
    /// Armors a binary formated OpenPGP message.
    /// </summary>
    /// <param name="bKey">The OpenPGP message in binary form.</param>
    /// <returns>Returns the armored OpenPGP message.</returns>
    public static string WrapMessage(byte[] bMessage) {
      return WrapMessage(Radix64.Encode(bMessage, true));
    }
    
    /// <summary>
    /// Armors an OpenPGP Cleartextsignature.
    /// </summary>
    /// <param name="strMessage">The Message that has been signed. Note that it
    /// must not be Dash Escaped before handing it to this function.</param>
    /// <param name="strSignature">The OpenPGP signature of the given 
    /// message formated in Radix64.</param>
    /// <returns>The armored OpenPGP cleartext signature.</returns>
    public static string WrapCleartextSignature(string strMessage, string strSignature) {
      string strFinal = "-----BEGIN PGP SIGNED MESSAGE-----\r\n";
      strFinal += "Hash: SHA1\r\n\r\n";
      strFinal += Radix64.DashEscape(strMessage);
      strFinal += "\r\n-----BEGIN PGP SIGNATURE-----\r\n";
      strFinal += "Version: " + SharpPrivacyLibrary.ApplicationVersionInfos + "\r\n\r\n";
      strFinal += strSignature;
      strFinal += "-----END PGP SIGNATURE-----\r\n";  
      
      return strFinal;
    }

    /// <summary>
    /// Armors an OpenPGP Cleartextsignature.
    /// </summary>
    /// <param name="strSignature">The OpenPGP signature of the given 
    /// message formated in Radix64.</param>
    /// <returns>The armored OpenPGP cleartext signature.</returns>
    public static string WrapCleartextSignature(string strSignature) 
    {
      string strFinal = "\r\n-----BEGIN PGP SIGNATURE-----\r\n";
      strFinal += "Version: " + SharpPrivacyLibrary.ApplicationVersionInfos + "\r\n\r\n";
      strFinal += strSignature;
      strFinal += "-----END PGP SIGNATURE-----\r\n";  
      
      return strFinal;
    }
    
    /// <summary>
    /// Armors an OpenPGP Cleartextsignature.
    /// </summary>
    /// <param name="strMessage">The Message that has been signed. Note that it
    /// must not be Dash Escaped before handing it to this function.</param>
    /// <param name="bSignature">The OpenPGP signature of the given 
    /// message formated in binary.</param>
    /// <returns>The armored OpenPGP cleartext signature.</returns>
    public static string WrapCleatextSignature(string strMessage, byte[] bSignature) {
      return WrapCleartextSignature(strMessage, Radix64.Encode(bSignature, 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.