예제 #1
0
        /// <summary>
        /// 修改密码并返回新的钱包json
        /// </summary>
        /// <param name="address"></param>
        /// <param name="newPwd"></param>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string ChangePWD(string address, string newPwd, string privateKey)
        {
            var scryptService = new KeyStoreScryptService();
            var scryptResult  = scryptService.EncryptAndGenerateKeyStoreAsJson(newPwd, privateKey.HexToByteArray(), address.Replace("0x", ""), scryptParams);

            return(scryptResult);
        }
예제 #2
0
        private string DecryptPrivateKeyFromScryptKeystore(string scrypt_keystore, string password)
        {
            var keyStoreScryptService = new KeyStoreScryptService();
            var keyStore            = keyStoreScryptService.DeserializeKeyStoreFromJson(scrypt_keystore);
            var privateKeyDecrypted = keyStoreScryptService.DecryptKeyStore(password, keyStore);

            return(privateKeyDecrypted.ToHex());
        }
예제 #3
0
        public static void Main(string[] args)
        {

            //Reading an existing file
            var address = "12890d2cce102216644c59dae5baed380d84830c";
            var password = "******";
            //UTC--2016-11-23T09-58-36Z--ca2137bc-e2a1-5c40-d60d-ab8cf5fb302c

            var file = File.OpenText("UTC--2016-11-23T09-58-36Z--ca2137bc-e2a1-5c40-d60d-ab8cf5fb302c");
           // var file = File.OpenText("UTC--2015-11-25T05-05-03.116905600Z--12890d2cce102216644c59dae5baed380d84830c");
            var json = file.ReadToEnd();
            
            //using the simple key store service
            var service = new KeyStoreService();
            //decrypt the private key
            var key = service.DecryptKeyStoreFromJson(password, json);
            
           //Generating a new file using the existing private key (key) 
           //create new file with the standard UTC file name
            var fileName = service.GenerateUTCFileName(address);
            using (var newfile = File.CreateText(fileName))
            {
                //generate the encrypted and key store content as json. (The default uses pbkdf2)
                var newJson = service.EncryptAndGenerateDefaultKeyStoreAsJson(password, key, address);
                newfile.Write(newJson);
                newfile.Flush();
            }
            
            //Decrypting the new file created
            file = File.OpenText(fileName);
            json = file.ReadToEnd();

            var newkey = service.DecryptKeyStoreFromJson(password, json);

            //Compare the keys
            System.Console.WriteLine("Original key: " + key.ToHex());
            System.Console.WriteLine("New key: " + key.ToHex());
            System.Console.ReadLine();


            //We can use EthECKey to generate a new ECKey pair, this is using SecureRandom
            var ecKey = EthECKey.GenerateKey();
            var privateKey = ecKey.GetPrivateKeyAsBytes();
            var genAddress = ecKey.GetPublicAddress();

            //instead of the default service we can use either
            //Scrypt
            var scryptService = new KeyStoreScryptService();
            var scryptResult = scryptService.EncryptAndGenerateKeyStoreAsJson(password, privateKey, genAddress);
            //or pkbdf2
            var pbkdf2Service = new KeyStorePbkdf2Service();
            var pkbdf2Result = pbkdf2Service.EncryptAndGenerateKeyStoreAsJson(password, privateKey, genAddress);

            //Both services can be configured with a new IRandomBytesGenerator for the IV and Salt, currently uses SecureRandom for both.
            //also when encrypting we can pass custom KdfParameters
        }
