/// <summary> /// Encrypts the key data. /// </summary> /// <param name="key">The key.</param> /// <param name="passwordPrompt">The password prompt.</param> /// <param name="iterationCount">The interation count.</param> /// <returns></returns> public static PbeKeyStore EncryptKeyData(byte[] key, Func <string> passwordPrompt, int iterationCount) { var pks = new PbeKeyStore() { Cipher = PbeKeyType.Aes128, Hmac = PbeHashType.HmacSha1, IterationCount = iterationCount, Salt = new byte[16] }; Secure.Random.NextBytes(pks.Salt); var pbeKey = new PbeAesKey() { Size = 128 }; pbeKey.AesKeyBytes = pks.GetDerivedBytes(pbeKey.Size / 8, passwordPrompt); pks.IV = pbeKey.IV; using (pbeKey) using (var ks = new ImportedKeySet(pbeKey, KeyPurpose.DecryptAndEncrypt, "Pbe key")) using (var crypter = new Crypter(ks)) { var data = crypter.Encrypt(key); byte[] justciphertext = new byte[data.Length - Keyczar.HeaderLength]; Array.Copy(data, Keyczar.HeaderLength, justciphertext, 0, justciphertext.Length); pks.Key = justciphertext; } return(pks); }
/// <summary> /// Encrypts the key data. /// </summary> /// <param name="key">The key.</param> /// <param name="passwordPrompt">The password prompt.</param> /// <param name="iterationCount">The interation count.</param> /// <returns></returns> public static PbeKeyStore EncryptKeyData(byte[] key, Func<string> passwordPrompt, int iterationCount) { var pks = new PbeKeyStore() { Cipher = PbeKeyType.Aes128, Hmac = PbeHashType.HmacSha1, IterationCount = iterationCount, Salt = new byte[16] }; Secure.Random.NextBytes(pks.Salt); var pbeKey = new PbeAesKey() {Size = 128}; pbeKey.AesKeyBytes = pks.GetDerivedBytes(pbeKey.Size/8, passwordPrompt); pks.IV = pbeKey.IV; using (pbeKey) using (var ks = new ImportedKeySet(pbeKey, KeyPurpose.DecryptAndEncrypt, "Pbe key")) using (var crypter = new Crypter(ks)) { var data = crypter.Encrypt(key); byte[] justciphertext = new byte[data.Length - Keyczar.HeaderLength]; Array.Copy(data, Keyczar.HeaderLength, justciphertext, 0, justciphertext.Length); pks.Key = justciphertext; } return pks; }