コード例 #1
0
        public static string ToCheckSumAddress(string address)
        {
            if (!Validation.IsAddress(address))
            {
                throw new Exception("not a valid base 16 address");
            }

            address = address.ToLower().Replace("0x", "");
            string        hash = ByteUtil.ByteArrayToHexString(HashUtil.Sha256(ByteUtil.HexStringToByteArray(address)));
            StringBuilder ret  = new StringBuilder("0x");

            byte[]     x = ByteUtil.HexStringToByteArray(hash);
            BigInteger v = new BigInteger(ByteUtil.HexStringToByteArray(hash));

            for (int i = 0; i < address.Length; i++)
            {
                if ("1234567890".IndexOf(address.ToCharArray()[i]) != -1)
                {
                    ret.Append(address.ToCharArray()[i]);
                }
                else
                {
                    BigInteger checker = v.And(BigInteger.ValueOf(2).Pow(255 - 6 * i));//(BigInteger.valueOf(2l).pow(255 - 6 * i))
                    ret.Append(checker.CompareTo(BigInteger.ValueOf(1)) < 0 ? address.ToCharArray()[i].ToString().ToLower() : address.ToCharArray()[i].ToString().ToUpper());
                }
            }
            return(ret.ToString());
        }
コード例 #2
0
        public static string GetAddressForContract(Transaction.Transaction tx)
        {
            string senderAddress = KeyTools.GetAddressFromPublicKey(tx.SenderPubKey);

            SHA256Managed.Create().ComputeHash(ByteUtil.HexStringToByteArray(senderAddress));
            Sha256Digest sha = new Sha256Digest();

            byte[] senderAddressBytes = ByteUtil.HexStringToByteArray(senderAddress);
            sha.BlockUpdate(senderAddressBytes, 0, senderAddressBytes.Count());

            int nonce = 0;

            if (!string.IsNullOrEmpty(tx.Nonce))
            {
                nonce = int.Parse(tx.Nonce);
                nonce--;
            }
            string hexNonce = Validation.IntToHex(nonce, 16);

            byte[] hexNonceBytes = ByteUtil.HexStringToByteArray(hexNonce);
            sha.BlockUpdate(hexNonceBytes, 0, hexNonceBytes.Count());
            byte[] bytes = new byte[sha.GetByteLength()];
            sha.DoFinal(bytes, 0);

            return(ByteUtil.ByteArrayToHexString(bytes).Substring(24, 40));
        }
コード例 #3
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)));
        }
コード例 #4
0
 private void ValidateMac(byte[] mac, byte[] cipherText, byte[] derivedKey)
 {
     var generatedMac = GenerateMac(derivedKey, cipherText);
     if (ByteUtil.ByteArrayToHexString(generatedMac) != ByteUtil.ByteArrayToHexString(mac))
         throw new Exception(
             "Cannot derive the same mac as the one provided from the cipher and derived key");
 }
コード例 #5
0
        public static string GetAddressFromPublicKey(string publicKey)
        {
            byte[] data          = GetAddressFromPublicKey(ByteUtil.HexStringToByteArray(publicKey));
            string originAddress = ByteUtil.ByteArrayToHexString(data);

            return(originAddress.Substring(24));
        }
コード例 #6
0
ファイル: Signature.cs プロジェクト: iantanwx/LaksaCsharp
        public override string ToString()
        {
            string rHex = ByteUtil.ByteArrayToHexString(R.ToByteArray());

            while (rHex.Length < 64)
            {
                rHex = "0" + rHex;
            }

            while (rHex.Length > 64 && rHex.StartsWith("0"))
            {
                rHex = rHex.Substring(1);
            }

            string sHex = ByteUtil.ByteArrayToHexString(S.ToByteArray());

            while (sHex.Length < 64)
            {
                sHex = "0" + sHex;
            }

            while (sHex.Length > 64 && sHex.StartsWith("0"))
            {
                sHex = sHex.Substring(1);
            }

            return(rHex + sHex);
        }
