Пример #1
0
        /// <summary>
        /// Encrypt with password
        /// </summary>
        /// <param name="input">Input stream</param>
        /// <param name="output">Output stream</param>
        /// <param name="password">Password</param>
        /// <param name="notifyProgression">Notify progression method</param>
        public static void EncryptWithPassword(Stream input, Stream output, string password, Action <int> notifyProgression = null)
        {
            byte[] salt = RandomHelper.GenerateBytes(16);
            byte[] key  = PBKDF2.GenerateKeyFromPassword(AES.KEY_SIZE, password, salt);
            byte[] iv   = RandomHelper.GenerateBytes(AES.IV_SIZE);

            BinaryHelper.WriteString(output, "ENCP!", Encoding.ASCII);
            BinaryHelper.WriteByte(output, _version);
            BinaryHelper.WriteByte(output, (byte)iv.Length);
            BinaryHelper.WriteByte(output, (byte)salt.Length);
            BinaryHelper.WriteBytes(output, iv);
            BinaryHelper.WriteBytes(output, salt);

            AES.EncryptCBC(input, output, key, iv, _paddingStyle, notifyProgression);
        }
Пример #2
0
        /// <summary>
        /// Decrypt with password
        /// </summary>
        /// <param name="input">Input stream</param>
        /// <param name="output">Output stream</param>
        /// <param name="password">Password</param>
        /// <param name="notifyProgression">Notify progression method</param>
        public static void DecryptWithPassword(Stream input, Stream output, string password, Action <int> notifyProgression = null)
        {
            input.Seek(5, SeekOrigin.Current); // Header
            input.Seek(1, SeekOrigin.Current); // Version

            byte ivLength   = BinaryHelper.ReadByte(input);
            byte saltLength = BinaryHelper.ReadByte(input);

            byte[] iv   = BinaryHelper.ReadBytes(input, ivLength);
            byte[] salt = BinaryHelper.ReadBytes(input, saltLength);

            if (notifyProgression != null)
            {
                notifyProgression(5 + 1 + 1 + 1 + ivLength + saltLength);
            }

            byte[] key = PBKDF2.GenerateKeyFromPassword(AES.KEY_SIZE, password, salt);

            AES.DecryptCBC(input, output, key, iv, _paddingStyle, notifyProgression);
        }