예제 #4
0
        public static void Main(string[] args)
        {
            //Reading an existing file
            var address  = "12890d2cce102216644c59dae5baed380d84830c";
            var password = "******";
            //UTC--2016-11-23T09-58-36Z--ca2137bc-e2a1-5c40-d60d-ab8cf5fb302c

            var file = File.OpenText("UTC--2016-11-23T09-58-36Z--ca2137bc-e2a1-5c40-d60d-ab8cf5fb302c");
            // var file = File.OpenText("UTC--2015-11-25T05-05-03.116905600Z--12890d2cce102216644c59dae5baed380d84830c");
            var json = file.ReadToEnd();

            //using the simple key store service
            var service = new KeyStoreService();
            //decrypt the private key
            var key = service.DecryptKeyStoreFromJson(password, json);

            //Generating a new file using the existing private key (key)
            //create new file with the standard UTC file name
            var fileName = service.GenerateUTCFileName(address);

            using (var newfile = File.CreateText(fileName))
            {
                //generate the encrypted and key store content as json. (The default uses pbkdf2)
                var newJson = service.EncryptAndGenerateDefaultKeyStoreAsJson(password, key, address);
                newfile.Write(newJson);
                newfile.Flush();
            }

            //Decrypting the new file created
            file = File.OpenText(fileName);
            json = file.ReadToEnd();

            var newkey = service.DecryptKeyStoreFromJson(password, json);

            //Compare the keys
            System.Console.WriteLine("Original key: " + key.ToHex());
            System.Console.WriteLine("New key: " + key.ToHex());
            System.Console.ReadLine();


            //We can use EthECKey to generate a new ECKey pair, this is using SecureRandom
            var ecKey      = EthECKey.GenerateKey();
            var privateKey = ecKey.GetPrivateKeyAsBytes();
            var genAddress = ecKey.GetPublicAddress();

            //instead of the default service we can use either
            //Scrypt
            var scryptService = new KeyStoreScryptService();
            var scryptResult  = scryptService.EncryptAndGenerateKeyStoreAsJson(password, privateKey, genAddress);
            //or pkbdf2
            var pbkdf2Service = new KeyStorePbkdf2Service();
            var pkbdf2Result  = pbkdf2Service.EncryptAndGenerateKeyStoreAsJson(password, privateKey, genAddress);

            //Both services can be configured with a new IRandomBytesGenerator for the IV and Salt, currently uses SecureRandom for both.
            //also when encrypting we can pass custom KdfParameters
        }
        public void ShouldGenerateAccountAndCreateKeyStoreFileScrypt()
        {
            var ecKey = Nethereum.Signer.EthECKey.GenerateKey();
            var keyStoreScryptService = new KeyStoreScryptService();
            var password = "******";
            var json     = keyStoreScryptService.EncryptAndGenerateKeyStoreAsJson(password, ecKey.GetPrivateKeyAsBytes(), ecKey.GetPublicAddress());
            var key      = keyStoreScryptService.DecryptKeyStoreFromJson(password, json);

            Assert.Equal(ecKey.GetPrivateKey(), key.ToHex(true));
        }
        public void ShouldDecryptScrypt_Kdf()
        {
            string password              = "******";
            string privateKey            = "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d";
            var    keyStoreScryptService = new KeyStoreScryptService();
            var    keyStore              = keyStoreScryptService.DeserializeKeyStoreFromJson(scryptKeyStoreDocument);
            var    privateKeyDecrypted   = keyStoreScryptService.DecryptKeyStore(password, keyStore);

            Assert.Equal(privateKey, privateKeyDecrypted.ToHex());
        }
        public void ShouldEncryptAndDecryptScrypt_Kdf()
        {
            string password              = "******";
            string privateKey            = "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d";
            string account               = "x";
            var    keyStoreScryptService = new KeyStoreScryptService();
            var    keyStoreJson          = keyStoreScryptService.EncryptAndGenerateKeyStoreAsJson(password, privateKey.HexToByteArray(), account);
            var    keyStore              = keyStoreScryptService.DeserializeKeyStoreFromJson(keyStoreJson);
            var    privateKeyDecrypted   = keyStoreScryptService.DecryptKeyStore(password, keyStore);

            Assert.Equal(privateKey, privateKeyDecrypted.ToHex());
        }
예제 #8
0
        public static string GetCoolAddress(EthECKey key, string password)
        {
            var scryptService = new KeyStoreScryptService();
            var scryptResult  = scryptService.EncryptAndGenerateKeyStoreAsJson(password, key.GetPrivateKeyAsBytes(), key.GetPublicAddress().ToLower().Replace("0x", ""), scryptParams);

            var path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + key.GetPublicAddress().ToLower();

            using (var newfile = File.CreateText(path))
            {
                newfile.Write(scryptResult);
                newfile.Flush();
                return(scryptResult);
            }
        }
