Obfuscate File Data

If you need to copy a file but don’t want the data contained to be easily read and encryption/3rd party applications aren’t available, this PowerShell solution may help. 

Here I have a file with some data I would like to obfuscate.

Below is the PowerShell steps which:

  1. Prepares some variable to point to the 1.txt file and other files which do not exist
  2.  Here we simply convert the text file to Base64 so it is unreadable to humans and create a file called obfuscatedwithaddeddata
  3.  We add a salt of 6 (this could be anything and anywhere in the file, but used here for simplicity) to the end of the file text. This essentially corrupts the file.
  4.  Here is an attempt to try convert from Base64 to text and it will fail
  5.  Here we remove the 6 (salt removed) and save it as a file called obfuscatedwithoutaddeddata
  6.  Finally we can convert the file obfuscatedwithoutaddeddata to file called 2.txt

All these files were created here so you can review the contents through the process.

#1. files
$source_file = “C:\temp\obfuscate\1.txt”
$obfuscated_file = “C:\temp\obfuscate\obfuscatedwithaddeddata”
$unobfuscated_file = “C:\temp\obfuscate\obfuscatedwithoutaddeddata”
$dest_file = “C:\temp\obfuscate\2.txt”

#2. text -> Base64
[io.file]::WriteAllText($obfuscated_file, [Convert]::ToBase64String([io.file]::ReadAllBytes($source_file)))

#3. obfuscate by adding 6 to end of file
Add-Content -Path $obfuscated_file -Value 6

#4. attempt to reverse
# Base64 -> text
[io.file]::WriteAllBytes($dest_file, [System.Convert]::FromBase64String([io.file]::ReadAllText($obfuscated_file)))

#5. remove 6
$unobfuscated_data = Get-Content $obfuscated_file
$unobfuscated_data = $unobfuscated_data.Substring(0,$unobfuscated_data.Length-1)
Set-Content -Path $unobfuscated_file -Value $unobfuscated_data

#6. Base64 -> text
[io.file]::WriteAllBytes($dest_file, [System.Convert]::FromBase64String([io.file]::ReadAllText($unobfuscated_file)))

Leave a Reply