예제 #1
0
        public ECPrivateKeyStructure(
            BigInteger key)
        {
            if (key == null)
                throw new ArgumentNullException("key");

            this.seq = new DerSequence(
                new DerInteger(1),
                new DerOctetString(key.ToByteArrayUnsigned()));
        }
예제 #2
0
		/**
         * Return the passed in value as an unsigned byte array of specified length, zero-extended as necessary.
         *
         * @param length desired length of result array.
         * @param n value to be converted.
         * @return a byte array of specified length, with leading zeroes as necessary given the size of n.
         */
		public static byte[] AsUnsignedByteArray(int length, BigInteger n)
		{
			byte[] bytes = n.ToByteArrayUnsigned();

			if(bytes.Length > length)
				throw new ArgumentException("standard length exceeded", "n");

			if(bytes.Length == length)
				return bytes;

			byte[] tmp = new byte[length];
			Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length);
			return tmp;
		}
예제 #3
0
        public static byte[] IntegerToBytes(BigInteger s, int qLength)
        {
            byte[] bytes = s.ToByteArrayUnsigned();

            if (qLength < bytes.Length)
            {
                byte[] tmp = new byte[qLength];
                Array.Copy(bytes, bytes.Length - tmp.Length, tmp, 0, tmp.Length);
                return tmp;
            }
            else if (qLength > bytes.Length)
            {
                byte[] tmp = new byte[qLength];
                Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length);
                return tmp;
            }

            return bytes;
        }
예제 #4
0
        public ECPrivateKeyStructure(
            BigInteger		key,
            DerBitString	publicKey,
            Asn1Encodable	parameters)
        {
            if (key == null)
                throw new ArgumentNullException("key");

            Asn1EncodableVector v = new Asn1EncodableVector(
                new DerInteger(1),
                new DerOctetString(key.ToByteArrayUnsigned()));

            if (parameters != null)
            {
                v.Add(new DerTaggedObject(true, 0, parameters));
            }

            if (publicKey != null)
            {
                v.Add(new DerTaggedObject(true, 1, publicKey));
            }

            this.seq = new DerSequence(v);
        }
예제 #5
0
		public override Key GetKey(string password)
		{
			var encrypted = PartialEncrypted.ToArray();
			//Derive passfactor using scrypt with ownerentropy and the user's passphrase and use it to recompute passpoint
			byte[] passfactor = CalculatePassFactor(password, LotSequence, OwnerEntropy);
			var passpoint = CalculatePassPoint(passfactor);

			var derived = SCrypt.BitcoinComputeDerivedKey2(passpoint, this.AddressHash.Concat(this.OwnerEntropy).ToArray());

			//Decrypt encryptedpart1 to yield the remainder of seedb.
			var seedb = DecryptSeed(encrypted, derived);
			var factorb = Hashes.Hash256(seedb).ToBytes();

			var curve = ECKey.CreateCurve();

			//Multiply passfactor by factorb mod N to yield the private key associated with generatedaddress.
			var keyNum = new BigInteger(1, passfactor).Multiply(new BigInteger(1, factorb)).Mod(curve.N);
			var keyBytes = keyNum.ToByteArrayUnsigned();
			if(keyBytes.Length < 32)
				keyBytes = new byte[32 - keyBytes.Length].Concat(keyBytes).ToArray();

			var key = new Key(keyBytes, fCompressedIn: IsCompressed);

			var generatedaddress = key.PubKey.GetAddress(Network);
			var addresshash = HashAddress(generatedaddress);

			if(!Utils.ArrayEqual(addresshash, AddressHash))
				throw new SecurityException("Invalid password (or invalid Network)");

			return key;
		}
예제 #6
0
		public byte[] ConvertOutput(
			BigInteger result)
		{
			byte[] output = result.ToByteArrayUnsigned();

			if (forEncryption)
			{
				int outSize = GetOutputBlockSize();

				// TODO To avoid this, create version of BigInteger.ToByteArray that
				// writes to an existing array
				if (output.Length < outSize) // have ended up with less bytes than normal, lengthen
				{
					byte[] tmp = new byte[outSize];
					output.CopyTo(tmp, tmp.Length - output.Length);
					output = tmp;
				}
			}

			return output;
		}
예제 #7
0
		/**
        * Return the passed in value as an unsigned byte array.
        *
        * @param value value to be converted.
        * @return a byte array without a leading zero byte if present in the signed encoding.
        */
		public static byte[] AsUnsignedByteArray(
			BigInteger n)
		{
			return n.ToByteArrayUnsigned();
		}
예제 #8
0
        // TODO Move functionality to more general class
		private static byte[] ConvertRSAParametersField(BigInteger n, int size)
		{
			byte[] bs = n.ToByteArrayUnsigned();

			if (bs.Length == size)
				return bs;

			if (bs.Length > size)
				throw new ArgumentException("Specified size too small", "size");

			byte[] padded = new byte[size];
			Array.Copy(bs, 0, padded, size - bs.Length, bs.Length);
			return padded;
		}