Exemple #1
0
        /// <summary>
        /// Implements the PBKDF1 key derivation function, as defined in §5.1, RFC 2898 (PKCS#5).
        /// </summary>
        /// <param name="Password">Password</param>
        /// <param name="Iterations">Number of iterations</param>
        /// <param name="KeyLength">Length of generated keys.</param>
        /// <param name="HashFunction">Hash function.</param>
        public Pbkdf1(string Password, int Iterations, int KeyLength, HashFunction HashFunction)
            : base(Password)
        {
            if (Iterations <= 0)
            {
                throw new ArgumentException("Must be postitive.", nameof(Iterations));
            }

            if (KeyLength <= 0)
            {
                throw new ArgumentException("Must be postitive.", nameof(Iterations));
            }

            this.iterations = Iterations;
            this.salt       = PfxEncoder.GetRandomBytes(8);

            byte[] Bin = Hashes.ComputeHash(HashFunction,
                                            Primitives.CONCAT(Encoding.UTF8.GetBytes(Password), this.salt));

            while (--Iterations >= 0)
            {
                Bin = Hashes.ComputeHash(HashFunction, Bin);
            }

            if (KeyLength > Bin.Length)
            {
                throw new ArgumentException("Derived key too long.", nameof(KeyLength));
            }

            this.key = new byte[KeyLength];

            Array.Copy(Bin, 0, this.key, 0, KeyLength);
        }
		/// <summary>
		/// Encrypts data.
		/// </summary>
		/// <param name="PlainText">Data to encrypt.</param>
		/// <returns>Encrypted data.</returns>
		public override byte[] Encrypt(byte[] PlainText)
		{
			using (TripleDES C = TripleDES.Create())
			{
				C.Key = this.Key;
				C.Mode = CipherMode.CBC;
				C.Padding = PaddingMode.PKCS7;

				using (ICryptoTransform E = C.CreateEncryptor())
				{
					C.IV = PfxEncoder.PRF(HashFunction.SHA1, this.Iterations,
						PfxEncoder.FormatPassword(Password), this.Salt, 64, 2);

					return E.TransformFinalBlock(PlainText, 0, PlainText.Length);
				}
			}
		}
Exemple #3
0
        /// <summary>
        /// Implements a password-based encryption algorithm, as defined in §C, RFC 7292 (PKCS#12).
        /// </summary>
        /// <param name="Password">Password</param>
        /// <param name="Iterations">Number of iterations</param>
        /// <param name="KeyLength">Length of generated keys.</param>
        /// <param name="HashFunction">Hash function.</param>
        public PbePkcs12(string Password, int Iterations, int KeyLength, HashFunction HashFunction)
            : base(Password)
        {
            if (Iterations <= 0)
            {
                throw new ArgumentException("Must be postitive.", nameof(Iterations));
            }

            if (KeyLength <= 0)
            {
                throw new ArgumentException("Must be postitive.", nameof(Iterations));
            }

            this.iterations = Iterations;
            this.salt       = PfxEncoder.GetRandomBytes(8);

            this.key = PfxEncoder.PRF(HashFunction, Iterations,
                                      PfxEncoder.FormatPassword(Password), this.salt, KeyLength, 1);
        }
Exemple #4
0
 public void TestInitialize()
 {
     this.pfxOutput = new PfxEncoder();
 }