コード例 #7
0
ファイル: KeyStore.cs プロジェクト: yanbin007/LaksaCsharp
        public string DecryptPrivateKey(string encryptJson, string passphrase)
        {
            KeystoreV3 keystoreV3 = JsonConvert.DeserializeObject <KeystoreV3>(encryptJson);

            byte[]    ciphertext = ByteUtil.HexStringToByteArray(keystoreV3.Crypto.Ciphertext);
            byte[]    iv         = ByteUtil.HexStringToByteArray(keystoreV3.Crypto.Cipherparams.Iv);
            Kdfparams kp         = keystoreV3.Crypto.Kdfparams;
            string    kdf        = keystoreV3.Crypto.Kdf;

            byte[] derivedKey;
            if (kdf == "pbkdf2")
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();
                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(kp.Salt);
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                ScryptParams scryptParams = new ScryptParams();
                scryptParams.Salt  = ByteUtil.ByteArrayToHexString(kp.Salt);
                scryptParams.DkLen = 32;
                scryptParams.P     = 1;
                scryptParams.R     = 8;
                scryptParams.N     = 8192;

                derivedKey = GetDerivedKey(Encoding.Default.GetBytes(passphrase), scryptParams);
            }
            string mac = ByteUtil.ByteArrayToHexString(HashUtil.GenerateMac(derivedKey, ciphertext));

            if (mac.ToUpper() != keystoreV3.Crypto.Mac)
            {
                throw new Exception("Failed to decrypt.");
            }

            byte[] encryptKey = new byte[16];
            Array.Copy(derivedKey, encryptKey, 16);

            //TODO 加密方法待完善
            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key     = encryptKey,
                Mode    = CipherMode.CBC,
                Padding = System.Security.Cryptography.PaddingMode.None
            };
            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
            byte[] ciphertextByte = cTransform.TransformFinalBlock(ciphertext, 0, ciphertext.Length);

            return(ByteUtil.ByteArrayToHexString(ciphertextByte));
        }
コード例 #8
0
        public void GenerateDerivedScryptKey()
        {
            PBKDF2Wrapper pbkdf2Wrapper = new PBKDF2Wrapper();
            PBKDF2Params  param         = new PBKDF2Params();

            param.Salt  = "0f2274f6c0daf36d5822d97985be5a3d881d11e2e741bad4e038a099eecc3b6d";
            param.Count = 262144;
            param.DkLen = 32;
            byte[] bytes = pbkdf2Wrapper.GetDerivedKey(System.Text.Encoding.Default.GetBytes("stronk_password"),
                                                       ByteUtil.HexStringToByteArray(param.Salt), param.Count, param.DkLen);
            byte[] macArray = HashUtil.GenerateMac(bytes, ByteUtil.HexStringToByteArray("dc55047d51f795509ffb6969db837a4481887ccfb6bfb7c259fb77b19078c2a4"));
            Console.WriteLine(ByteUtil.ByteArrayToHexString(macArray));
            Assert.AreEqual(ByteUtil.ByteArrayToHexString(macArray).ToLower(), "dedc361c53c421974c2811f7f989bc530aebf9a90c487b4161e0e54ae6faba31");
        }
コード例 #9
0
        public void GenerateDerivedScryptKey()
        {
            ScryptWrapper scryptWrapper = new ScryptWrapper();
            ScryptParams  param         = new ScryptParams();

            param.DkLen = 32;
            param.Salt  = "2c37db13a633c5a5e5b8c699109690e33860b7eb43bbc81bbab47d4e9c29f1b9";
            param.N     = 8192;
            param.R     = 8;
            param.P     = 1;

            byte[] bytes    = scryptWrapper.GetDerivedKey(System.Text.Encoding.Default.GetBytes("stronk_password"), ByteUtil.HexStringToByteArray(param.Salt), param.N, param.R, param.P, param.DkLen);
            byte[] macArray = HashUtil.GenerateMac(bytes, ByteUtil.HexStringToByteArray("ecdf81453d031ac2fa068b7185ddac044fa4632d3b061400d3c07a86510b4823"));
            Console.WriteLine(ByteUtil.ByteArrayToHexString(macArray));
            Assert.AreEqual("ed7fa37a4adbc8b7bbe0d43a329a047f89e2dcf7f2dfc96babfe79edd955f7a3", ByteUtil.ByteArrayToHexString(macArray).ToLower());
        }
