Basic output stream.
Inheritance: Org.BouncyCastle.Utilities.IO.BaseOutputStream, IBcpgOutputStream
Esempio n. 1
0
		internal static void Encode(
			BcpgOutputStream	bcpgOut,
			BigInteger			val)
		{
			bcpgOut.WriteShort((short) val.BitLength);
			bcpgOut.Write(val.ToByteArrayUnsigned());
		}
		internal OutputStreamPacket(
            BcpgOutputStream bcpgOut)
        {
			if (bcpgOut == null)
				throw new ArgumentNullException("bcpgOut");

			this.bcpgOut = bcpgOut;
        }
Esempio n. 3
0
        public virtual byte[] GetEncoded()
        {
            MemoryStream bOut = new MemoryStream();
            BcpgOutputStream pOut = new BcpgOutputStream(bOut);

			pOut.WriteObject(this);

			return bOut.ToArray();
        }
Esempio n. 4
0
        public byte[] GetEncoded()
        {
            MemoryStream bOut = new MemoryStream();
            BcpgOutputStream pOut = new BcpgOutputStream(bOut);

			pOut.WritePacket(this);

			return bOut.ToArray();
        }
        public override void Encode(
            BcpgOutputStream bcpgOut)
        {
            byte[] oid = this.oid.GetEncoded();
            bcpgOut.Write(oid, 1, oid.Length - 1);

            MPInteger point = new MPInteger(this.point);
            bcpgOut.WriteObject(point);
        }
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     base.Encode(bcpgOut);
     bcpgOut.WriteByte(0x3);
     bcpgOut.WriteByte(reserved);
     bcpgOut.WriteByte(hashFunctionId);
     bcpgOut.WriteByte(symAlgorithmId);
 }
 /// <summary>
 /// Gets the encoded version of this instance.
 /// </summary>
 /// <returns></returns>
 public byte[] GetEncoded()
 {
     using (var bOut = new MemoryStream())
     {
         using (var pOut = new BcpgOutputStream(bOut))
         {
             pOut.WritePacket(this);
             return bOut.ToArray();
         }
     }
 }
        public override void Encode(
            BcpgOutputStream bcpgOut)
        {
            MemoryStream bOut = new MemoryStream();

            for (int i = 0; i != subpackets.Length; i++)
            {
                subpackets[i].Encode(bOut);
            }

            bcpgOut.WritePacket(PacketTag.UserAttribute, bOut.ToArray(), false);
        }
 public byte[] GetEncoded()
 {
     try
     {
         using (var bOut = new MemoryStream())
         {
             using (var pOut = new BcpgOutputStream(bOut))
             {
                 pOut.WriteObject(this);
                 return bOut.ToArray();
             }
         }
     }
     catch (Exception)
     {
         return null;
     }
 }
		public override void Encode(
			BcpgOutputStream bcpgOut)
		{
			MemoryStream bOut = new MemoryStream();
			BcpgOutputStream pOut = new BcpgOutputStream(bOut);

			pOut.Write(
				(byte) version,
				(byte) sigType,
				(byte) hashAlgorithm,
				(byte) keyAlgorithm);

			pOut.WriteLong(keyId);

			pOut.WriteByte((byte) nested);

			bcpgOut.WritePacket(PacketTag.OnePassSignature, bOut.ToArray(), true);
		}
		public override void Encode(
			BcpgOutputStream bcpgOut)
		{
			MemoryStream bOut = new MemoryStream();
			BcpgOutputStream pOut = new BcpgOutputStream(bOut);

			pOut.WriteByte((byte) version);

			pOut.WriteLong(keyId);

			pOut.WriteByte((byte)algorithm);

			for (int i = 0; i != data.Length; i++)
			{
				MPInteger.Encode(pOut, data[i]);
			}

			bcpgOut.WritePacket(PacketTag.PublicKeyEncryptedSession , bOut.ToArray(), true);
		}
