GetEncoded() public abstract method

public abstract GetEncoded ( ) : byte[]
return byte[]
コード例 #1
0
 public PublicKey(ECPoint point)
 {
     this.IsCompressedPoint = point.IsCompressed;
     this.point = point;
     this.PublicKeyBytes = point.GetEncoded();
     if (validatePoint() == false) throw new ArgumentException("Not a valid public key");
 }
コード例 #2
0
 protected ECPublicBcpgKey(
     DerObjectIdentifier oid,
     ECPoint point)
 {
     this.point = new BigInteger(1, point.GetEncoded());
     this.oid = oid;
 }
コード例 #3
0
ファイル: Keys.cs プロジェクト: rgx91/BWTool
        /// <summary>
        /// Computes the public key from the private key.
        /// </summary>
        protected override byte[] ComputePublicKey()
        {
            var ps = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");

            Org.BouncyCastle.Math.EC.ECPoint point = ps.G;

            Org.BouncyCastle.Math.BigInteger Db = new Org.BouncyCastle.Math.BigInteger(1, _privKey);
            Org.BouncyCastle.Math.EC.ECPoint dd = point.Multiply(Db);


            if (IsCompressedPoint)
            {
                dd = ps.Curve.CreatePoint(dd.X.ToBigInteger(), dd.Y.ToBigInteger(), true);
                return(dd.GetEncoded());
            }
            else
            {
                byte[] pubaddr = new byte[65];
                byte[] Y       = dd.Y.ToBigInteger().ToByteArray();
                Array.Copy(Y, 0, pubaddr, 64 - Y.Length + 1, Y.Length);
                byte[] X = dd.X.ToBigInteger().ToByteArray();
                Array.Copy(X, 0, pubaddr, 32 - X.Length + 1, X.Length);
                pubaddr[0] = 4;
                return(pubaddr);
            }
        }
コード例 #4
0
        /**
         * @param privateKey hex string without 0x
         * @return
         */
        public static string GetPublicKeyFromPrivateKey(string privateKey, bool compressed)
        {
            BigInteger bigInteger = new BigInteger(privateKey, 16);
            ECPoint    point      = GetPublicPointFromPrivate(bigInteger);

            return(ByteUtil.ByteArrayToHexString(point.GetEncoded(compressed)));
        }
コード例 #5
0
        public static byte GetRecoveryId(byte[] sigR, byte[] sigS, byte[] message, byte[] publicKey)
        {
            //ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256k1");
            X9ECParameters spec = ECNamedCurveTable.GetByName("secp256k1");

            BigInteger pointN = spec.N;

            for (int recoveryId = 0; recoveryId < 2; recoveryId++)
            {
                try
                {
                    BigInteger pointX = new BigInteger(1, sigR);

                    byte[] compEnc =
                        X9IntegerConverter.IntegerToBytes(pointX, 1 + X9IntegerConverter.GetByteLength(spec.Curve));
                    compEnc[0] = (byte)((recoveryId & 1) == 1 ? 0x03 : 0x02);
                    ECPoint pointR = spec.Curve.DecodePoint(compEnc);
                    if (!pointR.Multiply(pointN).IsInfinity)
                    {
                        continue;
                    }

                    BigInteger pointE        = new BigInteger(1, message);
                    BigInteger pointEInv     = BigInteger.Zero.Subtract(pointE).Mod(pointN);
                    BigInteger pointRInv     = new BigInteger(1, sigR).ModInverse(pointN);
                    BigInteger srInv         = pointRInv.Multiply(new BigInteger(1, sigS)).Mod(pointN);
                    BigInteger pointEInvRInv = pointRInv.Multiply(pointEInv).Mod(pointN);
                    ECPoint    pointQ        = ECAlgorithms.SumOfTwoMultiplies(spec.G, pointEInvRInv, pointR, srInv);
                    byte[]     pointQBytes   = pointQ.GetEncoded(false);
                    bool       matchedKeys   = true;
                    for (int j = 0; j < publicKey.Length; j++)
                    {
                        if (pointQBytes[j] != publicKey[j])
                        {
                            matchedKeys = false;
                            break;
                        }
                    }

                    if (!matchedKeys)
                    {
                        continue;
                    }

                    return((byte)(0xFF & recoveryId));
                }
                catch (Exception e)
                {
                    continue;
                    Console.WriteLine(" Failed: GET recoveryID");
                }
            }

            return((byte)0xFF);
        }
