Tuesday, 13 August 2013

Powershell Encrypt/Decrypt script

Here is a simple script to encrypt the content of a text file. It's not that hard to break the encryption but it's useful if you don't want anyone to be able to read its contents.

$file = Get-Content C:\file.txt 

foreach($_ in $file){

   for($i=0; $i -lt $_.length;$i++){

   $encrypted = $encrypted + [char]([int][char]($_.substring($i,1)) - 21) 
                                   }
$encrypted | out-file C:\encryptedFile.txt -Encoding ASCII -append
$encrypted = $null 
}

And the following is the code to decrypt it.

$file = Get-Content C:\encryptedFile.txt
foreach($_ in $file){
  
    for($i=0; $i -le $_.length - 1;$i++){
    
    $encrypted = $encrypted + [char]([int][char]($_.substring($i,1)) + 21)
                                        } 

-join ($encrypted[0..$encrypted.Length]) $encrypted = $null 
 }

No comments:

Post a Comment