Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Decrypts the key data.
        /// </summary>
        /// <param name="passwordPrompt">The passsword prompt.</param>
        /// <returns></returns>
        public byte[] DecryptKeyData(Func <string> passwordPrompt)
        {
            var key = new PbeAesKey {
                IV = IV
            };

            if (Cipher == PbeKeyType.Aes128)
            {
                key.Size = 128;
            }
            else
            {
                throw new InvalidKeySetException("Unknown Pbe Cipher");
            }

            key.AesKeyBytes = GetDerivedBytes(key.Size / 8, passwordPrompt);

            using (key)
                using (var ks = new ImportedKeySet(key, KeyPurpose.DecryptAndEncrypt, "Pbe key"))
                    using (var crypter = new Crypter(ks))
                        using (var memstream = new MemoryStream())
                        {
                            memstream.Write(Keyczar.FormatBytes, 0, Keyczar.FormatBytes.Length);
                            memstream.Write(new byte[Keyczar.KeyHashLength], 0, Keyczar.KeyHashLength);
                            memstream.Write(Key, 0, Key.Length);
                            return(crypter.Decrypt(memstream.ToArray()));
                        }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Decrypts the key data.
        /// </summary>
        /// <param name="passwordPrompt">The passsword prompt.</param>
        /// <returns></returns>
        public byte[] DecryptKeyData(Func<string> passwordPrompt)
        {
            var key = new PbeAesKey {IV = IV};

            if (Cipher == PbeKeyType.Aes128)
            {
                key.Size = 128;
            }
            else
            {
                throw new InvalidKeySetException("Unknown Pbe Cipher");
            }

            key.AesKeyBytes = GetDerivedBytes(key.Size/8, passwordPrompt);

            using (key)
            using (var ks = new ImportedKeySet(key, KeyPurpose.DecryptAndEncrypt, "Pbe key"))
            using (var crypter = new Crypter(ks))
            using (var memstream = new MemoryStream())
            {
                memstream.Write(Keyczar.FormatBytes, 0, Keyczar.FormatBytes.Length);
                memstream.Write(new byte[Keyczar.KeyHashLength], 0, Keyczar.KeyHashLength);
                memstream.Write(Key, 0, Key.Length);
                return crypter.Decrypt(memstream.ToArray());
            }
        }
Ejemplo n.º 4
0
        /// <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;
        }