예제 #9
0
        public void GenerateNewAccountFile(string pass, string path)
        {
            var ecKey = EthECKey.GenerateKey();

            byte[] privateKey = null;
            while (true)
            {
                privateKey = ecKey.GetPrivateKeyAsBytes();
                if (privateKey.Length == 32)
                {
                    break;
                }
                ecKey = EthECKey.GenerateKey();
            }
            var genAddress = ecKey.GetPublicAddress();

            var service  = new KeyStoreService();
            var fileName = service.GenerateUTCFileName(genAddress);

            //using (var newfile = File.CreateText(fileName))
            //{
            //    //generate the encrypted and key store content as json. (The default uses pbkdf2)
            //    var newJson = service.EncryptAndGenerateDefaultKeyStoreAsJson(password, privateKey, genAddress);
            //    newfile.Write(newJson);
            //    newfile.Flush();
            //}

            //instead of the default service we can use either
            //Scrypt
            using (var newfile = File.CreateText(Path.Combine(path, fileName)))
            {
                //generate the encrypted and key store content as json. (The default uses pbkdf2)
                var scryptService = new KeyStoreScryptService();
                var scryptResult  = scryptService.EncryptAndGenerateKeyStoreAsJson(pass, privateKey, genAddress);
                newfile.Write(scryptResult);
                newfile.Flush();
            }

            Process.Start(path);

            //or pkbdf2
            //var pbkdf2Service = new KeyStorePbkdf2Service();
            //var pkbdf2Result = pbkdf2Service.EncryptAndGenerateKeyStoreAsJson(password, privateKey, genAddress);

            //File.WriteAllText("UTC--" + DateTime.UtcNow.ToString("o",
            //                                CultureInfo.InvariantCulture).Replace(':', '-') + "--" + genAddress, scryptResult);

            //Both services can be configured with a new IRandomBytesGenerator for the IV and Salt, currently uses SecureRandom for both.
            //also when encrypting we can pass custom KdfParameters
        }
        public void GenerateKeyStore(string password, byte[] pk, string address)
        {
            var keyStoreService = new KeyStoreScryptService();
            var scryptParams    = new ScryptParams {
                Dklen = 32, N = 262144, R = 1, P = 8
            };

            KeyStore <ScryptParams> store = keyStoreService.EncryptAndGenerateKeyStore(
                password, pk, address, scryptParams
                );

            string json = keyStoreService.SerializeKeyStoreToJson(store);

            GenerateFile(json, $"KeyStore_{DateTime.Now.ToShortDateString().Replace("/","-")}.json");
        }
