/// <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); }
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 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()); }
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); } }
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 }
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; }
/// <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); } }