コード例 #6
0
        public static ECPoint CleanPoint(ECCurve c, ECPoint p)
        {
            ECCurve cp = p.Curve;

            if (!c.Equals(cp))
            {
                throw new ArgumentException("Point must be on the same curve", "p");
            }

            return(c.DecodePoint(p.GetEncoded(false)));
        }
コード例 #7
0
        //public static bool VerifySignature(AsymmetricKeyParameter pubKey, string signature, string msg)
        //{
        //    try
        //    {
        //        byte[] msgBytes = Encoding.UTF8.GetBytes(msg);
        //        byte[] sigBytes = Convert.FromBase64String(signature);

        //        ISigner signer = SignerUtilities.GetSigner("SHA384withECDSA");
        //        signer.Init(false, pubKey);
        //        signer.BlockUpdate(msgBytes, 0, msgBytes.Length);

        //        return signer.VerifySignature(sigBytes);
        //    }
        //    catch (Exception exc)
        //    {
        //        Console.WriteLine("Verification failed with the error: " + exc.ToString());
        //        return false;
        //    }
        //}


        /// <summary>
        /// NOT WORKING cause of SHA 256
        /// </summary>
        /// <param name="pubK"></param>
        /// <returns></returns>
        //public static AsymmetricKeyParameter GetPublicKeyObjectRepresentation(string pubK)
        //{
        //    //byte[] recoveredPublic = Convert.FromBase64String(pubK);
        //    byte[] recoveredPublic = Base58Encoding.Decode(pubK);
        //    var publicKey = PublicKeyFactory.CreateKey(recoveredPublic);

        //    return publicKey;
        //}
        //public static string GetPublicKeyStringRepresentation(AsymmetricKeyParameter pubK)
        //{
        //    SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pubK);
        //    byte[] serializedPublicBytes = subjectPublicKeyInfo.ToAsn1Object().GetDerEncoded();

        //    var base64 = Convert.ToBase64String(serializedPublicBytes);
        //    var sha256 = ComputeSha256HashB(base64);
        //    var result = Base58Encoding.Encode(sha256);

        //    return result;
        //}


        /// <summary>
        /// Not gonna be used
        /// </summary>
        /// <param name="priK"></param>
        /// <returns></returns>
        //public static string GetPrivateKeyStringRepresentation(AsymmetricKeyParameter priK)
        //{
        //    PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(priK);
        //    byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetDerEncoded();

        //    //var result = Convert.ToBase64String(serializedPrivateBytes);
        //    var result = Base58Encoding.Encode(serializedPrivateBytes);

        //    return result;
        //}

        //private static string ComputeSha256Hash(string rawData)
        //{
        //    // Create a SHA256
        //    using (SHA256 sha256Hash = SHA256.Create())
        //    {
        //        // ComputeHash - returns byte array
        //        byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));

        //        // Convert byte array to a string
        //        StringBuilder builder = new StringBuilder();
        //        for (int i = 0; i < bytes.Length; i++)
        //        {
        //            builder.Append(bytes[i].ToString("x2"));
        //        }

        //        return builder.ToString();
        //    }
        //}


        /**
         * Converts a private key into its corresponding public key.
         */
        public static byte[] GetPublicKey(byte[] privateKey)
        {
            try
            {
                //ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256k1");
                X9ECParameters spec   = ECNamedCurveTable.GetByName("secp256k1");
                ECPoint        pointQ = spec.G.Multiply(new BigInteger(1, privateKey));

                return(pointQ.GetEncoded(false));
            }
            catch (Exception e)
            {
                Console.WriteLine(" Failed: GET public key");

                return(new byte[0]);
            }
        }