Esempio n. 12
0
        public override void Encode(
            BcpgOutputStream bcpgOut)
        {
            int length = val.BitLength;

            bcpgOut.WriteByte((byte) (length >> 8));
            bcpgOut.WriteByte((byte) length);

            byte[] bytes = val.ToByteArray();

            if (bytes[0] == 0)
            {
                bcpgOut.Write(bytes, 1, bytes.Length - 1);
            }
            else
            {
                bcpgOut.Write(bytes);
            }
        }
        public override void Encode(
            BcpgOutputStream bcpgOut)
        {
            MemoryStream bOut = new MemoryStream();
            BcpgOutputStream pOut = new BcpgOutputStream(bOut);

            pOut.Write(
				(byte) version,
				(byte) encAlgorithm);

			pOut.WriteObject(s2k);

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

			bcpgOut.WritePacket(PacketTag.SymmetricKeyEncryptedSessionKey, bOut.ToArray(), true);
        }
        public static void SignFile(Stream input, Stream outputStream, Stream keyIn, char[] pass)
        {
            var secretKey = ReadSecretKey(keyIn);
            var privateKey = secretKey.ExtractPrivateKey(pass);

            var signatureGenerator = new PgpSignatureGenerator(secretKey.PublicKey.Algorithm, HashAlgorithmTag.Sha1);
            var subpacketGenerator = new PgpSignatureSubpacketGenerator();

            signatureGenerator.InitSign(PgpSignature.CanonicalTextDocument, privateKey);
            foreach (string userId in secretKey.PublicKey.GetUserIds())
            {
                var signatureSubpacketGenerator = new PgpSignatureSubpacketGenerator();
                signatureSubpacketGenerator.SetSignerUserId(isCritical: false, userId: userId);
                signatureGenerator.SetHashedSubpackets(signatureSubpacketGenerator.Generate());
                // Just the first one!
                break;
            }

            // Closing armouredOutputStream does not close the underlying stream
            var armouredOutputStream = new ArmoredOutputStream(outputStream);
            using (var bcpgOutputStream = new BcpgOutputStream(armouredOutputStream))
            {
                armouredOutputStream.BeginClearText(HashAlgorithmTag.Sha1);

                int chr;
                while ((chr = input.ReadByte()) > 0)
                {
                    signatureGenerator.Update((byte)chr);
                    bcpgOutputStream.Write((byte)chr);
                }

                // For some reason we need to add a trailing newline
                bcpgOutputStream.Write((byte)'\n'); 

                armouredOutputStream.EndClearText();

                signatureGenerator.Generate().Encode(bcpgOutputStream);
            }
        }
Esempio n. 15
0
        /**
         * Return the byte encoding of the signature section.
         * @return uninterpreted signature bytes.
         */
        public byte[] GetSignatureBytes()
        {
            if (signatureEncoding != null)
            {
                return((byte[])signatureEncoding.Clone());
            }

            MemoryStream     bOut  = new MemoryStream();
            BcpgOutputStream bcOut = new BcpgOutputStream(bOut);

            foreach (MPInteger sigObj in signature)
            {
                try
                {
                    bcOut.WriteObject(sigObj);
                }
                catch (IOException e)
                {
                    throw new Exception("internal error: " + e);
                }
            }

            return(bOut.ToArray());
        }
Esempio n. 16
0
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     bcpgOut.WriteByte((byte)this.type);
     bcpgOut.WriteByte((byte)this.algorithm);
     if (this.type != 101)
     {
         if (this.type != 0)
         {
             bcpgOut.Write(this.iv);
         }
         if (this.type == 3)
         {
             bcpgOut.WriteByte((byte)this.itCount);
             return;
         }
     }
     else
     {
         bcpgOut.WriteByte(71);
         bcpgOut.WriteByte(78);
         bcpgOut.WriteByte(85);
         bcpgOut.WriteByte((byte)this.protectionMode);
     }
 }
