/// <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);
				}
			}
		}
예제 #2
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);
        }