예제 #11
0
        async void OnSaveWalletClicked(object sender, EventArgs e)
        {
            txtpwd.Text = txtpwd.Text.Trim();
            if (txtpwd.Text.Trim() == "" || txtpwd.Text.Length < 8)
            {
                await DisplayAlert("Password Error!", "Password length cannot be less than 8", "OK");

                return;
            }
            (sender as Button).IsEnabled = false;
            try
            {
                bool answer = await DisplayAlert("Password Confirm!", "Password:"******"Yes", "No");

                if (answer)
                {
                    try
                    {
                        WalletInfo wi    = new WalletInfo();
                        var        ecKey = EthECKey.GenerateKey();
                        wi.PublicKey  = HexByteConvertorExtensions.ToHex(ecKey.GetPubKey());
                        wi.PrivateKey = ecKey.GetPrivateKey();

                        wi.Address = ecKey.GetPublicAddress().ToLower();
                        var scryptService = new KeyStoreScryptService();
                        wi.FJson = scryptService.EncryptAndGenerateKeyStoreAsJson(txtpwd.Text, ecKey.GetPrivateKeyAsBytes()
                                                                                  , wi.Address.Replace("0x", ""), scryptParams);
                        await App.Wdb.SaveWalletAsync(wi);

                        txtpwd.Text = "";
                        WalletBind();
                        await DisplayAlert("Tips", "Success", "OK");
                    }
                    catch (Exception ex)
                    {
                        await DisplayAlert("Exception!", "Try again later " + ex.Message, "OK");
                    }
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("Exception!", "Try again later " + ex.Message, "OK");
            }
            (sender as Button).IsEnabled = true;
        }
예제 #12
0
        /// <summary>
        /// 生成一个钱包文件
        /// </summary>
        /// <param name="password"></param>
        /// <param name="path">钱包存放路径</param>
        /// <returns>返回生成的钱包地址</returns>
        public static string GenerateKeyStore(string password, string path)
        {
            var ecKey = EthECKey.GenerateKey();

            Debug.WriteLine("PrivateKey: " + ecKey.GetPrivateKey());
            Debug.WriteLine("PubKey: " + ecKey.GetPubKey().ToHex(true));
            Debug.WriteLine("PublicAddress: " + ecKey.GetPublicAddress());
            var privateKey = ecKey.GetPrivateKeyAsBytes();
            var genAddress = ecKey.GetPublicAddress().ToLower();

            var scryptService = new KeyStoreScryptService();
            var scryptResult  = scryptService.EncryptAndGenerateKeyStoreAsJson(password, privateKey, genAddress.Replace("0x", ""), scryptParams);

            path = path + "\\" + genAddress;
            using (var newfile = File.CreateText(path))
            {
                newfile.Write(scryptResult);
                newfile.Flush();
                return(genAddress);
            }
        }
예제 #13
0
        async void OnImportWalletClicked(object sender, EventArgs e)
        {
            try
            {
                var result = await Plugin.FilePicker.CrossFilePicker.Current.PickFile();

                if (result != null)
                {
                    var          stream     = result.GetStream();
                    StreamReader reader     = new StreamReader(stream);
                    string       str        = reader.ReadLine();
                    var          resultJson = JsonConvert.DeserializeObject <dynamic>(str);
                    string       address    = "0x" + resultJson.address;
                    if (address.Length > 20)
                    {
                        var wallet = App.Wdb.GetWalletAsyncByAddress(address).Result;
                        if (wallet == null)
                        {
                            WalletInfo wi = new WalletInfo();

                            wi.PublicKey  = "";
                            wi.PrivateKey = "";

                            wi.Address = address;
                            var scryptService = new KeyStoreScryptService();
                            wi.FJson = str;
                            await App.Wdb.SaveWalletAsync(wi);
                            await DisplayAlert("Tips", "Success", "OK");

                            WalletBind();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("Exception!", "Try again later " + ex.Message, "OK");
            }
        }
예제 #14
0
 public void ShouldDecryptScrypt_Kdf()
 {
     string password = "******";
     string privateKey = "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d";
     var keyStoreScryptService = new KeyStoreScryptService();
     var keyStore = keyStoreScryptService.DeserializeKeyStoreFromJson(scryptKeyStoreDocument);
     var privateKeyDecrypted = keyStoreScryptService.DecryptKeyStore(password, keyStore);
     Assert.Equal(privateKey, privateKeyDecrypted.ToHex());
 }
예제 #15
0
        public void ShouldEncryptAndDecryptScrypt_Kdf()
        {
            string password = "******";
            string privateKey = "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d";
            string account = "x";
            var keyStoreScryptService = new KeyStoreScryptService();
            var keyStoreJson = keyStoreScryptService.EncryptAndGenerateKeyStoreAsJson(password, privateKey.HexToByteArray(), account);
            var keyStore = keyStoreScryptService.DeserializeKeyStoreFromJson(keyStoreJson);
            var privateKeyDecrypted = keyStoreScryptService.DecryptKeyStore(password, keyStore);
            Assert.Equal(privateKey, privateKeyDecrypted.ToHex());

        }
예제 #16
0
 public KeyStoreService(KeyStoreKdfChecker keyStoreKdfChecker, KeyStoreScryptService keyStoreScryptService, KeyStorePbkdf2Service keyStorePbkdf2Service)
 {
     _keyStoreKdfChecker = keyStoreKdfChecker;
     _keyStoreScryptService = keyStoreScryptService;
     _keyStorePbkdf2Service = keyStorePbkdf2Service;
 }
예제 #17
0
 public KeyStoreService()
 {
     _keyStoreKdfChecker = new KeyStoreKdfChecker();
     _keyStorePbkdf2Service = new KeyStorePbkdf2Service();
     _keyStoreScryptService = new KeyStoreScryptService();
 }