Ejemplo n.º 1
0
        public static void Encrypt(Stream input, Stream output, string password, string filename = null, Action <double> progress = null, Func <bool> isCanceled = null)
        {
            var derivationSettings = PasswordDerivationSettings.Create();
            var secretKey          = Hasher.CreateAesKeyFromPassword(password, derivationSettings.Salt, derivationSettings.Iterations);
            var parameter          = new EncryptInternalParameter
            {
                Filename = filename,
                PasswordDerivationSettings         = derivationSettings,
                EllipticCurveEncryptionInformation = null,
                Progress   = progress,
                IsCanceled = isCanceled,
            };

            EncryptInternal(input, output, secretKey, parameter);
        }
Ejemplo n.º 2
0
        public static void Encrypt(string inputPath, string outputPath, string password, string filename = null, Action <double> progress = null, Func <bool> isCanceled = null)
        {
            var derivationSettings = PasswordDerivationSettings.Create();
            var secretKey          = Hasher.CreateAesKeyFromPassword(password, derivationSettings.Salt, derivationSettings.Iterations);
            var parameter          = new EncryptInternalParameter
            {
                Filename = filename,
                PasswordDerivationSettings         = derivationSettings,
                EllipticCurveEncryptionInformation = null,
                Progress   = progress,
                IsCanceled = isCanceled,
            };

            using (var input = File.OpenRead(inputPath))
                using (var output = File.Open(outputPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    EncryptInternal(input, output, secretKey, parameter);
                }
        }
Ejemplo n.º 3
0
        public static void Encrypt(string inputPath, string outputPath, byte[] secret, string filename = null, Action <double> progress = null, Func <bool> isCanceled = null)
        {
            if (secret.Length < AesKeyLength + HmacKeyLength)
            {
                throw new Exception($"length of secret must be {AesKeyLength + HmacKeyLength} or more.");
            }

            var parameter = new EncryptInternalParameter
            {
                Filename = filename,
                PasswordDerivationSettings         = null,
                EllipticCurveEncryptionInformation = null,
                Progress   = progress,
                IsCanceled = isCanceled,
            };

            using (var input = File.OpenRead(inputPath))
                using (var output = File.Open(outputPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    EncryptInternal(input, output, secret, parameter);
                }
        }
Ejemplo n.º 4
0
        public static void Encrypt(Stream input, Stream output, byte[] secret, string filename = null, Action <double> progress = null, Func <bool> isCanceled = null)
        {
            if (secret.Length < AesKeyLength + HmacKeyLength)
            {
                throw new Exception($"length of secret must be {AesKeyLength + HmacKeyLength} or more.");
            }

            if (!(output.CanRead && output.CanWrite))
            {
                throw new Exception("Strean must support read and write operations");
            }

            var parameter = new EncryptInternalParameter
            {
                Filename = filename,
                PasswordDerivationSettings         = null,
                EllipticCurveEncryptionInformation = null,
                Progress   = progress,
                IsCanceled = isCanceled,
            };

            EncryptInternal(input, output, secret, parameter);
        }
Ejemplo n.º 5
0
        internal static void EncryptInternal(Stream input, Stream output, byte[] secretKey, EncryptInternalParameter parameter = null)
        {
            byte[] secretInformationEncryptedData = null;
            if (parameter?.Filename != null)
            {
                var secretInformation = new SecretInformation
                {
                    Filename = parameter.Filename,
                };

                secretInformationEncryptedData = secretInformation.ToEncyptedData(secretKey);
            }
            var metaInformation = new MetaInformation
            {
                PasswordDerivationSettings         = parameter?.PasswordDerivationSettings,
                SecretInformationEncrypted         = secretInformationEncryptedData,
                EllipticCurveEncryptionInformation = parameter?.EllipticCurveEncryptionInformation,
            };

            RawFileAccessor.Init(output);
            RawFileAccessor.WriteMeta(output, metaInformation);
            RawFileAccessor.SeekToMainData(output);

            var result = EncryptRaw(input, output, secretKey, parameter?.Progress, parameter?.IsCanceled);

            RawFileAccessor.Write(output, result.iv, RawFileAccessor.Field.InitializationVector);
            RawFileAccessor.Write(output, result.hmacHash, RawFileAccessor.Field.Hmac);

            output.Dispose();
        }