コード例 #8
0
        /**
         * Recover the public key that corresponds to the private key, which signed this message.
         */
        public static byte[] RecoverPublicKey(byte[] sigR, byte[] sigS, byte[] sigV, byte[] message)
        {
            //ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256k1");
            X9ECParameters spec = ECNamedCurveTable.GetByName("secp256k1");

            BigInteger pointN = spec.N;

            try
            {
                BigInteger pointX = new BigInteger(1, sigR);


                byte[] compEnc =
                    X9IntegerConverter.IntegerToBytes(pointX, 1 + X9IntegerConverter.GetByteLength(spec.Curve));
                compEnc[0] = (byte)((sigV[0] & 1) == 1 ? 0x03 : 0x02);
                ECPoint pointR = spec.Curve.DecodePoint(compEnc);
                if (!pointR.Multiply(pointN).IsInfinity)
                {
                    return(new byte[0]);
                }

                BigInteger pointE        = new BigInteger(1, message);
                BigInteger pointEInv     = BigInteger.Zero.Subtract(pointE).Mod(pointN);
                BigInteger pointRInv     = new BigInteger(1, sigR).ModInverse(pointN);
                BigInteger srInv         = pointRInv.Multiply(new BigInteger(1, sigS)).Mod(pointN);
                BigInteger pointEInvRInv = pointRInv.Multiply(pointEInv).Mod(pointN);
                ECPoint    pointQ        = ECAlgorithms.SumOfTwoMultiplies(spec.G, pointEInvRInv, pointR, srInv);
                byte[]     pointQBytes   = pointQ.GetEncoded(false);
                return(pointQBytes);
            }
            catch (Exception e)
            {
            }

            return(new byte[0]);
        }
コード例 #9
0
ファイル: X9ECPoint.cs プロジェクト: KimikoMuffin/bc-csharp
 public X9ECPoint(ECPoint p, bool compressed)
 {
     this.p = p.Normalize();
     this.encoding = new DerOctetString(p.GetEncoded(compressed));
 }
コード例 #10
0
ファイル: ECPointTest.cs プロジェクト: jesusgarza/bc-csharp
        /**
         * Test encoding with and without point compression.
         *
         * @param p
         *            The point to be encoded and decoded.
         */
        private void ImplTestEncoding(ECPoint p)
        {
            // Not Point Compression
            byte[] unCompBarr = p.GetEncoded(false);
            ECPoint decUnComp = p.Curve.DecodePoint(unCompBarr);
            AssertPointsEqual("Error decoding uncompressed point", p, decUnComp);

            // Point compression
            byte[] compBarr = p.GetEncoded(true);
            ECPoint decComp = p.Curve.DecodePoint(compBarr);
            AssertPointsEqual("Error decoding compressed point", p, decComp);
        }
コード例 #11
0
		static byte[] ExternalizeKey (ECPoint q)
		{
			// TODO Add support for compressed encoding and SPF extension

			/*
			 * RFC 4492 5.7. ...an elliptic curve point in uncompressed or compressed format.
			 * Here, the format MUST conform to what the server has requested through a
			 * Supported Point Formats Extension if this extension was used, and MUST be
			 * uncompressed if this extension was not used.
			 */
			return q.GetEncoded ();
		}
コード例 #12
0
 public K256VerifyingKey(ECPoint pub)
 {
     PubKey = pub;
     PubKeyBytes = pub.GetEncoded(true);
     SetVerifier(pub);
 }
コード例 #13
0
        public static byte[] SerializeECPoint(byte[] ecPointFormats, ECPoint point)
        {
            ECCurve curve = point.Curve;

            /*
             * RFC 4492 5.7. ...an elliptic curve point in uncompressed or compressed format. Here, the
             * format MUST conform to what the server has requested through a Supported Point Formats
             * Extension if this extension was used, and MUST be uncompressed if this extension was not
             * used.
             */
            bool compressed = false;
            if (ECAlgorithms.IsFpCurve(curve))
            {
                compressed = IsCompressionPreferred(ecPointFormats, ECPointFormat.ansiX962_compressed_prime);
            }
            else if (ECAlgorithms.IsF2mCurve(curve))
            {
                compressed = IsCompressionPreferred(ecPointFormats, ECPointFormat.ansiX962_compressed_char2);
            }
            return point.GetEncoded(compressed);
        }