/// <summary> /// 导出私钥 /// </summary> /// <param name="passphrase">密码</param> /// <param name="N">scrypt</param> /// <param name="r">scrypt</param> /// <param name="p">scrypt</param> /// <returns>WIF私钥</returns> public string Export(string passphrase, int N = 16384, int r = 8, int p = 8) { //Console.WriteLine($"Export passphrase: {passphrase}"); using (Decrypt()) { UInt160 script_hash = Contract.CreateSignatureRedeemScript(PublicKey).ToScriptHash(); string address = Wallet.ToAddress(script_hash); byte[] addresshash = Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).ToArray(); byte[] derivedkey; if (string.IsNullOrEmpty(passphrase)) { derivedkey = SCrypt.DeriveKey(addresshash, N, r, p, 64); // 无密码 } else { derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, N, r, p, 64); } byte[] derivedhalf1 = derivedkey.Take(32).ToArray(); byte[] derivedhalf2 = derivedkey.Skip(32).ToArray(); byte[] encryptedkey = XOR(PrivateKey, derivedhalf1).AES256Encrypt(derivedhalf2); byte[] buffer = new byte[39]; buffer[0] = 0x01; buffer[1] = 0x42; buffer[2] = 0xe0; Buffer.BlockCopy(addresshash, 0, buffer, 3, addresshash.Length); Buffer.BlockCopy(encryptedkey, 0, buffer, 7, encryptedkey.Length); return(buffer.Base58CheckEncode()); } }
/// <summary> /// Encrypts a WIF key using a given keyphrase under NEP-2 Standard. /// </summary> /// <param name="wif">WIF key to encrypt (52 chars long).</param> /// <param name="password">The password will be encoded as UTF-8.</param> /// <param name="scryptParameters">Parameters for Scrypt. Defaults to NEP2 specified parameters.</param> /// <returns></returns> public static async Task <string> Encrypt(string wif, string password, ScryptParameters scryptParameters = null) { if (scryptParameters == null) { scryptParameters = ScryptParameters.Default; } using (Decrypt()) { var keyPair = new KeyPair(Wallet.GetPrivateKeyFromWif(wif)); var scriptHash = Helper.CreateSignatureRedeemScript(keyPair.PublicKey).ToScriptHash(); // todo move this method var address = Wallet.ToAddress(scriptHash); var addresshash = Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).ToArray(); return(await Task.Run(() => { var derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(password), addresshash, scryptParameters.N, scryptParameters.R, scryptParameters.P, 64); var derivedhalf1 = derivedkey.Take(32).ToArray(); var derivedhalf2 = derivedkey.Skip(32).ToArray(); var encryptedkey = XOR(keyPair.PrivateKey, derivedhalf1).AES256Encrypt(derivedhalf2); var buffer = new byte[39]; buffer[0] = 0x01; buffer[1] = 0x42; buffer[2] = 0xe0; Buffer.BlockCopy(addresshash, 0, buffer, 3, addresshash.Length); Buffer.BlockCopy(encryptedkey, 0, buffer, 7, encryptedkey.Length); return buffer.Base58CheckEncode(); })); } }
public string Export(string passphrase, int N = 16384, int r = 8, int p = 8) { using (Decrypt()) { //获取地址合约脚本哈希 UInt160 script_hash = Contract.CreateSignatureRedeemScript(PublicKey).ToScriptHash(); //获取地址 string address = Wallet.ToAddress(script_hash); //获取地址摘要前四字节 byte[] addresshash = Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).ToArray(); //计算scrypt key byte[] derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, N, r, p, 64); byte[] derivedhalf1 = derivedkey.Take(32).ToArray(); byte[] derivedhalf2 = derivedkey.Skip(32).ToArray(); //aes加密 byte[] encryptedkey = XOR(PrivateKey, derivedhalf1).AES256Encrypt(derivedhalf2); byte[] buffer = new byte[39]; //校验位 buffer[0] = 0x01; buffer[1] = 0x42; buffer[2] = 0xe0; //将地址摘要前四字节写入缓存 Buffer.BlockCopy(addresshash, 0, buffer, 3, addresshash.Length); //密文写入缓存 Buffer.BlockCopy(encryptedkey, 0, buffer, 7, encryptedkey.Length); //base58加密 return(buffer.Base58CheckEncode()); } }
public static byte[] GetPrivateKeyFromBRC2(string brc2, string passphrase, int N = 16384, int r = 8, int p = 8) { if (brc2 == null) { throw new ArgumentNullException(nameof(brc2)); } if (passphrase == null) { throw new ArgumentNullException(nameof(passphrase)); } byte[] data = brc2.Base58CheckDecode(); if (data.Length != 39 || data[0] != 0x01 || data[1] != 0x42 || data[2] != 0xe0) { throw new FormatException(); } byte[] addresshash = new byte[4]; Buffer.BlockCopy(data, 3, addresshash, 0, 4); byte[] derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, N, r, p, 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); Cryptography.ECC.ECPoint pubkey = Cryptography.ECC.ECCurve.Secp256r1.G * prikey; UInt160 script_hash = Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash(); string address = script_hash.ToAddress(); if (!Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).SequenceEqual(addresshash)) { throw new FormatException(); } return(prikey); }
/// <summary> /// Decrypts a NEP2 key using a passphrase and the ScryptParameters, returning a private key /// </summary> /// <param name="nep2"></param> /// <param name="passphrase"></param> /// <param name="scryptParameters"></param> /// <returns>private key</returns> public static byte[] DecryptKey(string nep2, string passphrase, ScryptParameters scryptParameters) { 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(); } byte[] addresshash = new byte[4]; Buffer.BlockCopy(data, 3, addresshash, 0, 4); byte[] derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, scryptParameters.N, scryptParameters.R, scryptParameters.P, 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); ECPoint pubkey = ECCurve.Secp256r1.G * prikey; UInt160 scriptHash = Neo.SmartContract.Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash(); string address = Wallet.ToAddress(scriptHash); if (!Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).SequenceEqual(addresshash)) { throw new FormatException(); } return(prikey); }
public void DeriveKeyTest() { int N = 16384, r = 8, p = 8; var derivedkey = SCrypt.DeriveKey(new byte[] { 0x01, 0x02, 0x03 }, new byte[] { 0x04, 0x05, 0x06 }, N, r, p, 64).ToHexString(); Assert.AreEqual("2bb9c7bb9c392f0dd37821b76e42b01944902520f48d00946a51e72c960fba0a3c62a87d835c9df10a8ad66a04cdf02fbb10b9d7396c20959f28d6cb3ddfdffb", derivedkey); }
public void DeriveKeyTest() { int N = 32, r = 2, p = 2; var derivedkey = SCrypt.DeriveKey(new byte[] { 0x01, 0x02, 0x03 }, new byte[] { 0x04, 0x05, 0x06 }, N, r, p, 64).ToHexString(); Assert.AreEqual("b6274d3a81892c24335ab46a08ec16d040ac00c5943b212099a44b76a9b8102631ab988fa07fb35357cee7b0e3910098c0774c0e97399997676d890b2bf2bb25", derivedkey); }
public static byte[] GetPrivateKeyFromNEP2(string nep2, string passphrase, int N = 16384, int r = 8, int p = 8) { //if (nep2 == null) throw new ArgumentNullException(nameof(nep2)); //if (passphrase == null) throw new ArgumentNullException(nameof(passphrase)); byte[] data = Base58CheckDecode(nep2); var dataStr = data.ToHexString(); //0142e05f50e9507f66015665c07c9d42645f38a15efc7895e2dee07110d0edf10b30cafbcdccc5 //if (data.Length != 39 || data[0] != 0x01 || data[1] != 0x42 || data[2] != 0xe0) // throw new FormatException(); byte[] addresshash = new byte[4]; Buffer.BlockCopy(data, 3, addresshash, 0, 4); var addresshashStr = addresshash.ToHexString(); //5f50e950 byte[] derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, N, r, p, 64); var derivedkeyStr = derivedkey.ToHexString(); //179d95651ca3c7b47b86fef395f597da3214c7e057b440febf7e40e9c0fb51b04936b4c2a3f7e29e40dfad46e4a0b0b320cc72b735c8d77b0030e68869d57e69 byte[] derivedhalf1 = derivedkey.Take(32).ToArray(); var derivedhalf1Str = derivedhalf1.ToHexString(); //179d95651ca3c7b47b86fef395f597da3214c7e057b440febf7e40e9c0fb51b0 byte[] derivedhalf2 = derivedkey.Skip(32).ToArray(); var derivedhalf2Str = derivedhalf2.ToHexString(); //4936b4c2a3f7e29e40dfad46e4a0b0b320cc72b735c8d77b0030e68869d57e69 byte[] encryptedkey = new byte[32]; Buffer.BlockCopy(data, 7, encryptedkey, 0, 32); var encryptedkeyStr = encryptedkey.ToHexString(); //7f66015665c07c9d42645f38a15efc7895e2dee07110d0edf10b30cafbcdccc5 byte[] prikey = XOR(AES256Decrypt(encryptedkey, derivedhalf2), derivedhalf1); var prikeyStr = prikey.ToHexString(); //25228cd2b2aeb4fec8065cadab8dc28cb618fba3a789853fcda357c37f0864c1 //Cryptography.ECC.ECPoint pubkey = Cryptography.ECC.ECCurve.Secp256r1.G * prikey; //UInt160 script_hash = Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash(); //string address = ToAddress(script_hash); //if (!Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).SequenceEqual(addresshash)) // throw new FormatException(); return(prikey); }
private static byte[] GenerateKey(string password) { const ulong n = 16384; const uint r = 16, p = 1; var salt = SCrypt.GenerateSalt(saltLengthBytes: 16u, n, r, p, hashLengthBytes: 128u); SCrypt.ParseSalt(salt, out var saltBytes, out _, out _, out _, out var hashLengthBytes); var passwordBytes = Encoding.Unicode.GetBytes(password); var key = SCrypt.DeriveKey(passwordBytes, saltBytes, n, r, p, 64u); return(key); }
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); }
//============================================================================ public void doScrypt(object args) { Array argArray = new object[4]; argArray = (Array)args; byte[] tdatabyte = (byte[])argArray.GetValue(0); byte[] databyte = new byte[80]; Array.Copy(tdatabyte, 0, databyte, 0, 76); byte[] target = (byte[])argArray.GetValue(1); uint nonce = (uint)argArray.GetValue(2); uint x = (uint)argArray.GetValue(3); byte[] voide = new byte[4]; try { byte[] scrypted = new byte[32]; //Loop over and increment nonce while (!done) { databyte[76] = (byte)(nonce >> 0); databyte[77] = (byte)(nonce >> 8); databyte[78] = (byte)(nonce >> 16); databyte[79] = (byte)(nonce >> 24); scrypted = SCrypt.DeriveKey(databyte, databyte, 1024, 1, 1, 32); hr++; if (meetsTarget(scrypted, target)) { if (!done) { fnonce = nonce; } done = true; break; } else { nonce += x; //Otherwise increment the nonce } elapsedtime = (DateTime.Now - D0).Milliseconds; if (longpooling == null && elapsedtime >= 1) { done = true; break; } } } catch (Exception ex) { Console.WriteLine(ex); fnonce = 0; } }
/// <summary> /// Decrypts an encrypted key using a given keyphrase under NEP-2 Standard. /// </summary> /// <param name="encryptedKey"></param> /// <param name="password"></param> /// <param name="scryptParameters"></param> /// <returns></returns> public static async Task <byte[]> Decrypt(string encryptedKey, string password, ScryptParameters scryptParameters = null) { if (encryptedKey == null) { throw new ArgumentNullException(nameof(encryptedKey)); } if (password == null) { throw new ArgumentNullException(nameof(password)); } if (scryptParameters == null) { scryptParameters = ScryptParameters.Default; } var data = encryptedKey.Base58CheckDecode(); if (data.Length != 39 || data[0] != 0x01 || data[1] != 0x42 || data[2] != 0xe0) { throw new FormatException(); } var addresshash = new byte[4]; Buffer.BlockCopy(data, 3, addresshash, 0, 4); return(await Task.Run(() => { var derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(password), addresshash, scryptParameters.N, scryptParameters.R, scryptParameters.P, 64); var derivedhalf1 = derivedkey.Take(32).ToArray(); var derivedhalf2 = derivedkey.Skip(32).ToArray(); var encryptedkey = new byte[32]; Buffer.BlockCopy(data, 7, encryptedkey, 0, 32); var prikey = XOR(encryptedkey.AES256Decrypt(derivedhalf2), derivedhalf1); var pubkey = ECCurve.Secp256r1.G *prikey; var scriptHash = Helper.CreateSignatureRedeemScript(pubkey).ToScriptHash(); var address = scriptHash.ToAddress(); if (!Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).SequenceEqual(addresshash)) { throw new FormatException(); } return prikey; })); }
/// <summary> /// Encrypts a private key using a passphrase and the ScryptParameters, returning the NEP2 key in string format /// </summary> /// <param name="passphrase"></param> /// <param name="keyPair"></param> /// <param name="scryptParameters"></param> /// <returns>NEP2</returns> public static string EncryptKey(string passphrase, KeyPair keyPair, ScryptParameters scryptParameters) { UInt160 scriptHash = Neo.SmartContract.Contract.CreateSignatureRedeemScript(keyPair.PublicKey).ToScriptHash(); string address = Wallet.ToAddress(scriptHash); byte[] addresshash = Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).ToArray(); byte[] derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, scryptParameters.N, scryptParameters.R, scryptParameters.P, 64); byte[] derivedhalf1 = derivedkey.Take(32).ToArray(); byte[] derivedhalf2 = derivedkey.Skip(32).ToArray(); byte[] encryptedkey = Xor(keyPair.PrivateKey, derivedhalf1).Aes256Encrypt(derivedhalf2); byte[] buffer = new byte[39]; buffer[0] = 0x01; buffer[1] = 0x42; buffer[2] = 0xe0; Buffer.BlockCopy(addresshash, 0, buffer, 3, addresshash.Length); Buffer.BlockCopy(encryptedkey, 0, buffer, 7, encryptedkey.Length); return(buffer.Base58CheckEncode()); }
public static byte[] GetPrivateKeyFromNEP2(string nep2, string passphrase, int N = 16384, int r = 8, int p = 8) { if (nep2 == null) { throw new ArgumentNullException(nameof(nep2)); } if (passphrase == null) { throw new ArgumentNullException(nameof(passphrase)); } byte[] data = Base58CheckDecode(nep2); if (data.Length != 39 || data[0] != 0x01 || data[1] != 0x42 || data[2] != 0xe0) { throw new FormatException(); } byte[] addresshash = new byte[4]; Buffer.BlockCopy(data, 3, addresshash, 0, 4); byte[] derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, N, r, p, 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(AES256Decrypt(encryptedkey, derivedhalf2), derivedhalf1); var pubkey = GetPublicKeyFromPrivateKey(prikey); var address = GetAddressFromPublicKey(pubkey); var hash = Sha256(Encoding.ASCII.GetBytes(address)); hash = Sha256(hash); for (var i = 0; i < 4; i++) { if (hash[i] != addresshash[i]) { throw new Exception("check error."); } } //Cryptography.ECC.ECPoint pubkey = Cryptography.ECC.ECCurve.Secp256r1.G * prikey; //UInt160 script_hash = Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash(); //string address = ToAddress(script_hash); //if (!Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).SequenceEqual(addresshash)) // throw new FormatException(); return(prikey); }
public static byte[] GetPrivateKeyFromNEP2(string nep2, string passphrase, int N = 16384, int r = 8, int p = 8) { if (nep2 == null) { throw new ArgumentNullException(nameof(nep2)); } if (passphrase == null) { throw new ArgumentNullException(nameof(passphrase)); } //base58解密 byte[] data = nep2.Base58CheckDecode(); //格式校验 if (data.Length != 39 || data[0] != 0x01 || data[1] != 0x42 || data[2] != 0xe0) { throw new FormatException(); } byte[] addresshash = new byte[4]; //读取地址哈希 Buffer.BlockCopy(data, 3, addresshash, 0, 4); //计算scrypt key 这里结果和加密的 scrypt key需要相同 byte[] derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(passphrase), addresshash, N, r, p, 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); //aes解密获取私钥 byte[] prikey = XOR(encryptedkey.AES256Decrypt(derivedhalf2), derivedhalf1); //计算公钥 Cryptography.ECC.ECPoint pubkey = Cryptography.ECC.ECCurve.Secp256r1.G * prikey; //获取账户合约脚本哈希 UInt160 script_hash = Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash(); //计算地址 string address = ToAddress(script_hash); //验证解密结果 if (!Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).SequenceEqual(addresshash)) { throw new FormatException(); } return(prikey); }
public string Export(string passphrase) { using (Decrypt()) { UInt160 script_hash = Contract.CreateSignatureRedeemScript(PublicKey).ToScriptHash(); string address = Wallet.ToAddress(script_hash); byte[] addresshash = Encoding.ASCII.GetBytes(address).Sha256().Sha256().Take(4).ToArray(); 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 = XOR(PrivateKey, derivedhalf1).AES256Encrypt(derivedhalf2); byte[] buffer = new byte[39]; buffer[0] = 0x00; buffer[1] = 0x02; buffer[2] = 0x19; Buffer.BlockCopy(addresshash, 0, buffer, 3, addresshash.Length); Buffer.BlockCopy(encryptedkey, 0, buffer, 7, encryptedkey.Length); return(buffer.Base58CheckEncode()); } }
public static NeoKeys FromNEP2(string nep2, string passphrase, int N = 16384, int r = 8, int p = 8) { Throw.IfNull(nep2, nameof(nep2)); Throw.IfNull(passphrase, nameof(passphrase)); byte[] data = nep2.Base58CheckDecode(); if (data.Length != 39 || data[0] != 0x01 || data[1] != 0x42 || data[2] != 0xe0) { throw new FormatException(); } byte[] addressHash = new byte[4]; Buffer.BlockCopy(data, 3, addressHash, 0, 4); byte[] datapassphrase = Encoding.UTF8.GetBytes(passphrase); byte[] derivedkey = SCrypt.DeriveKey(datapassphrase, addressHash, N, r, p, 64); Array.Clear(datapassphrase, 0, datapassphrase.Length); byte[] derivedhalf1 = derivedkey.Take(32).ToArray(); byte[] derivedhalf2 = derivedkey.Skip(32).ToArray(); Array.Clear(derivedkey, 0, derivedkey.Length); byte[] encryptedkey = new byte[32]; Buffer.BlockCopy(data, 7, encryptedkey, 0, 32); Array.Clear(data, 0, data.Length); byte[] prikey = XOR(encryptedkey.AES256Decrypt(derivedhalf2), derivedhalf1); Array.Clear(derivedhalf1, 0, derivedhalf1.Length); Array.Clear(derivedhalf2, 0, derivedhalf2.Length); ECPoint pubkey = ECCurve.Secp256r1.G * prikey; var keys = new NeoKeys(prikey); var temp = Encoding.ASCII.GetBytes(keys.Address).Sha256().Sha256().Take(4).ToArray(); if (!temp.SequenceEqual(addressHash)) { throw new FormatException("invalid passphrase when decrypting NEP2"); } return(keys); }
public void createNewAccount() { if (createAccount_nameInput.text != "" && createAccount_nameInput.text != null) { accountName = accountNameInput.text = createAccount_nameInput.text; PlayerPrefs.SetString("neoAccount", createAccount_nameInput.text); Debug.Log("account: '" + accountName + "' created and stored in PlayerPrefs"); } else { loginStatus.text = "Please supply an account name"; return; } // TODO: Add check for password if (createAccount_passwordInput.text != "" && createAccount_passwordInput.text != null) { password = createAccount_passwordInput.text; Debug.Log("password: "******"Please supply a password"; return; } // TODO: write a function to do all of the following key generation and wif encryption byte[] privateKey = new byte[32]; // generate a new private key RandomNumberGenerator rng = RandomNumberGenerator.Create(); rng.GetBytes(privateKey); // generate a key pair keys = new KeyPair(privateKey); // for loading specific private key strings, do it this way //keys = new KeyPair("a9e2b5436cab6ff74be2d5c91b8a67053494ab5b454ac2851f872fb0fd30ba5e".HexToBytes()); addressLabel.text = keys.address; balanceLabel.text = "Please WAIT, syncing balance: ..."; wifLabel.text = keys.WIF; // WIF ENCRYPTION byte[] addresshash = Encoding.ASCII.GetBytes(keys.address).Sha256().Sha256().Take(4).ToArray(); // these are the hardcoded scrypt defaults per neo-gui / neon-js: int N = 16384, int r = 8, int p = 8 byte[] derivedkey = SCrypt.DeriveKey(Encoding.UTF8.GetBytes(password), addresshash, 16384, 8, 8, 64); Debug.Log("derived key: " + derivedkey.ByteToHex()); byte[] derivedhalf1 = derivedkey.Take(32).ToArray(); byte[] derivedhalf2 = derivedkey.Skip(32).ToArray(); byte[] encryptedkey = XOR(keys.PrivateKey, derivedhalf1).AES256Encrypt(derivedhalf2); byte[] buffer = new byte[39]; buffer [0] = 0x01; buffer [1] = 0x42; buffer [2] = 0xe0; Buffer.BlockCopy(addresshash, 0, buffer, 3, addresshash.Length); Buffer.BlockCopy(encryptedkey, 0, buffer, 7, encryptedkey.Length); encryptedWif = buffer.Base58CheckEncode(); Debug.Log("WIF: " + keys.WIF); Debug.Log("Encrypted WIF: " + encryptedWif); Debug.Log("Encrypted Private Key: " + encryptedkey.ByteToHex()); Debug.Log("Private Key: " + keys.PrivateKey.ByteToHex()); Debug.Log("Public Key: " + keys.PublicKey.ByteToHex()); Debug.Log("Address: " + keys.address); encryptedWifInput.text = encryptedWif; PlayerPrefs.SetString(accountName, encryptedWif); newAccountPanel.gameObject.SetActive(false); loginPanel.gameObject.SetActive(true); }