Ejemplo n.º 1
0
        public static string EncryptFile(string inputFile, string outputFile, string password, bool base64Encoding = false, bool localOutput = false)
        {
            ReaderWriterLockSlim locker = new ReaderWriterLockSlim();

            try
            {
                locker.EnterReadLock();
                byte[] encryptedBytes      = File.ReadAllBytes(inputFile);
                byte[] passwordToByteArray = System.Text.Encoding.ASCII.GetBytes(password);
                //hash the password with sha256
                passwordToByteArray = SHA256.Create().ComputeHash(passwordToByteArray);
                byte[] encryptedByteArray = AES.GetEncryptedByteArray(encryptedBytes, passwordToByteArray);
                string writeAt            = !string.IsNullOrEmpty(outputFile) ? outputFile : inputFile;

                if (base64Encoding)
                {
                    string base64 = System.Convert.ToBase64String(encryptedByteArray, Base64FormattingOptions.None);
                    File.WriteAllText(writeAt, base64, Encoding.UTF8);
                    string encryptoSettingsFileName = "encrypto.settings";

                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        string encryptoSettingsPath = string.Empty;
                        if (localOutput)
                        {
                            encryptoSettingsPath = Path.Combine(Path.GetDirectoryName(inputFile), encryptoSettingsFileName);
                        }
                        else
                        {
                            var encryptoPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "encrypto");
                            if (!Directory.Exists(encryptoPath))
                            {
                                Directory.CreateDirectory(encryptoPath);
                            }
                            encryptoSettingsPath = Path.Combine(encryptoPath, encryptoSettingsFileName);
                        }

                        MappingModel mapping = new MappingModel();
                        mapping.original  = inputFile;
                        mapping.encrypted = outputFile;
                        string content = JsonConvert.SerializeObject(mapping);
                        File.WriteAllText(encryptoSettingsPath, content); // %AppData%/encrypto/encrypto.settings
                    }

                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                    {
                        string encryptoSettingsPath = string.Empty;
                        if (localOutput)
                        {
                            encryptoSettingsPath = Path.Combine(Path.GetDirectoryName(inputFile), encryptoSettingsFileName);
                        }
                        else
                        {
                            var encryptoPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".encrypto");
                            if (!Directory.Exists(encryptoPath))
                            {
                                Directory.CreateDirectory(encryptoPath);
                            }
                            encryptoSettingsPath = Path.Combine(encryptoPath, encryptoSettingsFileName);
                        }

                        MappingModel mapping = new MappingModel();
                        mapping.original  = inputFile;
                        mapping.encrypted = outputFile;
                        string content = JsonConvert.SerializeObject(mapping);
                        File.WriteAllText(encryptoSettingsPath, content); // ~/.encrypto/encrypto.settings
                    }
                }
                else
                {
                    File.WriteAllBytes(writeAt, encryptedByteArray);
                }
                return("encryption succeeded");
            }
            catch (Exception)
            {
                return("encryption failed");
            }
            finally
            {
                locker.ExitReadLock();
            }
        }
Ejemplo n.º 2
0
        public static string DecryptFile(string inputFile, string outputFile, string password, bool base64Decoding = false)
        {
            ReaderWriterLockSlim locker = new ReaderWriterLockSlim();

            try
            {
                locker.EnterReadLock();
                byte[] bytesToBeDecrypted = File.ReadAllBytes(inputFile);
                byte[] passwordBytes      = System.Text.Encoding.ASCII.GetBytes(password);
                passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
                string writeAt = !string.IsNullOrEmpty(outputFile) ? outputFile : inputFile;

                if (base64Decoding)
                {
                    MappingModel mapping = new MappingModel();
                    if (string.IsNullOrEmpty(outputFile))
                    {
                        string encryptoSettingsFileName = "encrypto.settings";

                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            var encryptolocalPath = Path.Combine(Path.GetDirectoryName(inputFile), encryptoSettingsFileName);
                            var encryptoPath      = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "encrypto");

                            if (File.Exists(encryptolocalPath))
                            {
                                string content = File.ReadAllText(encryptolocalPath);
                                mapping = JsonConvert.DeserializeObject <MappingModel>(content);
                            }
                            else
                            {
                                if (Directory.Exists(encryptoPath))
                                {
                                    var    encryptoSettingsPath = Path.Combine(encryptoPath, encryptoSettingsFileName);
                                    string content = File.ReadAllText(encryptoSettingsPath);
                                    mapping = JsonConvert.DeserializeObject <MappingModel>(content);
                                }
                            }
                        }

                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                        {
                            var encryptolocalPath = Path.Combine(Path.GetDirectoryName(inputFile), encryptoSettingsFileName); // localFile
                            var encryptoPath      = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".encrypto");

                            if (File.Exists(encryptolocalPath))
                            {
                                string contentMapping = File.ReadAllText(encryptolocalPath);
                                mapping = JsonConvert.DeserializeObject <MappingModel>(contentMapping);
                            }
                            else
                            {
                                if (Directory.Exists(encryptoPath))
                                {
                                    var    encryptoSettings = Path.Combine(encryptoPath, encryptoSettingsFileName);
                                    string contentMapping   = File.ReadAllText(encryptoSettings);
                                    mapping = JsonConvert.DeserializeObject <MappingModel>(contentMapping);
                                }
                            }
                        }
                    }

                    string base64Content = File.ReadAllText(inputFile, Encoding.UTF8);
                    if (!isValidBase64(base64Content))
                    {
                        System.Console.WriteLine("Error: Invalid base64");
                        return("Decryption failed");
                    }
                    else
                    {
                        byte[] result         = System.Convert.FromBase64String(base64Content);
                        byte[] bytesDecrypted = AES.GetDecryptedByteArray(result, passwordBytes);
                        writeAt = string.IsNullOrEmpty(mapping.original) ? writeAt : mapping.original;
                        File.WriteAllBytes(writeAt, bytesDecrypted);
                    }
                }
                else
                {
                    byte[] bytesDecrypted = AES.GetDecryptedByteArray(bytesToBeDecrypted, passwordBytes);
                    File.WriteAllBytes(writeAt, bytesDecrypted);
                }
                return("Decryption succeeded");
            }
            catch (Exception)
            {
                return("Decryption failed");
            }
            finally
            {
                locker.ExitReadLock();
            }
        }