private static KeyStoreV3 <ScryptParams> EncryptKey(byte[] key, string address, string password, ScryptParams kdfParams) { if (key.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(key)); } if (password.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(password)); } // random values ( salt, iv ) var salt = kdfParams.salt; var cipherParams = new CipherParams(); // derivedKey -> cipherKey -> cipherText -> mac var derivedKey = PbkdfCrypt.GenerateDerivedScryptKey(password, salt.ToByteArray(), kdfParams.n, kdfParams.r, kdfParams.p, kdfParams.dklen); var cipherKey = PbkdfCrypt.GenerateCipherKey(derivedKey); var cipherText = PbkdfCrypt.GenerateAesCtrCipher(cipherParams.iv.ToByteArray(), cipherKey, key); var mac = PbkdfCrypt.GenerateMac(derivedKey, cipherText); return(new KeyStoreV3 <ScryptParams>() { version = Version, id = Guid.NewGuid().ToString(), address = address, crypto = { ciphertext = cipherText.ToHexString(), cipherparams = cipherParams, cipher = CIPHER, kdf = KdfType.scrypt.ToString(), kdfparams = kdfParams, mac = mac.ToHexString() } }); }
internal static KeyStoreV3 <ScryptParams> EncryptKey(PrivateKey key, string password, ScryptParams kdfParams) { if (ReferenceEquals(key, null)) { throw new KdfException(ErrorCode.BAD_ARGUMENT, "empty key"); } if (string.IsNullOrEmpty(password)) { throw new KdfException(ErrorCode.BAD_ARGUMENT, "empty password"); } // random values ( salt, iv ) var salt = kdfParams.salt; var cipherParams = new CipherParams(); // derivedKey -> cipherKey -> cipherText -> mac var derivedKey = PbkdfCrypt.GenerateDerivedScryptKey(password, salt.HexToBytes(), kdfParams.n, kdfParams.r, kdfParams.p, kdfParams.dklen); var cipherKey = PbkdfCrypt.GenerateCipherKey(derivedKey); var cipherText = PbkdfCrypt.GenerateAesCtrCipher(cipherParams.iv.HexToBytes(), cipherKey, key.Bytes); var mac = PbkdfCrypt.GenerateMac(derivedKey, cipherText); return(new KeyStoreV3 <ScryptParams>() { version = Version, id = Guid.NewGuid().ToString(), address = key.Address.HexAddress.ToLower(), crypto = { ciphertext = cipherText.ToHex(), cipherparams = cipherParams, cipher = CIPHER, kdf = KdfType.scrypt.ToString(), kdfparams = kdfParams, mac = mac.ToHex() } }); }