BattleFaction2019.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
function Get-CaesarCipher {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true)]
        [string]$Text,
        [parameter(Mandatory = $true)]
        [int]$SecretNumber,
        [string]$RootString =  $([char[]]$(32..126) -join ''),
        [switch]$Encode,
        [switch]$Decode
    )
    
    begin {
        # If you are decoding, you need to reverse the lookup
        if ($Decode) { $SecretNumber = -$SecretNumber }
        # Modulus is used to keep the values inside array of possible positions
        $Modulus = $RootString.length
        Write-Verbose "Modulus: $Modulus"
        If ($SecretNumber -lt 0) {
            $SecretNumber = $Modulus + ($SecretNumber % $Modulus)
        }
        Write-Verbose $SecretNumber
    }
    
    process {
        # Build 2 hashtables. One for looking a letter based on poition in the string and one for looking up the position in the string based on the letter
        $Counter = @{ }
        $Letter = @{ }
        $i = 0
        [int[]][char[]]$RootString | ForEach-Object { $Counter.add($i, $_); $Letter.Add($_, $i); $i++ }

        # Convert original chars to the char ints matching the secret number
        $Converted = [int[]][char[]]$Text | ForEach-Object {
            try {
                if ($_ -eq 10 -or $_ -eq 13) { $_ } else {
                    $no = $letter[$_]
                    $Counter[$($($no + $SecretNumber) % $Modulus)]
                }
            }
            catch {
                Write-Warning "Error on $_"
            }
        }
    }
    
    end {
        # Just joining the string would remove new line carriage return, formatting a string allows them to expand correctly
        $str = ""
        for ($j = 0; $j -le $Converted.count - 1; $j++) { $str += "{$j}" }
        $str -f $($Converted.foreach{ [char]$_ })
    }
}
function Protect-Message {
    [CmdletBinding()]
    param (
        $Message,
        [securestring]$Password,
        [System.IO.FileInfo]$File
    )
    
    begin {
        $certPath = "$env:Temp\BattleCert.pfx"
        $UserInfo = @{
            User     = $env:USERNAME
            Computer = $env:COMPUTERNAME
            Date     = (Get-Date).ToString()
        } | ConvertTo-Json
    }
    
    process {
        $certName = 'BattleFaction' + ((Get-Random -InputObject (0..9) -count 4) -join '')
        $battleCert = New-SelfSignedCertificate -DnsName $certName -CertStoreLocation "Cert:\CurrentUser\My" -KeyUsage KeyEncipherment, DataEncipherment, KeyAgreement -Type DocumentEncryptionCert
        $Message | Protect-CmsMessage -To $battleCert.Subject -OutFile $File.FullName
        $Exported = $battleCert | Export-PfxCertificate -FilePath $certPath -Password $Password
        $fileContentBytes = get-content $certPath -Encoding Byte
        $certString = [System.Convert]::ToBase64String($fileContentBytes)
        Add-Content -value $certString -Path $File.FullName -Stream Cert
        Add-Content -value $UserInfo -Path $File.FullName -Stream UserInfo


    }
    
    end {
        Get-Childitem -Path Cert:\CurrentUser\My -DocumentEncryptionCert | where { $_.Subject -eq $battleCert.subject } | Remove-Item
        Remove-item $certPath
    }
}
function Unprotect-Message {
    [CmdletBinding()]
    param (
        [System.IO.FileInfo]$MessageFile,
        [SecureString]$Password,
        [switch]$ThisMessageWillSelfDestruct
    )
    
    begin {
        $cert = Get-Content $MessageFile.FullName -Stream Cert
        $userInfo = Get-Content $MessageFile.FullName -Stream UserInfo | convertfrom-json
        $certPath = "$env:Temp\BattleCert.pfx"
    }
    
    process {
        [IO.File]::WriteAllBytes($certPath, [Convert]::FromBase64String($cert))
        $battleCert = Import-PfxCertificate -CertStoreLocation "Cert:\CurrentUser\My" -FilePath $certPath -Password $Password
        $Message = Unprotect-CmsMessage -Path $MessageFile.FullName
        $userMessageString = "Message was generated on {0} by {1} at {2}" -f $userInfo.Computer, $userInfo.User, $userInfo.Date
        Write-Verbose $userMessageString
        $Message
    }
    
    end {
        Get-Childitem -Path Cert:\CurrentUser\My -DocumentEncryptionCert | where { $_.Subject -eq $battleCert.subject } | Remove-Item
        Remove-item $certPath
        if ($ThisMessageWillSelfDestruct){Remove-Item $MessageFile}
    }
}