Exemplo n.º 1
0
        /// <summary>
        /// Decrypts the specified file.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="password"></param>
        /// <returns>
        /// <c>true</c> on success; <c>false</c> if the decryption failed.
        /// </returns>
        static byte[] DecryptFile(string filePath, string password)
        {
            var bufferSize = (int)new FileInfo(filePath).Length + 64 * 1024;

            using (var inputStream = File.OpenRead(filePath))
            {
                var outputStream = new MemoryStream(bufferSize);

                StreamCryptoUtils.DecryptStream(inputStream, outputStream, password);

                return(outputStream.ToArray());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Encrypts the specified file.
        /// </summary>
        /// <param name="filePath">Path to the file to encrypt.</param>
        /// <param name="loaderPath">Path to the loader application.</param>
        /// <returns>
        /// <c>true</c> if the file has been encrypted; <c>false</c> if the file is not a .NET assembly or otherwise, cannot be encrypted.
        /// </returns>
        static bool EncryptFile(string filePath, string loaderPath, string password)
        {
            if (false == IsValidAssembly(filePath))
            {
                return(false);
            }

            var fileType = GetFileType(filePath);

            string outputExtension;

            switch (fileType)
            {
            default:
            case ExecutableFileType.None:
                return(false);

            case ExecutableFileType.Program:
                outputExtension = SharedConstants.EncryptedExeFileExtension;
                break;

            case ExecutableFileType.Library:
                outputExtension = SharedConstants.EncryptedLibFileExtension;
                break;
            }

            var outputFilePath = Path.ChangeExtension(filePath, outputExtension);

            using (var inputStream = File.OpenRead(filePath))
            {
                using (var outputStream = File.Create(outputFilePath))
                {
                    StreamCryptoUtils.EncryptStream(inputStream, outputStream, password);
                }
            }

            // Remove the original file
            File.Delete(filePath);

            // Replace the original file with the loader if it is an .exe
            if (fileType == ExecutableFileType.Program)
            {
                File.Copy(loaderPath, filePath);
            }

            return(true);
        }
Exemplo n.º 3
0
        static int Main(string[] args)
        {
            PrintHeader();

            if (args.Length < 3 || args.Length > 4)
            {
                Help();
                return(1);
            }

            if (IsHelpArgument(args[0]))
            {
                // Display the help screen
                Help();
                return(0);
            }

            var command    = args[0];
            var inputPath  = args[1];
            var outputPath = args[2];

            var encrypt = command.Equals("-e", StringComparison.OrdinalIgnoreCase);
            var decrypt = command.Equals("-d", StringComparison.OrdinalIgnoreCase);

            if (encrypt || decrypt)
            {
                // Valid command
            }
            else
            {
                PrintError("Invalid command: '{0}'.", command);
                return(1);
            }

            if (false == File.Exists(inputPath))
            {
                PrintError("File not found: '{0}'.", inputPath);
                return(1);
            }

            string password;

            if (args.Length > 3)
            {
                password = args[3];
            }
            else
            {
                password = ReadPassword("Password: "******"Password cannot be empty.");
                return(1);
            }

            try
            {
                using (var inputStream = File.OpenRead(inputPath))
                {
                    using (var outputStream = File.Create(outputPath))
                    {
                        try
                        {
                            if (encrypt)
                            {
                                StreamCryptoUtils.EncryptStream(inputStream, outputStream, password);

                                Console.WriteLine("File encrypted.");
                            }
                            else
                            {
                                StreamCryptoUtils.DecryptStream(inputStream, outputStream, password);

                                Console.WriteLine("File decrypted.");
                            }
                        }
                        catch (StreamCryptoException exc)
                        {
                            string message;
                            switch (exc.Error)
                            {
                            case StreamCryptoError.DecryptionError:
                                message = "Wrong password or file has been tampered with. Decryption error: " + exc.Message;
                                break;

                            case StreamCryptoError.IntegrityCheckFailed:
                                message = "Wrong password or file has been tampered with. " + exc.Message;
                                break;

                            case StreamCryptoError.NotEncrypted:
                                message = "The file is not encrypted.";
                                break;

                            default:
                                message = exc.Message;
                                break;
                            }

                            PrintError(message);
                            return(1);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                PrintError(exc.Message);
                return(1);
            }

            return(0);
        }