private void repeatHeaderTest() { MemoryStream bOut = new MemoryStream(); ArmoredOutputStream aOut = new ArmoredOutputStream(bOut); aOut.SetHeader("Comment", "Line 1"); aOut.AddHeader("Comment", "Line 2"); aOut.Write(sample, 0, sample.Length); aOut.Close(); MemoryStream bIn = new MemoryStream(bOut.ToArray(), false); ArmoredInputStream aIn = new ArmoredInputStream(bIn, true); string[] hdrs = aIn.GetArmorHeaders(); int count = 0; for (int i = 0; i != hdrs.Length; i++) { if (hdrs[i].IndexOf("Comment: ") == 0) { count++; } } IsEquals(2, count); }
public string PublicKey(string email, Dictionary <string, string> headers) { Context = new CryptoContext(Context); var publicKey = GetPublicKeyForEncryption(email); var sigKey = GetSecretKeyForSigning(email); var literalData = new PgpLiteralDataGenerator(); var data = publicKey.GetEncoded(); using (var sout = new MemoryStream()) { using (var armoredOut = new ArmoredOutputStream(sout)) { foreach (var header in headers) { armoredOut.SetHeader(header.Key, header.Value); } //using (var literalOut = literalData.Open( // armoredOut, // PgpLiteralData.Binary, // "email", // data.Length, // DateTime.UtcNow)) //{ // literalOut.Write(data, 0, data.Length); //} armoredOut.Write(data); } return(ASCIIEncoding.ASCII.GetString(sout.ToArray())); } }
private string signEnvelopeData(string msg) { Stream privateKeyStream = getPrivateKeyStream(_privateKey); MemoryStream result = new MemoryStream(); ArmoredOutputStream aOut = new ArmoredOutputStream(result); BcpgOutputStream bOut = null; char[] privateKeyPassword = _passPhrase.ToCharArray(); var utf8Encoding = new System.Text.UTF8Encoding(); try { PgpSecretKey sk = readSecretKey(privateKeyStream); PgpPrivateKey pk = sk.ExtractPrivateKey(privateKeyPassword); PgpSignatureGenerator sigGen = new PgpSignatureGenerator(sk.PublicKey.Algorithm, HashAlgorithmTag.Sha256); PgpSignatureSubpacketGenerator spGen = new PgpSignatureSubpacketGenerator(); var enumerator = sk.PublicKey.GetUserIds().GetEnumerator(); if (enumerator.MoveNext()) { spGen.SetSignerUserId(false, (string)enumerator.Current); sigGen.SetHashedSubpackets(spGen.Generate()); } aOut.BeginClearText(HashAlgorithmTag.Sha256); sigGen.InitSign(PgpSignature.CanonicalTextDocument, pk); byte[] msgBytes = utf8Encoding.GetBytes(msg); sigGen.Update(msgBytes, 0, msgBytes.Length); aOut.Write(msgBytes, 0, msgBytes.Length); bOut = new BcpgOutputStream(aOut); aOut.EndClearText(); sigGen.Generate().Encode(bOut); using (BinaryReader br = new BinaryReader(result)) { br.BaseStream.Position = 0; return(utf8Encoding.GetString(br.ReadBytes((int)result.Length))); } } catch (Exception e) { Console.WriteLine("This happened: " + e.Message); throw new Exception("Signing Failed: " + e.Message); } finally { try { if (privateKeyStream != null) { privateKeyStream.Close(); } //if(bOut != null) //bOut.Close(); //aOut.Close(); result.Close(); } catch (IOException) {} } }
string PGPEncryptToASCIIArmored(byte[] data, string filename = "encrypted-data.gpg") { using (var encOut = new MemoryStream()) { var byteData = GPGTools.EncryptForKeys(data, keys, filename); var s = new ArmoredOutputStream(encOut); s.Write(byteData, 0, byteData.Length); s.Close(); encOut.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(encOut); return(reader.ReadToEnd()); } }
private string Export(byte[] key) { MemoryStream bOut = new MemoryStream(); using (ArmoredOutputStream armorOut = new ArmoredOutputStream(bOut)) { armorOut.Write(key); armorOut.Flush(); } bOut.Position = 0; var reader = new StreamReader(bOut); return(reader.ReadToEnd()); }
public static string ByteKeyToText(byte[] publicKey) { byte[] output; using (MemoryStream outstream = new MemoryStream()) { ArmoredOutputStream armor = new ArmoredOutputStream(outstream); armor.Write(publicKey, 0, publicKey.Length); armor.Flush(); outstream.Flush(); output = outstream.ToArray(); } return(Encoding.UTF8.GetString(output, 0, output.Length)); }
public string PublicKey(string email, Dictionary <string, string> headers) { Context = new CryptoContext(Context); var publicKey = GetPublicKeyForEncryption(email); var data = publicKey.GetEncoded(); using (var sout = new MemoryStream()) { using (var armoredOut = new ArmoredOutputStream(sout)) { foreach (var header in headers) { armoredOut.SetHeader(header.Key, header.Value); } armoredOut.Write(data); } return(ASCIIEncoding.ASCII.GetString(sout.ToArray())); } }
public override void PerformTest() { // // test immediate close // MemoryStream bOut = new MemoryStream(); ArmoredOutputStream aOut = new ArmoredOutputStream(bOut); aOut.Close(); byte[] data = bOut.ToArray(); if (data.Length != 0) { Fail("No data should have been written"); } // // multiple close // bOut = new MemoryStream(); aOut = new ArmoredOutputStream(bOut); aOut.Write(sample, 0, sample.Length); aOut.Close(); aOut.Close(); int mc = markerCount(bOut.ToArray()); if (mc < 1) { Fail("No end marker found"); } if (mc > 1) { Fail("More than one end marker found"); } // // writing and reading single objects // bOut = new MemoryStream(); aOut = new ArmoredOutputStream(bOut); aOut.Write(sample, 0, sample.Length); aOut.Close(); ArmoredInputStream aIn = new ArmoredInputStream( new MemoryStream(bOut.ToArray(), false)); PgpObjectFactory fact = new PgpObjectFactory(aIn); int count = 0; while (fact.NextPgpObject() != null) { count++; } if (count != 1) { Fail("wrong number of objects found: " + count); } // // writing and reading multiple objects - in single block // bOut = new MemoryStream(); aOut = new ArmoredOutputStream(bOut); aOut.Write(sample, 0, sample.Length); aOut.Write(sample, 0, sample.Length); aOut.Close(); aIn = new ArmoredInputStream( new MemoryStream(bOut.ToArray(), false)); fact = new PgpObjectFactory(aIn); count = 0; while (fact.NextPgpObject() != null) { count++; } if (count != 2) { Fail("wrong number of objects found: " + count); } // // writing and reading multiple objects - in single block // bOut = new MemoryStream(); aOut = new ArmoredOutputStream(bOut); aOut.Write(sample, 0, sample.Length); aOut.Close(); // does not close underlying stream aOut = new ArmoredOutputStream(bOut); aOut.Write(sample, 0, sample.Length); aOut.Close(); aIn = new ArmoredInputStream( new MemoryStream(bOut.ToArray(), false)); count = 0; bool atLeastOne; do { atLeastOne = false; fact = new PgpObjectFactory(aIn); while (fact.NextPgpObject() != null) { atLeastOne = true; count++; } }while (atLeastOne); if (count != 2) { Fail("wrong number of objects found: " + count); } blankLineTest(); pgpUtilTest(); repeatHeaderTest(); }
/// <summary> /// Sign data using key /// </summary> /// <param name="data">Data to sign</param> /// <param name="key">Email address of key</param> /// <returns>Returns ascii armored signature</returns> public string SignClear(string data, string key, Encoding encoding, Dictionary <string, string> headers) { Context = new CryptoContext(Context); var senderKey = GetSecretKeyForSigning(key); if (senderKey == null) { throw new SecretKeyNotFoundException("Error, unable to locate signing key \"" + key + "\"."); } // Setup signature stuff // var signatureData = new PgpSignatureGenerator(senderKey.PublicKey.Algorithm, HashAlgorithmTag.Sha1); signatureData.InitSign(PgpSignature.CanonicalTextDocument, senderKey.ExtractPrivateKey(Context.Password)); foreach (string userId in senderKey.PublicKey.GetUserIds()) { var subPacketGenerator = new PgpSignatureSubpacketGenerator(); subPacketGenerator.SetSignerUserId(false, userId); signatureData.SetHashedSubpackets(subPacketGenerator.Generate()); // Just the first one! break; } // // using (var sout = new MemoryStream()) { using (var armoredOut = new ArmoredOutputStream(sout)) { foreach (var header in headers) { armoredOut.SetHeader(header.Key, header.Value); } armoredOut.BeginClearText(HashAlgorithmTag.Sha1); // Remove any extra trailing whitespace. // this should not include \r or \n. data = data.TrimEnd(null); using (var stringReader = new StringReader(data)) { do { var line = stringReader.ReadLine(); if (line == null) { break; } // Lines must have all white space removed line = line.TrimEnd(null); line = line.TrimEnd(new char[] { ' ', '\t', '\r', '\n' }); line += "\r\n"; signatureData.Update(encoding.GetBytes(line)); armoredOut.Write(encoding.GetBytes(line)); }while (true); } // Write extra line before signature block. armoredOut.Write(encoding.GetBytes("\r\n")); armoredOut.EndClearText(); using (var outputStream = new BcpgOutputStream(armoredOut)) { signatureData.Generate().Encode(outputStream); } } return(encoding.GetString(sout.ToArray())); } }
public override void PerformTest() { // // test immediate close // MemoryStream bOut = new MemoryStream(); ArmoredOutputStream aOut = new ArmoredOutputStream(bOut); aOut.Close(); byte[] data = bOut.ToArray(); if (data.Length != 0) { Fail("No data should have been written"); } // // multiple close // bOut = new MemoryStream(); aOut = new ArmoredOutputStream(bOut); aOut.Write(sample, 0, sample.Length); aOut.Close(); aOut.Close(); int mc = markerCount(bOut.ToArray()); if (mc < 1) { Fail("No end marker found"); } if (mc > 1) { Fail("More than one end marker found"); } // // writing and reading single objects // bOut = new MemoryStream(); aOut = new ArmoredOutputStream(bOut); aOut.Write(sample, 0, sample.Length); aOut.Close(); ArmoredInputStream aIn = new ArmoredInputStream( new MemoryStream(bOut.ToArray(), false)); PgpObjectFactory fact = new PgpObjectFactory(aIn); int count = 0; while (fact.NextPgpObject() != null) { count++; } if (count != 1) { Fail("wrong number of objects found: " + count); } // // writing and reading multiple objects - in single block // bOut = new MemoryStream(); aOut = new ArmoredOutputStream(bOut); aOut.Write(sample, 0, sample.Length); aOut.Write(sample, 0, sample.Length); aOut.Close(); aIn = new ArmoredInputStream( new MemoryStream(bOut.ToArray(), false)); fact = new PgpObjectFactory(aIn); count = 0; while (fact.NextPgpObject() != null) { count++; } if (count != 2) { Fail("wrong number of objects found: " + count); } // // writing and reading multiple objects - in single block // bOut = new MemoryStream(); aOut = new ArmoredOutputStream(bOut); aOut.Write(sample, 0, sample.Length); aOut.Close(); // does not close underlying stream aOut = new ArmoredOutputStream(bOut); aOut.Write(sample, 0, sample.Length); aOut.Close(); aIn = new ArmoredInputStream( new MemoryStream(bOut.ToArray(), false)); count = 0; bool atLeastOne; do { atLeastOne = false; fact = new PgpObjectFactory(aIn); while (fact.NextPgpObject() != null) { atLeastOne = true; count++; } } while (atLeastOne); if (count != 2) { Fail("wrong number of objects found: " + count); } blankLineTest(); }