Esempio n. 17
0
        public byte[] GetSignatureBytes()
        {
            if (this.signatureEncoding != null)
            {
                return((byte[])this.signatureEncoding.Clone());
            }
            MemoryStream     memoryStream     = new MemoryStream();
            BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream);

            MPInteger[] array = this.signature;
            for (int i = 0; i < array.Length; i++)
            {
                MPInteger bcpgObject = array[i];
                try
                {
                    bcpgOutputStream.WriteObject(bcpgObject);
                }
                catch (IOException arg)
                {
                    throw new Exception("internal error: " + arg);
                }
            }
            return(memoryStream.ToArray());
        }
Esempio n. 18
0
        public byte[] GetEncodedContents()
        {
            //IL_0000: Unknown result type (might be due to invalid IL or missing references)
            //IL_0006: Expected O, but got Unknown
            MemoryStream     val = new MemoryStream();
            BcpgOutputStream bcpgOutputStream = new BcpgOutputStream((Stream)(object)val);

            bcpgOutputStream.Write(pubKeyPacket.GetEncodedContents());
            ((Stream)bcpgOutputStream).WriteByte((byte)s2kUsage);
            if (s2kUsage == 255 || s2kUsage == 254)
            {
                ((Stream)bcpgOutputStream).WriteByte((byte)encAlgorithm);
                bcpgOutputStream.WriteObject(s2k);
            }
            if (iv != null)
            {
                bcpgOutputStream.Write(iv);
            }
            if (secKeyData != null && secKeyData.Length > 0)
            {
                bcpgOutputStream.Write(secKeyData);
            }
            return(val.ToArray());
        }
Esempio n. 19
0
        public byte[] GetEncodedContents()
        {
            MemoryStream bOut = new MemoryStream();
            BcpgOutputStream pOut = new BcpgOutputStream(bOut);

            pOut.WriteByte((byte) version);
            pOut.WriteInt((int) time);

			if (version <= 3)
            {
                pOut.WriteShort((short) validDays);
            }

			pOut.WriteByte((byte) algorithm);

			pOut.WriteObject((BcpgObject)key);

			return bOut.ToArray();
        }
Esempio n. 20
0
 private static void EncodeLengthAndData(BcpgOutputStream pOut, byte[] data)
 {
     pOut.WriteShort((short)data.Length);
     pOut.Write(data);
 }
Esempio n. 21
0
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.PublicSubkey, GetEncodedContents(), oldFormat: true);
 }
		public override void Encode(
            BcpgOutputStream bcpgOut)
        {
            bcpgOut.WritePacket(tag, contents, true);
        }
Esempio n. 23
0
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     bcpgOut.WriteShort((short)this.val.BitLength);
     bcpgOut.Write(this.val.ToByteArrayUnsigned());
 }
Esempio n. 24
0
 public abstract void Encode(BcpgOutputStream bcpgOut);
Esempio n. 25
0
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(tag, contents, oldFormat: true);
 }
Esempio n. 26
0
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.UserId, idData, oldFormat: true);
 }
Esempio n. 27
0
		public override void Encode(
            BcpgOutputStream bcpgOut)
        {
            bcpgOut.WritePacket(PacketTag.UserId, idData, true);
        }
Esempio n. 28
0
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.SecretSubkey, base.GetEncodedContents(), true);
 }
