Пример #1
0
    public byte[] GetPrivateKeyFromNEP2(string nep2, string passphrase)
    {
//		if (nep2 == null)
//			throw new ArgumentNullException (nameof (nep2));
//		if (passphrase == null)
//			throw new ArgumentNullException (nameof (passphrase));

        byte[] data = nep2.Base58CheckDecode();

        if (data.Length != 39 || data [0] != 0x01 || data [1] != 0x42 || data [2] != 0xe0)
        {
            throw new FormatException();
        }

        Debug.Log("nep2 " + nep2);

        byte[] addresshash = new byte[4];

        Buffer.BlockCopy(data, 3, addresshash, 0, 4);

        Debug.Log("in nep2: " + passphrase);

        byte[] derivedkey   = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, 16384, 8, 8, 64);
        byte[] derivedhalf1 = derivedkey.Take(32).ToArray();
        byte[] derivedhalf2 = derivedkey.Skip(32).ToArray();
        byte[] encryptedkey = new byte[32];
        Buffer.BlockCopy(data, 7, encryptedkey, 0, 32);
        byte[] prikey = XOR(encryptedkey.AES256Decrypt(derivedhalf2), derivedhalf1);
        Neo.Cryptography.ECC.ECPoint pubkey = Neo.Cryptography.ECC.ECCurve.Secp256r1.G * prikey;

        var bytes = pubkey.EncodePoint(true).ToArray();
//		byte[] CompressedPublicKey = bytes;

        // byte[] PublicKeyHash = Crypto.Default.ToScriptHash (bytes);

        string  signatureScript = KeyPair.CreateSignatureScript(bytes);
        UInt160 signatureHash   = Crypto.Default.ToScriptHash(signatureScript.HexToBytes());

        byte[] publickey = pubkey.EncodePoint(false).Skip(1).ToArray();

        string address = Crypto.Default.ToAddress(signatureHash);

        Debug.Log("decrypted private key: " + prikey.ByteToHex());
        Debug.Log("decrypted public key: " + publickey.ByteToHex());
        Debug.Log("decrypted address: " + address);

        return(prikey);
    }
Пример #2
0
        public void TestEncodePoint()
        {
            ECPoint point = new ECPoint(null, null, ECCurve.Secp256k1);

            byte[] result1 = { 0 };
            point.EncodePoint(true).Should().BeEquivalentTo(result1);

            point = ECCurve.Secp256k1.G;
            byte[] result2 = { 4,  121, 190, 102, 126, 249, 220, 187, 172,  85, 160, 98, 149, 206, 135,  11,  7,   2, 155, 252, 219, 45, 206,  40, 217,  89, 242, 129, 91,  22, 248, 23, 152, 72,
                               58, 218, 119,  38, 163, 196, 101,  93, 164, 251, 252, 14,  17,   8, 168, 253, 23, 180,  72, 166, 133, 84,  25, 156,  71, 208, 143, 251, 16, 212, 184 };
            point.EncodePoint(false).Should().BeEquivalentTo(result2);

            byte[] result3 = { 2, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152 };
            point.EncodePoint(true).Should().BeEquivalentTo(result3);

            point = ECCurve.Secp256r1.G;
            byte[] result4 = { 3, 107, 23, 209, 242, 225, 44, 66, 71, 248, 188, 230, 229, 99, 164, 64, 242, 119, 3, 125, 129, 45, 235, 51, 160, 244, 161, 57, 69, 216, 152, 194, 150 };
            point.EncodePoint(true).Should().BeEquivalentTo(result4);
        }
Пример #3
0
        public void TestEncodePoint()
        {
            ECPoint point = new ECPoint(null, null, ECCurve.Secp256k1);

            byte[] result1 = { 0 };
            point.EncodePoint(true).Should().BeEquivalentTo(result1);

            point = ECCurve.Secp256k1.G;
            byte[] result2 = { 4,  121, 190, 102, 126, 249, 220, 187, 172,  85, 160, 98, 149, 206, 135,  11,  7,   2, 155, 252, 219, 45, 206,  40, 217,  89, 242, 129, 91,  22, 248, 23, 152, 72,
                               58, 218, 119,  38, 163, 196, 101,  93, 164, 251, 252, 14,  17,   8, 168, 253, 23, 180,  72, 166, 133, 84,  25, 156,  71, 208, 143, 251, 16, 212, 184 };
            point.EncodePoint(false).Should().BeEquivalentTo(result2);
            point.EncodePoint(false).Should().BeEquivalentTo(result2);

            byte[] result3 = { 2, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152 };
            point.EncodePoint(true).Should().BeEquivalentTo(result3);
            point.EncodePoint(true).Should().BeEquivalentTo(result3);

            point = ECCurve.Secp256r1.G;
            byte[] result4 = { 3, 107, 23, 209, 242, 225, 44, 66, 71, 248, 188, 230, 229, 99, 164, 64, 242, 119, 3, 125, 129, 45, 235, 51, 160, 244, 161, 57, 69, 216, 152, 194, 150 };
            point.EncodePoint(true).Should().BeEquivalentTo(result4);
            point.EncodePoint(true).Should().BeEquivalentTo(result4);

            // Test cache

            point = ECPoint.DecodePoint(ECCurve.Secp256r1.G.EncodePoint(true), ECCurve.Secp256r1);
            point.EncodePoint(true).Should().BeEquivalentTo(result4);
            point.EncodePoint(true).Should().BeEquivalentTo(result4);

            byte[] result5 = "046b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c2964fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5".HexToBytes();
            point = ECPoint.DecodePoint(ECCurve.Secp256r1.G.EncodePoint(false), ECCurve.Secp256r1);
            point.EncodePoint(true).Should().BeEquivalentTo(result4);
            point.EncodePoint(true).Should().BeEquivalentTo(result4);
            point.EncodePoint(false).Should().BeEquivalentTo(result5);
            point.EncodePoint(false).Should().BeEquivalentTo(result5);
        }