Example #1
0
        public static byte[] EncryptText(string inputText, string password)
        {
            ReaderWriterLockSlim locker = new ReaderWriterLockSlim();

            try
            {
                locker.EnterReadLock();
                byte[] encryptedBytes      = System.Text.Encoding.UTF8.GetBytes(inputText);
                byte[] passwordToByteArray = System.Text.Encoding.UTF8.GetBytes(password);
                passwordToByteArray = SHA256.Create().ComputeHash(passwordToByteArray);
                byte[] encryptedByteArray = AES.GetEncryptedByteArray(encryptedBytes, passwordToByteArray);
                return(encryptedByteArray);
            }
            catch (Exception)
            {
                return(null);
            }
            finally
            {
                locker.ExitReadLock();
            }
        }
Example #2
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();
            }
        }