forked from BornToBeRoot/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert-ROT13.ps1
228 lines (190 loc) · 6.76 KB
/
Convert-ROT13.ps1
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Convert-ROT13.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Rotate chars by n places (Caesar cipher)
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Rotate lower and upper chars by n places (Caesar cipher)
.DESCRIPTION
Rotate lower and upper chars by n places (Caesar cipher). By default all 26 options are converted. You can encrypt with the parameter "-Encrypt" or decrypt with the parameter "-Decrypt", depens on what you need. Decryption is selected by default.
.EXAMPLE
.\Convert-ROT13.ps1 -Text "This is an encrypted string!" -Rot 7 -Encrypt
Rot Text
--- ----
7 Aopz pz hu lujyfwalk zaypun!
.EXAMPLE
.\Convert-ROT13.ps1 -Text "Aopz pz hu lujyfwalk zaypun!"
Rot Text
--- ----
1 Znoy oy gt ktixevzkj yzxotm!
2 Ymnx nx fs jshwduyji xywnsl!
3 Xlmw mw er irgvctxih wxvmrk!
4 Wklv lv dq hqfubswhg vwulqj!
5 Vjku ku cp gpetarvgf uvtkpi!
6 Uijt jt bo fodszqufe tusjoh!
7 This is an encrypted string!
8 Sghr hr zm dmbqxosdc rsqhmf!
9 Rfgq gq yl clapwnrcb qrpgle!
10 Qefp fp xk bkzovmqba pqofkd!
11 Pdeo eo wj ajynulpaz opnejc!
12 Ocdn dn vi zixmtkozy nomdib!
13 Nbcm cm uh yhwlsjnyx mnlcha!
14 Mabl bl tg xgvkrimxw lmkbgz!
15 Lzak ak sf wfujqhlwv kljafy!
16 Kyzj zj re vetipgkvu jkizex!
17 Jxyi yi qd udshofjut ijhydw!
18 Iwxh xh pc tcrgneits higxcv!
19 Hvwg wg ob sbqfmdhsr ghfwbu!
20 Guvf vf na rapelcgrq fgevat!
21 Ftue ue mz qzodkbfqp efduzs!
22 Estd td ly pyncjaepo dectyr!
23 Drsc sc kx oxmbizdon cdbsxq!
24 Cqrb rb jw nwlahycnm bcarwp!
25 Bpqa qa iv mvkzgxbml abzqvo!
26 Aopz pz hu lujyfwalk zaypun!
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Script/Convert-ROT13.README.md
#>
[CmdletBinding(DefaultParameterSetName='Decrypt')]
param(
[Parameter(
Position=0,
Mandatory=$true,
HelpMessage='String which you want to encrypt or decrypt')]
[String]$Text,
[Parameter(
Position=1,
HelpMessage='Specify which rotation you want to use (Default=1..26)')]
[ValidateRange(1,26)]
[Int32[]]$Rot=1..26,
[Parameter(
ParameterSetName='Encrypt',
Position=2,
HelpMessage='Encrypt a string')]
[switch]$Encrypt,
[Parameter(
ParameterSetName='Decrypt',
Position=2,
HelpMessage='Decrypt a string')]
[switch]$Decrypt
)
Begin{
[System.Collections.ArrayList]$UpperChars = @()
[System.Collections.ArrayList]$LowerChars = @()
$UpperIndex = 1
$LowerIndex = 1
# Add upper case chars from ascii
foreach($i in 65..90)
{
$Char = [char]$i
[pscustomobject]$Result = @{
Index = $UpperIndex
Char = $Char
}
[void]$UpperChars.Add($Result)
$UpperIndex++
}
# Add lower case chars from ascii
foreach($i in 97..122)
{
$Char = [char]$i
[pscustomobject]$Result = @{
Index = $LowerIndex
Char = $Char
}
[void]$LowerChars.Add($Result)
$LowerIndex++
}
# Default mode is "Decrypt"
if(($Encrypt -eq $false -and $Decrypt -eq $false) -or ($Decrypt))
{
$Mode = "Decrypt"
}
else
{
$Mode = "Encrypt"
}
Write-Verbose -Message "Mode is set to: $Mode"
}
Process{
foreach($Rot2 in $Rot)
{
$ResultText = [String]::Empty
# Go through each char in string
foreach($i in 0..($Text.Length -1))
{
$CurrentChar = $Text.Substring($i, 1)
if($UpperChars.Char -ccontains $CurrentChar) # Upper chars
{
if($Mode -eq "Encrypt")
{
[int]$NewIndex = ($UpperChars | Where-Object {$_.Char -ceq $CurrentChar}).Index + $Rot2
if($NewIndex -gt $UpperChars.Count)
{
$NewIndex -= $UpperChars.Count
$ResultText += ($UpperChars | Where-Object {$_.Index -eq $NewIndex}).Char
}
else
{
$ResultText += ($UpperChars | Where-Object {$_.Index -eq $NewIndex}).Char
}
}
else
{
[int]$NewIndex = ($UpperChars | Where-Object {$_.Char -ceq $CurrentChar}).Index - $Rot2
if($NewIndex -lt 1)
{
$NewIndex += $UpperChars.Count
$ResultText += ($UpperChars | Where-Object {$_.Index -eq $NewIndex}).Char
}
else
{
$ResultText += ($UpperChars | Where-Object {$_.Index -eq $NewIndex}).Char
}
}
}
elseif($LowerChars.Char -ccontains $CurrentChar) # Lower chars
{
if($Mode -eq "Encrypt")
{
[int]$NewIndex = ($LowerChars | Where-Object {$_.Char -ceq $CurrentChar}).Index + $Rot2
if($NewIndex -gt $LowerChars.Count)
{
$NewIndex -= $LowerChars.Count
$ResultText += ($LowerChars | Where-Object {$_.Index -eq $NewIndex}).Char
}
else
{
$ResultText += ($LowerChars | Where-Object {$_.Index -eq $NewIndex}).Char
}
}
else
{
[int]$NewIndex = ($LowerChars | Where-Object {$_.Char -ceq $CurrentChar}).Index - $Rot2
if($NewIndex -lt 1)
{
$NewIndex += $LowerChars.Count
$ResultText += ($LowerChars | Where-Object {$_.Index -eq $NewIndex}).Char
}
else
{
$ResultText += ($LowerChars | Where-Object {$_.Index -eq $NewIndex}).Char
}
}
}
else
{
$ResultText += $CurrentChar
}
}
[pscustomobject] @{
Rot = $Rot2
Text = $ResultText
}
}
}
End{
}