public static Some<Tuple<string, long>> EncryptFile(string dstDir, EncryptOperation oper, byte[] key)
        {
            if (!File.Exists(oper.FileName)) return ErrorValues[ErrorType.FileNoExist];

            var outputFileName = Path.Combine(dstDir, oper.EncFileName);
            var outputDir = Path.GetDirectoryName(outputFileName);
            if (string.IsNullOrWhiteSpace(outputDir)) return ErrorValues[ErrorType.OutDirMissing];
            Directory.CreateDirectory(outputDir);

            using (var aes = new AuthenticatedAesCng())
            {
                aes.CngMode = CngChainingMode.Gcm;
                aes.Key = key;
                aes.GenerateIV();

                byte[] tag;

                using (var outputFile = File.OpenWrite(outputFileName))
                {
                    outputFile.Write(OuterMagicHeader.ToArray());
                    outputFile.Write(BitConverter.GetBytes(aes.Tag.Length));
                    outputFile.Write(BitConverter.GetBytes(aes.IV.Length));
                    outputFile.Write(aes.Tag);
                    outputFile.Write(aes.IV);

                    using (var enc = aes.CreateEncryptor())
                    {
                        using (var cryptoStream = new CryptoStream(outputFile, enc, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(MagicHeader.ToArray());
                            var fileName = Encoding.UTF8.GetBytes(oper.RelFileName);
                            var fileNameSize = BitConverter.GetBytes(Convert.ToInt32(fileName.Length));
                            cryptoStream.Write(fileNameSize);
                            cryptoStream.Write(fileName);

                            using (var inputFile = File.OpenRead(oper.FileName))
                            {
                                inputFile.CopyTo(cryptoStream);
                            }
                        }

                        tag = ((IAuthenticatedCryptoTransform)enc).GetTag();
                    }
                }

                using (var outputFile = File.OpenWrite(outputFileName))
                {
                    outputFile.Seek(TagStartPos, SeekOrigin.Begin);
                    outputFile.Write(tag, 0, tag.Length);
                }

                return new Some<Tuple<string, long>>(Tuple.Create(outputFileName, new FileInfo(oper.FileName).Length));
            }
        }
Example #2
0
        public long PerformEncryption(CommonOptions options, IOutputHandler outputHandler, EncryptOperation o, byte[] key)
        {
            outputHandler.WriteVerbose("\t{0} => ", o.RelFileName);

            var result = FileEncrypter.EncryptFile(options.DestinationDir, o, key);

            if (result.HasResult)
            {
                outputHandler.WriteVerboseLine(o.EncFileName);
                return(result.Result.Item2);
            }

            outputHandler.WriteVerboseOrNormalLine("\t{0} => Failed to encrypt file! ({1})", "Failed to encrypt file! ({1})", o.RelFileName, result.ErrorMessage);
            return(0);
        }
        public static Some <Tuple <string, long> > EncryptFile(string dstDir, EncryptOperation oper, byte[] key)
        {
            if (!File.Exists(oper.FileName))
            {
                return(ErrorValues[ErrorType.FileNoExist]);
            }

            var outputFileName = Path.Combine(dstDir, oper.EncFileName);
            var outputDir      = Path.GetDirectoryName(outputFileName);

            if (string.IsNullOrWhiteSpace(outputDir))
            {
                return(ErrorValues[ErrorType.OutDirMissing]);
            }
            Directory.CreateDirectory(outputDir);

            using (var aes = new AuthenticatedAesCng())
            {
                aes.CngMode = CngChainingMode.Gcm;
                aes.Key     = key;
                aes.GenerateIV();

                byte[] tag;

                using (var outputFile = File.OpenWrite(outputFileName))
                {
                    outputFile.Write(OuterMagicHeader.ToArray());
                    outputFile.Write(BitConverter.GetBytes(aes.Tag.Length));
                    outputFile.Write(BitConverter.GetBytes(aes.IV.Length));
                    outputFile.Write(aes.Tag);
                    outputFile.Write(aes.IV);

                    using (var enc = aes.CreateEncryptor())
                    {
                        using (var cryptoStream = new CryptoStream(outputFile, enc, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(MagicHeader.ToArray());
                            var fileName     = Encoding.UTF8.GetBytes(oper.RelFileName);
                            var fileNameSize = BitConverter.GetBytes(Convert.ToInt32(fileName.Length));
                            cryptoStream.Write(fileNameSize);
                            cryptoStream.Write(fileName);

                            using (var inputFile = File.OpenRead(oper.FileName))
                            {
                                inputFile.CopyTo(cryptoStream);
                            }
                        }

                        tag = ((IAuthenticatedCryptoTransform)enc).GetTag();
                    }
                }

                using (var outputFile = File.OpenWrite(outputFileName))
                {
                    outputFile.Seek(TagStartPos, SeekOrigin.Begin);
                    outputFile.Write(tag, 0, tag.Length);
                }

                return(new Some <Tuple <string, long> >(Tuple.Create(outputFileName, new FileInfo(oper.FileName).Length)));
            }
        }