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