コード例 #10
0
        public string DecryptPrivateKey(string encryptJson, string passphrase)
        {
            KeystoreV3 keystoreV3 = JsonConvert.DeserializeObject <KeystoreV3>(encryptJson);

            byte[]    ciphertext = ByteUtil.HexStringToByteArray(keystoreV3.Crypto.Ciphertext);
            byte[]    iv         = ByteUtil.HexStringToByteArray(keystoreV3.Crypto.Cipherparams.Iv);
            Kdfparams kp         = keystoreV3.Crypto.Kdfparams;
            string    kdf        = keystoreV3.Crypto.Kdf;

            byte[] derivedKey;
            if (kdf == "pbkdf2")
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();
                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(kp.Salt);
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                ScryptParams scryptParams = new ScryptParams();
                scryptParams.Salt  = ByteUtil.ByteArrayToHexString(kp.Salt);
                scryptParams.DkLen = 32;
                scryptParams.P     = 1;
                scryptParams.R     = 8;
                scryptParams.N     = 8192;

                derivedKey = GetDerivedKey(Encoding.Default.GetBytes(passphrase), scryptParams);
            }
            string mac = ByteUtil.ByteArrayToHexString(HashUtil.GenerateMac(derivedKey, ciphertext));

            if (mac.ToUpper() != keystoreV3.Crypto.Mac)
            {
                throw new Exception("Failed to decrypt.");
            }

            byte[] encryptKey = new byte[16];
            Array.Copy(derivedKey, encryptKey, 16);

            KeyStoreCrypto cry = new KeyStoreCrypto();

            byte[] ciphertextByte = cry.GenerateAesCtrCipher(iv, encryptKey, ciphertext);

            return(ByteUtil.ByteArrayToHexString(ciphertextByte));
        }
コード例 #11
0
        public void EncodeTransactionProto()
        {
            TxParams txParams = new TxParams();

            txParams.Version      = "0";
            txParams.Nonce        = "0";
            txParams.ToAddr       = "2E3C9B415B19AE4035503A06192A0FAD76E04243";
            txParams.SenderPubKey = "0246e7178dc8253201101e18fd6f6eb9972451d121fc57aa2a06dd5c111e58dc6a";
            txParams.Amount       = "10000";
            txParams.GasPrice     = "100";
            txParams.GasLimit     = "1000";
            txParams.Code         = "";
            txParams.Data         = "";

            TransactionUtil util = new TransactionUtil();

            byte[] bytes = util.EncodeTransactionProto(txParams);
            Console.WriteLine(ByteUtil.ByteArrayToHexString(bytes));
        }
コード例 #12
0
ファイル: Account.cs プロジェクト: iantanwx/LaksaCsharp
 public Account(ECKeyPair keys)
 {
     this.keys    = keys;
     this.address = KeyTools.GetAddressFromPublicKey(ByteUtil.ByteArrayToHexString(this.keys.PublicKey.ToByteArray()));
 }
