public static string DecryptText(byte[] input, string password) { ReaderWriterLockSlim locker = new ReaderWriterLockSlim(); try { locker.EnterReadLock(); byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password); passwordBytes = SHA256.Create().ComputeHash(passwordBytes); byte[] bytesDecrypted = AES.GetDecryptedByteArray(input, passwordBytes); var result = System.Text.Encoding.UTF8.GetString(bytesDecrypted); return(result); } catch (Exception) { return("Decryption failed"); } finally { locker.ExitReadLock(); } }
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(); } }