Esempio n. 29
0
		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) {}
			}
		}
        public override void Encode(
			BcpgOutputStream bcpgOut)
		{
			MemoryStream bOut = new MemoryStream();
			BcpgOutputStream pOut = new BcpgOutputStream(bOut);

			pOut.WriteByte((byte) version);

			pOut.WriteLong(keyId);

			pOut.WriteByte((byte)algorithm);

            for (int i = 0; i < data.Length; ++i)
            {
                pOut.Write(data[i]);
            }

            Platform.Dispose(pOut);

            bcpgOut.WritePacket(PacketTag.PublicKeyEncryptedSession , bOut.ToArray(), true);
		}
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(this.tag, this.contents, true);
 }
        public override void Encode(IBcpgOutputStream bcpgOut)
        {
            using (var bOut = new MemoryStream())
            {
                using (var pOut = new BcpgOutputStream(bOut))
                {

                    pOut.WriteByte((byte)_version);

                    pOut.WriteLong(_keyId);

                    pOut.WriteByte((byte)_algorithm);

                    for (var i = 0; i != _data.Length; i++)
                    {
                        MPInteger.EncodeInteger(pOut, _data[i]);
                    }

                    if (_extraData != null)
                    {
                        if (_extraData.Length > 0xFF)
                            throw new PgpException("Extra Data is too large.");
                        pOut.WriteByte((byte)_extraData.Length);
                        pOut.Write(_extraData, 0, _extraData.Length);
                    }

                    bcpgOut.WritePacket(PacketTag.PublicKeyEncryptedSession, bOut.ToArray(), true);
                }
            }
        }
		public override void Encode(
			BcpgOutputStream bcpgOut)
        {
            bcpgOut.WritePacket(PacketTag.SecretSubkey, GetEncodedContents(), true);
        }
Esempio n. 34
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WriteObjects(d, p, q, u);
 }
Esempio n. 35
0
        /// <summary>
        /// Attempt to sign a PGP message using the specific private key.
        /// </summary>
        /// <param name="messageStream">Stream containing the message to sign.</param>
        /// <param name="signatureStream">Stream to write the signature into.</param>
        /// <param name="senderPublicKey">The BouncyCastle public key associated with the signature.</param>
        /// <param name="senderPrivateKey">The BouncyCastle private key to be used for signing.</param>
        /// <param name="hashAlgorithmTag">The hash algorithm tag to use for signing.</param>
        /// <param name="armor">Whether to wrap the message with ASCII armor.</param>
        /// <returns>Whether the signature completed successfully.</returns>
        public static bool Sign(Stream messageStream, Stream signatureStream, PgpPublicKey senderPublicKey, PgpPrivateKey senderPrivateKey, HashAlgorithmTag hashAlgorithmTag = HashAlgorithmTag.Sha256, bool armor = true)
        {
            // Create a signature generator.
            PgpSignatureGenerator signatureGenerator = new PgpSignatureGenerator(senderPublicKey.Algorithm, hashAlgorithmTag);
            signatureGenerator.InitSign(PgpSignature.BinaryDocument, senderPrivateKey);

            // Add the public key user ID.
            foreach (string userId in senderPublicKey.GetUserIds())
            {
                PgpSignatureSubpacketGenerator signatureSubGenerator = new PgpSignatureSubpacketGenerator();
                signatureSubGenerator.SetSignerUserId(false, userId);
                signatureGenerator.SetHashedSubpackets(signatureSubGenerator.Generate());
                break;
            }

            // Handle ASCII armor.
            if (armor)
            {
                using (ArmoredOutputStream armoredStream = new ArmoredOutputStream(signatureStream))
                {
                    armoredStream.BeginClearText(hashAlgorithmTag);

                    // Process each character in the message.
                    int messageChar;
                    while ((messageChar = messageStream.ReadByte()) >= 0)
                    {
                        armoredStream.WriteByte((byte)messageChar);
                        signatureGenerator.Update((byte)messageChar);
                    }

                    armoredStream.EndClearText();

                    using (BcpgOutputStream bcpgStream = new BcpgOutputStream(armoredStream))
                    {
                        signatureGenerator.Generate().Encode(bcpgStream);
                    }
                }
            }
            else
            {
                // Process each character in the message.
                int messageChar;
                while ((messageChar = messageStream.ReadByte()) >= 0)
                {
                    signatureGenerator.Update((byte)messageChar);
                }

                signatureGenerator.Generate().Encode(signatureStream);
            }

            return true;
        }
