public static ECKeyPair Decrypt(String password, WalletFile walletFile) { Validate(walletFile); var crypto = walletFile.Crypto; var mac = ByteUtils.ToByteArray(crypto.Mac); var iv = ByteUtils.ToByteArray(crypto.Cipherparams.IV); var cipherText = ByteUtils.ToByteArray(crypto.Ciphertext); byte[] derivedKey; var kdfParams = crypto.Kdfparams; if (kdfParams is ScryptKdfParams) { var scryptKdfParams = (ScryptKdfParams)crypto.Kdfparams; int dklen = scryptKdfParams.Dklen; int n = scryptKdfParams.N; int p = scryptKdfParams.P; int r = scryptKdfParams.R; var salt = ByteUtils.ToByteArray(scryptKdfParams.Salt); derivedKey = GenerateDerivedScryptKey(Encoding.UTF8.GetBytes(password), salt, n, r, p, dklen); } else if (kdfParams is Aes128CtrKdfParams) { var aes128CtrKdfParams = (Aes128CtrKdfParams)crypto.Kdfparams; int c = aes128CtrKdfParams.C; String prf = aes128CtrKdfParams.Prf; var salt = ByteUtils.ToByteArray(aes128CtrKdfParams.Salt); derivedKey = GenerateAes128CtrDerivedKey(Encoding.UTF8.GetBytes(password), salt, c, prf); } else { throw new CipherException("Unable to deserialize params: " + crypto.Kdf); } var derivedMac = GenerateMac(derivedKey, cipherText); if (!Arrays.Equals(derivedMac, mac)) { throw new CipherException("Invalid password provided"); } var encryptKey = Arrays.CopyOfRange(derivedKey, 0, 16); var privateKey = PerformCipherOperation(false, iv, encryptKey, cipherText); return(ECKeyPair.Create(privateKey)); }
public static WalletFile CreateWalletFile( ECKeyPair ecKeyPair, byte[] cipherText, byte[] iv, byte[] salt, byte[] mac, int n, int p) { var walletFile = new WalletFile { Address = ecKeyPair.GetHexAddress() }; var crypto = new Crypto { Cipher = CIPHER, Ciphertext = ByteUtils.ToHexString(cipherText, null) }; walletFile.Crypto = crypto; var cipherParams = new CipherParams { IV = ByteUtils.ToHexString(iv, null) }; crypto.Cipherparams = cipherParams; crypto.Kdf = SCRYPT; var kdfParams = new ScryptKdfParams { Dklen = DKLEN, N = n, P = p, R = R, Salt = ByteUtils.ToHexString(salt, null) }; crypto.Kdfparams = kdfParams; crypto.Mac = ByteUtils.ToHexString(mac, null); walletFile.Crypto = crypto; walletFile.Id = Guid.NewGuid().ToString(); walletFile.Version = CURRENT_VERSION; return(walletFile); }
static void Validate(WalletFile walletFile) { var crypto = walletFile.Crypto; if (walletFile.Version != CURRENT_VERSION) { throw new CipherException("Wallet version is not supported"); } if (!crypto.Cipher.Equals(CIPHER)) { throw new CipherException("Wallet cipher is not supported"); } if (!crypto.Kdf.Equals(AES_128_CTR) && !crypto.Kdf.Equals(SCRYPT)) { throw new CipherException("KDF type is not supported"); } }
public WalletInfo(WalletFile walletFile, ECKeyPair keyPair) { this.WalletFile = walletFile; this.KeyPair = keyPair; }