コード例 #13
0
        public string EncryptPrivateKey(string privateKey, string passphrase, KDFType type)
        {
            string address = KeyTools.GetAddressFromPrivateKey(privateKey);

            byte[] iv   = KeyTools.GenerateRandomBytes(16);
            byte[] salt = KeyTools.GenerateRandomBytes(32);
            byte[] derivedKey;
            if (type == KDFType.PBKDF2)
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();

                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(salt);
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                ScryptParams scryptParams = new ScryptParams();

                scryptParams.Salt = ByteUtil.ByteArrayToHexString(salt);

                scryptParams.DkLen = 32;
                scryptParams.P     = 1;
                scryptParams.R     = 8;
                scryptParams.N     = 8192;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), scryptParams);
            }

            byte[] encryptKey = new byte[16];
            Array.Copy(derivedKey, encryptKey, 16);

            KeyStoreCrypto cry = new KeyStoreCrypto();

            byte[] ciphertext = cry.GenerateAesCtrCipher(iv, encryptKey, ByteUtil.HexStringToByteArray(privateKey));
            byte[] mac        = HashUtil.GenerateMac(derivedKey, ciphertext);

            //build struct
            CipherParams cipherParams = new CipherParams();

            cipherParams.Iv = ByteUtil.ByteArrayToHexString(iv);

            Kdfparams kp     = new Kdfparams(ByteUtil.ToSbyte(salt));
            Crypto    crypto = new Crypto();

            crypto.Cipher       = "aes-128-ctr";
            crypto.Cipherparams = cipherParams;
            crypto.Ciphertext   = ByteUtil.ByteArrayToHexString(ciphertext);
            crypto.Kdf          = (type == KDFType.PBKDF2 ? "pbkdf2" : "scrypt");
            crypto.Kdfparams    = kp;
            crypto.Mac          = ByteUtil.ByteArrayToHexString(mac);

            KeystoreV3 key = new KeystoreV3();

            key.Address = address;
            key.Crypto  = crypto;
            key.Id      = Guid.NewGuid().ToString();
            key.Version = 3;

            return(JsonConvert.SerializeObject(key));
        }
コード例 #14
0
 public static string GetAddressFromPublicKey(string publicKey)
 {
     byte[] address = GetAddressFromPublicKey(Numeric.HexStringToByteArray(publicKey));
     return(ByteUtil.ByteArrayToHexString(address).Substring(24));
 }
コード例 #15
0
 public void MarshalToAddress()
 {
     byte[] address = Base58.Decode(this.ToAddr);
     this.ToAddr = ByteUtil.ByteArrayToHexString(address);
 }
コード例 #16
0
ファイル: Account.cs プロジェクト: iantanwx/LaksaCsharp
 public string GetPrivateKey()
 {
     return(ByteUtil.ByteArrayToHexString(this.keys.PrivateKey.ToByteArray()));
 }
コード例 #17
0
        public string EncryptPrivateKey(string privateKey, string passphrase, KDFType type)
        {
            string address = CryptoUtil.GetAddressFromPrivateKey(privateKey);

            byte[] iv   = CryptoUtil.GenerateRandomBytes(16);
            byte[] salt = CryptoUtil.GenerateRandomBytes(32);
            byte[] derivedKey;
            if (type == KDFType.PBKDF2)
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();

                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(salt);
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                var scryptParams = new KDFParams();

                scryptParams.Salt = ByteUtil.ByteArrayToHexString(salt);

                scryptParams.dklen = 32;
                scryptParams.p     = 1;
                scryptParams.r     = 8;
                scryptParams.n     = 8192;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), scryptParams);
            }

            byte[] encryptKey = new byte[16];
            Array.Copy(derivedKey, encryptKey, 16);

            KeyStore cry = new KeyStore();

            byte[] ciphertext = cry.GenerateAesCtrCipher(iv, encryptKey, ByteUtil.HexStringToByteArray(privateKey));
            //build struct
            CipherParams cipherParams = new CipherParams();

            cipherParams.Iv = ByteUtil.ByteArrayToHexString(iv);

            var kp = new KDFParams()
            {
                Salt = Encoding.UTF8.GetString(salt)
            };
            var crypto = new MusCipher();

            crypto.Cipher       = "aes-128-ctr";
            crypto.Cipherparams = cipherParams;
            crypto.Ciphertext   = ByteUtil.ByteArrayToHexString(ciphertext);
            crypto.Kdf          = (type == KDFType.PBKDF2 ? "pbkdf2" : "scrypt");
            crypto.KdfParams    = kp;
            crypto.Mac          = Encoding.UTF8.GetString(HashUtil.GenerateMac(derivedKey, ciphertext));

            KeyStore key = new KeyStore();

            key.Address = "0x" + address;
            key.Crypto  = crypto;
            key.Id      = Guid.NewGuid().ToString();
            key.Version = 3;

            return(JsonConvert.SerializeObject(key));
        }
