Close() public method

public Close ( ) : void
return void
Example #1
0
 protected MemoryStream DerByteStream()
 {
     // Usually 70-72 bytes.
     var bos = new MemoryStream(72);
     var seq = new DerSequenceGenerator(bos);
     seq.AddObject(new DerInteger(R));
     seq.AddObject(new DerInteger(S));
     seq.Close();
     return bos;
 }
Example #2
0
 public Byte[] signData(Byte[] data)
 {
     if (privKey == null)
         throw new InvalidOperationException();
     ECDsaSigner signer = new ECDsaSigner();
     signer.Init(true, new ECPrivateKeyParameters(new BigInteger(1, privKey), ecParams));
     BigInteger[] sig = signer.GenerateSignature(data);
     using (MemoryStream ms = new MemoryStream())
     using (Asn1OutputStream asn1stream = new Asn1OutputStream(ms))
     {
         DerSequenceGenerator seq = new DerSequenceGenerator(asn1stream);
         seq.AddObject(new DerInteger(sig[0]));
         seq.AddObject(new DerInteger(sig[1]));
         seq.Close();
         return ms.ToArray();
     }
 }
        public Stream Open(
            Stream  outStream,
            string        contentOID,
            string        compressionOID)
        {
            BerSequenceGenerator sGen = new BerSequenceGenerator(outStream);

            sGen.AddObject(CmsObjectIdentifiers.CompressedData);

            //
            // Compressed Data
            //
            BerSequenceGenerator cGen = new BerSequenceGenerator(sGen.GetRawOutputStream(), 0, true);

            cGen.AddObject(new DerInteger(0));

            //
            // AlgorithmIdentifier
            //
            DerSequenceGenerator algGen = new DerSequenceGenerator(cGen.GetRawOutputStream());

            algGen.AddObject(new DerObjectIdentifier(ZLib));

            algGen.Close();

            //
            // Encapsulated ContentInfo
            //
            BerSequenceGenerator eiGen = new BerSequenceGenerator(cGen.GetRawOutputStream());

            eiGen.AddObject(new DerObjectIdentifier(contentOID));

            BerOctetStringGenerator octGen = new BerOctetStringGenerator(eiGen.GetRawOutputStream(), 0, true);

            return new CmsCompressedOutputStream(
                new ZDeflaterOutputStream(octGen.GetOctetOutputStream()), sGen, cGen, eiGen);
        }
        private static byte[] SerializeAsn1(InheritedEntity e)
        {
            using (var ms = new MemoryStream())
            {
                var s = new DerSequenceGenerator(ms, 1, false);
                s.AddObject(new DerPrintableString(e.Message));
                s.AddObject(new DerPrintableString(e.FunctionCall));
                s.AddObject(new DerPrintableString(e.Parameters));
                s.AddObject(new DerPrintableString(e.Name));
                s.AddObject(new DerInteger(e.EmployeeId));

                var floatBytes = BitConverter.GetBytes(e.RaiseRate);
                    // unable to find API to write floats, hacking away :/
                s.AddObject(new DerOctetString(floatBytes));

                s.AddObject(new DerPrintableString(e.AddressLine1));
                s.AddObject(new DerPrintableString(e.AddressLine2));
                s.AddObject(new DerOctetString(e.Icon));
                s.AddObject(new DerOctetString(e.LargeIcon));
                s.Close();

                return ms.ToArray();
            }
        }
Example #5
0
		public static string exportPrivToPEM(AsymmetricCipherKeyPair pair) {
			byte[] asn1key;
			var privkey = ((ECPrivateKeyParameters)pair.Private).D;
			var pubkey = ((ECPublicKeyParameters)pair.Public).Q.GetEncoded ();

			/* Export in the same format as the one by OpenSSL */
			using (var mem = new MemoryStream ()) {
				using (var encoder = new Asn1OutputStream (mem)) {
					var seq = new DerSequenceGenerator (encoder);
					seq.AddObject (new DerInteger (1));
					seq.AddObject (new DerOctetString (privkey.ToByteArray ()));
					seq.AddObject (new DerTaggedObject (0, new DerObjectIdentifier (OID_secp256k1)));
					seq.AddObject (new DerTaggedObject (1, new DerBitString (pubkey)));
					seq.Close ();
					asn1key = mem.ToArray ();
				}
			}

			return bytesToPEM (asn1key, "EC PRIVATE");
		}