Esempio n. 36
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.Trust, levelAndTrustAmount, true);
 }
Esempio n. 37
0
		/**
		 * Return the byte encoding of the signature section.
		 * @return uninterpreted signature bytes.
		 */
		public byte[] GetSignatureBytes()
		{
			if (signatureEncoding != null)
			{
				return (byte[]) signatureEncoding.Clone();
			}

			MemoryStream bOut = new MemoryStream();
			BcpgOutputStream bcOut = new BcpgOutputStream(bOut);

			foreach (MPInteger sigObj in signature)
			{
				try
				{
					bcOut.WriteObject(sigObj);
				}
				catch (IOException e)
				{
					throw new Exception("internal error: " + e);
				}
			}

			return bOut.ToArray();
		}
Esempio n. 38
0
 internal static void Encode(BcpgOutputStream bcpgOut, BigInteger val)
 {
     bcpgOut.WriteShort((short)val.BitLength);
     bcpgOut.Write(val.ToByteArrayUnsigned());
 }
Esempio n. 39
0
		public override void Encode(
            BcpgOutputStream bcpgOut)
        {
            MemoryStream bOut = new MemoryStream();
            BcpgOutputStream pOut = new BcpgOutputStream(bOut);

			pOut.WriteByte((byte) version);

			if (version == 3 || version == 2)
            {
				pOut.Write(
					5, // the length of the next block
					(byte) signatureType);

				pOut.WriteInt((int)(creationTime / 1000L));

				pOut.WriteLong(keyId);

				pOut.Write(
					(byte) keyAlgorithm,
					(byte) hashAlgorithm);
            }
            else if (version == 4)
            {
                pOut.Write(
					(byte) signatureType,
					(byte) keyAlgorithm,
					(byte) hashAlgorithm);

				EncodeLengthAndData(pOut, GetEncodedSubpackets(hashedData));

				EncodeLengthAndData(pOut, GetEncodedSubpackets(unhashedData));
            }
            else
            {
                throw new IOException("unknown version: " + version);
            }

			pOut.Write(fingerprint);

			if (signature != null)
			{
				pOut.WriteObjects(signature);
			}
			else
			{
				pOut.Write(signatureEncoding);
			}

			bcpgOut.WritePacket(PacketTag.Signature, bOut.ToArray(), true);
        }
Esempio n. 40
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.Marker, marker, true);
 }
Esempio n. 41
0
		private static void EncodeLengthAndData(
			BcpgOutputStream	pOut,
			byte[]				data)
		{
			pOut.WriteShort((short) data.Length);
			pOut.Write(data);
		}
		public override void Encode(
			BcpgOutputStream bcpgOut)
		{
			bcpgOut.WriteObject(x);
		}
Esempio n. 43
0
		public override void Encode(
            BcpgOutputStream bcpgOut)
        {
            bcpgOut.WritePacket(PacketTag.ModificationDetectionCode, digest, false);
        }
Esempio n. 44
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.ModificationDetectionCode, digest, false);
 }
Esempio n. 45
0
		public abstract void Encode(BcpgOutputStream bcpgOut);
Esempio n. 46
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.PublicKey, GetEncodedContents(), true);
 }
Esempio n. 47
0
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     bcpgOut.WriteObject(this.x);
 }
Esempio n. 48
0
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     bcpgOut.WritePacket(PacketTag.UserId, this.idData, true);
 }
Esempio n. 49
0
		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();
        }
Esempio n. 50
0
 public override void Encode(
     BcpgOutputStream bcpgOut)
 {
     bcpgOut.WriteObjects(p, g, y);
 }
Esempio n. 51
0
        public override void Encode(
			BcpgOutputStream bcpgOut)
        {
            bcpgOut.WriteObjects(d, p, q, u);
        }