コード例 #18
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            byte[] fileByteArray = File.ReadAllBytes(@"");
            byte[] searchBytes   = ByteUtil.HexStringToByteArray("1C E9 9D 00 00 00 8B 45 E8 8D 55 EC 52 89 5D EC 68 3F 3F 3F 54 8B 08 50 FF 51 78 85 C0 79 2D 8D 45 0C C7 45 0C");
            byte[] replaceBytes  = ByteUtil.HexStringToByteArray("1C E9 9D 00 00 00 8B 45 E8 8D 55 EC 52 89 5D EC EB 09 90 90 90 8B 08 50 FF 51 78 85 C0 79 2D 8D 45 0C C7 45 0C");
            //int[] indexs = FuzzyMatcher.MatchAll(fileByteArray, searchBytes);
            int[] indexs = FuzzyMatcher.MatchNotReplaced(fileByteArray, searchBytes, replaceBytes);
            txtInfo.AppendText("查找结果位置:" + string.Join(",", indexs) + Environment.NewLine);
            // 371130

            List <Change> changes = ComputChanges(indexs, searchBytes, replaceBytes);

            foreach (Change c in changes)
            {
                txtInfo.AppendText("替换位置:" + Convert.ToString(c.Position, 16) + " 替换内容:" + ByteUtil.ByteArrayToHexString(c.Content) + Environment.NewLine);
            }
        }
コード例 #19
0
ファイル: KeyStore.cs プロジェクト: yanbin007/LaksaCsharp
        public string EncryptPrivateKey(string privateKey, string passphrase, KDFType type)
        {
            string address = KeyTools.GetAddressFromPrivateKey(privateKey);

            byte[] iv   = KeyTools.GenerateRandomBytes(16);
            byte[] salt = KeyTools.GenerateRandomBytes(32);
            byte[] derivedKey;
            if (type == KDFType.PBKDF2)
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();

                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(salt);
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                ScryptParams scryptParams = new ScryptParams();

                scryptParams.Salt = ByteUtil.ByteArrayToHexString(salt);

                scryptParams.DkLen = 32;
                scryptParams.P     = 1;
                scryptParams.R     = 8;
                scryptParams.N     = 8192;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), scryptParams);
            }

            byte[] encryptKey = new byte[16];
            Array.Copy(derivedKey, encryptKey, 16);


            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key     = encryptKey,
                Mode    = CipherMode.CBC,
                Padding = System.Security.Cryptography.PaddingMode.None
            };
            //TODO 加密方法待完善

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
            byte[] ciphertext = cTransform.TransformFinalBlock(ByteUtil.HexStringToByteArray(privateKey), 0, ByteUtil.HexStringToByteArray(privateKey).Length);

            byte[] mac = HashUtil.GenerateMac(derivedKey, ciphertext);

            //build struct
            CipherParams cipherParams = new CipherParams();

            cipherParams.Iv = ByteUtil.ByteArrayToHexString(iv);

            Kdfparams kp     = new Kdfparams(salt);
            Crypto    crypto = new Crypto();

            crypto.Cipher       = "aes-128-ctr";
            crypto.Cipherparams = cipherParams;
            crypto.Ciphertext   = ByteUtil.ByteArrayToHexString(ciphertext);
            crypto.Kdf          = (type == KDFType.PBKDF2 ? "pbkdf2" : "scrypt");
            crypto.Kdfparams    = kp;
            crypto.Mac          = ByteUtil.ByteArrayToHexString(mac);

            KeystoreV3 key = new KeystoreV3();

            key.Address = address;
            key.Crypto  = crypto;
            key.Id      = Guid.NewGuid().ToString();
            key.Version = 3;

            return(JsonConvert.SerializeObject(key));
        }
コード例 #20
0
 public string GetPrivateKey()
 {
     return(ByteUtil.ByteArrayToHexString(KeyPair.PrivateKey.ToByteArray()));
 }