/// <summary>Decodes the given Radix64-encoded String /// to a byte array that is filled with the decoded binary /// data.<br></br> /// The parameter strData has to have a checksum appended. /// This checksum is also verified. /// </summary> /// <exception cref="ApplicationException"> /// An ApplicationException is thrown when the parameter strData /// did not have a checksum appended, or if the checksum was /// incorrect. /// </exception> /// <param name="strData">Radix64 encoded String</param> /// <returns>Returns a byte array containing the decoded /// base64 data.</returns> /// <remarks>No remarks</remarks> public static byte[] Decode(string strData) { //Get the checksum of the message int nChecksumstart = strData.LastIndexOf('='); string strCRC = ""; try { strCRC = strData.Substring(nChecksumstart + 1, 4); } catch { throw new System.ApplicationException("The Radix64 checksum was wrong!"); } //Strip the checksum from the real message strData = strData.Substring(0, nChecksumstart); byte[] bData = Convert.FromBase64String(strData); string strShouldCRC = Radix64.CRC_Checksum(bData).Trim(); if (strShouldCRC != strCRC) { throw new System.ApplicationException("The Radix64 checksum was wrong!"); } return(bData); }
/// <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); }
private static string CRC_Checksum(byte[] bData) { long crc = 0xb704ce; byte[] bCRC = new byte[3]; for (int i = 0; i < bData.Length; i++) { crc = (crc << 8) ^ CRCTable[((crc >> 16) ^ bData[i]) & 0xff]; } crc &= 0xffffff; bCRC[0] = (byte)((crc & 0xFF0000) >> 16); bCRC[1] = (byte)((crc & 0x00FF00) >> 8); bCRC[2] = (byte)((crc & 0x0000FF)); return(Radix64.Encode(bCRC, false)); }
/// <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))); }
/// <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 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> /// Static function that makes parsing packets easy. Just give /// a Radix64 encoded representation of a set of OpenPGP packets /// as parameter, and the function returns an array of packets /// that are the parsed OpenPGP packets. /// </summary> /// <param name="strBase64Text">A string containing the Radix64 /// representation of a set of OpenPGP packets</param> /// <returns>Returns an array of packets</returns> /// <remarks>No remarks</remarks> public static Packet[] ParsePackets(string strBase64Text) { byte[] bData = Radix64.Decode(strBase64Text); return(ParsePackets(bData)); }