EncryptionShell.psm1

function New-ESKey {
    Process {
        $random = -join ((65..90) + (97..122) | Get-Random -Count 32 | % {[char]$_})
        $encoding = New-Object System.Text.ASCIIEncoding
        $bytes = $encoding.GetBytes($random)
        return $bytes
    }
}

function Get-ESHash {
    <#
    .SYNOPSIS
        Gets the MD5 hash of a string.
 
    .DESCRIPTION
        Gets the MD5 hash of a string.
 
    .PARAMETER Data
        Data to be MD5 hashed.
 
    #>

    [cmdletbinding()]
    Param (
        [Parameter(mandatory=$true)][string]$Data
    )    
    Process {
        try {
            $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
            $utf8 = New-Object -TypeName System.Text.UTF8Encoding
            $hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($data)))
            return $hash
        } catch {
            throw $_.exception.message
        }
    }
}

function Write-ESData {
    <#
    .SYNOPSIS
        Encrypts data using a passphrase.
 
    .DESCRIPTION
        Encrypts data using a passphrase. The longer and more complex you make the passphrase the better the encryption.
 
    .PARAMETER Data
        Data to be encrypted.
 
    .PARAMETER Key
        Passphrase used to encrypt the data.
     
    .EXAMPLE
        Write-ESData -Data "Encrypt me" -Key "Passphrase"
 
        This will provide a result like this -
        76492d1116743f0423413b16050a5345MgB8AGIAawBxADIAVQBOADUAegBTAGwANgByAGwAQQBrAHAATwBKAFgAMwAvAFEAPQA9AHwAZAAzAGIANwA0ADEAYgAxAGYANQA2ADcANQA5AGIAMwAwADQAYgA5ADQAYgA3ADAAZgBkADUANAA1AGMAMgBlADQAZABmAGMAZQBlADMANwAzADEAMQAzADUAOABhAGMANAA5AGEAOQA5ADUAZQBmAGIAOQAwADIAZQBmADUAMQA=
 
    .EXAMPLE
        Write-ESData -Data (get-content .\test.txt -raw) -Key $Passphrase
 
        This will encrypt the text file content of test.txt.
 
        Please note that the -raw parameter was required in order to maintain the format of "string". Get-Content will automatically split the contents of a file into an array.
 
    #>

    [cmdletbinding()]
    Param (
        [Parameter(mandatory=$true)][string]$Data,
        [Parameter(mandatory=$true)][string]$Key
    )    
    Process {
        try {
            $keydata = $null; $keydata = [Byte[]]($Key.PadRight(24).Substring(0,24).ToCharArray())
            $encrypted = $null; $encrypted = $data | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString -Key $keydata
            return $encrypted
        } catch {
            throw $_.exception.message
        }
    }
}

function Read-ESData {
    <#
    .SYNOPSIS
        Decrypts data encrypted using the Write-ESData cmdlet.
 
    .DESCRIPTION
        Decrypts data encrypted using the Write-ESData cmdlet.
 
    .PARAMETER Data
        Data to be decrypted.
 
    .PARAMETER Key
        Passphrase used to decrypt the data.
     
    .EXAMPLE
        Read-ESData -Data $EncryptedData -Key $Passphrase
 
    #>

    [cmdletbinding()]
    Param (
        [Parameter(mandatory=$true,valuefrompipelinebypropertyname=$true)][string]$Data,
        [Parameter(mandatory=$true,valuefrompipelinebypropertyname=$true)][string]$Key
    )    
    Process {    
        try {  
            $keydata = $null; $keydata = [Byte[]]($Key.PadRight(24).Substring(0,24).ToCharArray())
            $decrypted = $null; $decrypted = ConvertTo-SecureString -string $Data -Key $keydata -erroraction stop
            $decrypted =  [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($decrypted))
            return $decrypted
        } catch {
            throw $_.exception.message
        }
    }
}