Example #6
0
		public static string exportPubToPEM(AsymmetricKeyParameter key) {
			byte[] asn1key;
			var pubkey = ((ECPublicKeyParameters) key).Q.GetEncoded ();

			/* Export in the same format as the one by OpenSSL */
			using (var mem = new MemoryStream ()) {
				using (var encoder = new Asn1OutputStream (mem)) {
					var seq = new DerSequenceGenerator (mem);

					var vec = new Asn1EncodableVector ();
					vec.Add (new DerObjectIdentifier (OID_ecPublicKey));
					vec.Add (new DerObjectIdentifier (OID_secp256k1));

					seq.AddObject (new DerSequence(vec));
					seq.AddObject (new DerBitString (pubkey));
					seq.Close ();

					asn1key = mem.ToArray ();
				}
			}

			return bytesToPEM (asn1key, "PUBLIC");
		}
Example #7
0
 /// <summary>
 /// Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by the BitCoin reference
 /// implementation in its wallet storage format.
 /// </summary>
 public byte[] ToAsn1()
 {
     using (var baos = new MemoryStream(400))
     {
         using (var encoder = new Asn1OutputStream(baos))
         {
             // ASN1_SEQUENCE(EC_PRIVATEKEY) = {
             //   ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
             //   ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
             //   ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
             //   ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
             // } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
             var seq = new DerSequenceGenerator(encoder);
             seq.AddObject(new DerInteger(1)); // version
             seq.AddObject(new DerOctetString(_priv.ToByteArray()));
             seq.AddObject(new DerTaggedObject(0, SecNamedCurves.GetByName("secp256k1").ToAsn1Object()));
             seq.AddObject(new DerTaggedObject(1, new DerBitString(PubKey)));
             seq.Close();
         }
         return baos.ToArray();
     }
 }
Example #8
0
 /// <summary>
 /// Calculates an ECDSA signature in DER format for the given input hash. Note that the input is expected to be
 /// 32 bytes long.
 /// </summary>
 public byte[] Sign(byte[] input)
 {
     var signer = new ECDsaSigner();
     var privKey = new ECPrivateKeyParameters(_priv, _ecParams);
     signer.Init(true, privKey);
     var sigs = signer.GenerateSignature(input);
     // What we get back from the signer are the two components of a signature, r and s. To get a flat byte stream
     // of the type used by BitCoin we have to encode them using DER encoding, which is just a way to pack the two
     // components into a structure.
     using (var bos = new MemoryStream())
     {
         var seq = new DerSequenceGenerator(bos);
         seq.AddObject(new DerInteger(sigs[0]));
         seq.AddObject(new DerInteger(sigs[1]));
         seq.Close();
         return bos.ToArray();
     }
 }
Example #9
0
        public byte[] ToDER(bool compressed)
        {
            AssertPrivateKey();
            MemoryStream baos = new MemoryStream();

            // ASN1_SEQUENCE(EC_PRIVATEKEY) = {
            //   ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
            //   ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
            //   ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
            //   ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
            // } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
            DerSequenceGenerator seq = new DerSequenceGenerator(baos);
            seq.AddObject(new DerInteger(1)); // version
            seq.AddObject(new DerOctetString(PrivateKey.D.ToByteArrayUnsigned()));

            //Did not managed to generate the same der as brainwallet by using this
            //seq.AddObject(new DerTaggedObject(0, Secp256k1.ToAsn1Object()));
            Asn1Object secp256k1Der = null;
            if(compressed)
            {
                secp256k1Der = DerSequence.FromByteArray(DataEncoders.Encoders.Hex.DecodeData("308182020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101"));
            }
            else
            {
                secp256k1Der = DerSequence.FromByteArray(DataEncoders.Encoders.Hex.DecodeData("3081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101"));
            }
            seq.AddObject(new DerTaggedObject(0, secp256k1Der));
            seq.AddObject(new DerTaggedObject(1, new DerBitString(GetPubKey(compressed).ToBytes())));
            seq.Close();
            return baos.ToArray();
        }
Example #10
0
 /**
 * What we get back from the signer are the two components of a signature, r and s. To get a flat byte stream
 * of the type used by Bitcoin we have to encode them using DER encoding, which is just a way to pack the two
 * components into a structure.
 */
 public byte[] ToDER()
 {
     // Usually 70-72 bytes.
     MemoryStream bos = new MemoryStream(72);
     DerSequenceGenerator seq = new DerSequenceGenerator(bos);
     seq.AddObject(new DerInteger(R));
     seq.AddObject(new DerInteger(S));
     seq.Close();
     return bos.ToArray();
 }