public static bool CheckPassword(string password, KeyStore keystore) { byte[] derivedkey = new byte[32]; KeyStoreKdfInfo kdf = keystore.Crypto.Kdf; KeyStoreAesInfo aes = keystore.Crypto.Aes; if (!KeyStoreCrypto.EncryptScrypt(password , kdf.Params.N , kdf.Params.R , kdf.Params.P , kdf.Params.Dklen , kdf.Params.Salt , out derivedkey)) { Console.WriteLine("fail to generate scrypt."); return(false); } byte[] iv = aes.Params.Iv; byte[] ciphertext = aes.Text; byte[] mac = keystore.Crypto.Mac; if (!KeyStoreCrypto.VerifyMac(derivedkey, ciphertext, mac)) { Console.WriteLine("Password do not match."); return(false); } return(true); }
public static bool DecryptKeyStore(string password, KeyStore keystore, out byte[] privatekey) { byte[] derivedkey = new byte[32]; privatekey = null; KeyStoreKdfInfo kdf = keystore.Crypto.Kdf; KeyStoreAesInfo aes = keystore.Crypto.Aes; if (!KeyStoreCrypto.EncryptScrypt(password , kdf.Params.N , kdf.Params.R , kdf.Params.P , kdf.Params.Dklen , kdf.Params.Salt , out derivedkey)) { Console.WriteLine("fail to generate scrypt."); return(false); } byte[] iv = aes.Params.Iv; byte[] ciphertext = aes.Text; byte[] mac = keystore.Crypto.Mac; if (!KeyStoreCrypto.VerifyMac(derivedkey, ciphertext, mac)) { Console.WriteLine("Password do not match."); return(false); } byte[] cipherkey = KeyStoreCrypto.GenerateCipherKey(derivedkey); privatekey = new byte[32]; using (var am = new Aes128CounterMode(iv)) using (var ict = am.CreateDecryptor(cipherkey, null)) { ict.TransformBlock(ciphertext, 0, ciphertext.Length, privatekey, 0); } return(true); }