EncryptionShell.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
        }
    }
}