Imported Keyset, can be used for compatiblity with keys stored as PEM,DER or X509 certificates
Inheritance: IKeySet, IDisposable
Ejemplo n.º 1
0
        public void AESTest(
            [Values(2048)] int datasize,
            [Values(128, 192, 256)] int keysize,
            [Values("AES", "STDNET40_AES", "C#_AES_AEAD")] string alg
            )
        {
            KeyType type = alg;
            var key = Key.Generate(type, keysize);
            using (var ks = new ImportedKeySet(key, KeyPurpose.DecryptAndEncrypt, "Test"))
            using (var crypter = new Crypter(ks))
            {
                var watchEncrypt = new System.Diagnostics.Stopwatch();
                var watchDecrypt = new System.Diagnostics.Stopwatch();
                for (int i = 0; i < iterations; i++)
                {
                    var input = new byte[datasize];

                    watchEncrypt.Start();
                    var output = crypter.Encrypt(input);
                    watchEncrypt.Stop();

                    watchDecrypt.Start();
                    var result = crypter.Decrypt(output);
                    watchDecrypt.Stop();

                    Expect(result, Is.EqualTo(input));
                }

                Console.WriteLine(String.Format("{3}-{4},{2}\t\tEncryption Total:{0},\tThroughput:{1:#,##0.00} MB/S",
                                                watchEncrypt.Elapsed,
                                                (datasize*iterations*1000m)/
                                                (1024m*1024m*watchEncrypt.ElapsedMilliseconds),
                                                datasize,
                                                alg,
                                                keysize
                                      ));
                Console.WriteLine(String.Format("{3}-{4},{2}\t\tDecryption Total:{0},\tThroughput:{1:#,##0.00} MB/S",
                                                watchDecrypt.Elapsed,
                                                (datasize*iterations*1000m)/
                                                (1024m*1024m*watchDecrypt.ElapsedMilliseconds),
                                                datasize,
                                                alg,
                                                keysize
                                      ));
            }
        }
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>
        /// 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;
        }