test/KidRSA.Tests.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
Import-Module -Name $PSScriptRoot\..\KidRSA.psm1 -Verbose -Force

<#
AlicesKeys
VERBOSE: The value for 'a' is: 67
VERBOSE: The value for 'b' is: 63
VERBOSE: The value for 'a_' is: 2
VERBOSE: The value for 'b_' is: 3
VERBOSE: The value of Private key 'e' (encrypt) is: 8507
VERBOSE: The value of Public key 'd' (decrypt) is: 12723
VERBOSE: The value of 'n' is: 25648
#>

$script:AlicesKeys = $(Get-RSAKey -a 67 -b 63 -a_ 2 -b_ 3)

<#
BobsKeys
VERBOSE: The value for 'a' is: 80
VERBOSE: The value for 'b' is: 45
VERBOSE: The value for 'a_' is: 95
VERBOSE: The value for 'b_' is: 69
VERBOSE: The value of Private key 'e' (encrypt) is: 341985
VERBOSE: The value of Public key 'd' (decrypt) is: 248376
VERBOSE: The value of 'n' is: 23601241
#>

$script:BobsKeys = $(Get-RSAKey -a 80 -b 45 -a_ 95 -b_ 69)

Describe "Test asymetric key generation" {
    Context "Invoke Get-RSAKey" {
        It "Should auto-generate keys" {
            $Results = Get-RSAKey
            $Results.GetType().Name | Should -Be 'AsymmetricKeys'
            $Results.e | Should -BeOfType Int64
            $Results.d | Should -BeOfType Int64
            $Results.n | Should -BeOfType Int64
        }
        It "Should return an encryption key of '<e>' and a decryption key of '<d>'." -TestCases @(@{
                a  = 9
                b  = 11
                a_ = 5
                b_ = 8
                e  = 499
                d  = 795
                n  = 4048
            }) {
            Param($a, $b, $a_, $b_, $e, $d, $n)
            $Results = Get-RSAKey $a $b $a_ $b_
            $Results.GetType().Name | Should -Be 'AsymmetricKeys'
            $Results.e | Should -Be $e
            $Results.d | Should -Be $d
            $Results.n | Should -Be $n
        }
    }
}

Describe "Test Encoding and Decoding" {
    Context "Invoke ConvertTo-CipherText and ConvertTo-PlainText" {
        It "Should encode '<PlainText>' to '<CipherText>' and decode that value to 'BCD'. This
        discrepancy is expected due to the simplicity of this demonstration which isn't designed to
        handle first character being 'A'."
 -TestCases @(@{
                PlainText  = 'ABCD'
                CipherText = 7310
            }) {
            Param($PlainText, $CipherText)
            #$OriginalPlainText = $PlainText
            #$OriginalCipherText = $CipherText
            {ConvertTo-CipherText $PlainText} | Should -Throw
            #$CipherText | Should -Be $OriginalCipherText

            #$PlainText = ConvertTo-PlainText $CipherText
            #$PlainText | Should -Be 'BCD'
        }
        It "Should encode '<PlainText>' to '<CipherText>' and decode that value back to
        '<PlainText>'."
 -TestCases @(@{
                PlainText  = 'BCDE'
                CipherText = 190100
            }) {
            Param($PlainText, $CipherText)
            $OriginalPlainText = $PlainText
            $OriginalCipherText = $CipherText
            $CipherText = ConvertTo-CipherText $PlainText
            $CipherText | Should -Be $OriginalCipherText

            $PlainText = ConvertTo-PlainText $CipherText
            $PlainText | Should -Be $OriginalPlainText
        }
        It "Should encode '<PlainText>' to '<CipherText>' and decode that value back to
        '<PlainText>'."
 -TestCases @(@{
                PlainText  = 'WXYZ'
                CipherText = 4028690
            }) {
            Param($PlainText, $CipherText)
            $OriginalPlainText = $PlainText
            $OriginalCipherText = $CipherText
            $CipherText = ConvertTo-CipherText $PlainText
            $CipherText | Should -Be $OriginalCipherText

            $PlainText = ConvertTo-PlainText $CipherText
            $PlainText | Should -Be $OriginalPlainText
        }
        It "Should encode '<PlainText>' to '<CipherText>' and decode that value back to
        '<PlainText>'."
 -TestCases @(@{
                PlainText  = 'B'
                CipherText = 10
            }) {
            Param($PlainText, $CipherText)
            $OriginalPlainText = $PlainText
            $OriginalCipherText = $CipherText
            $CipherText = ConvertTo-CipherText $PlainText
            $CipherText | Should -Be $OriginalCipherText

            $PlainText = ConvertTo-PlainText $CipherText
            $PlainText | Should -Be $OriginalPlainText
        }
        It "Should encode '<PlainText>' to '<CipherText>' and decode that value back to
        '<PlainText>'."
 -TestCases @(@{
                PlainText  = 'BO'
                CipherText = 400
            }) {
            Param($PlainText, $CipherText)
            $OriginalPlainText = $PlainText
            $OriginalCipherText = $CipherText
            $CipherText = ConvertTo-CipherText $PlainText
            $CipherText | Should -Be $OriginalCipherText

            $PlainText = ConvertTo-PlainText $CipherText
            $PlainText | Should -Be $OriginalPlainText
        }
        It "Should encode '<PlainText>' to '<CipherText>' and decode that value back to
        '<PlainText>'."
 -TestCases @(@{
                PlainText  = 'BOB'
                CipherText = 10410
            }) {
            Param($PlainText, $CipherText)
            $OriginalPlainText = $PlainText
            $OriginalCipherText = $CipherText
            $CipherText = ConvertTo-CipherText $PlainText
            $CipherText | Should -Be $OriginalCipherText

            $PlainText = ConvertTo-PlainText $CipherText
            $PlainText | Should -Be $OriginalPlainText
        }
        It "Should encode '<PlainText>' to '<CipherText>' and decode that value back to
        '<PlainText>'."
 -TestCases @(@{
                PlainText  = 'BOBB'
                CipherText = 270670
            }) {
            Param($PlainText, $CipherText)
            $OriginalPlainText = $PlainText
            $OriginalCipherText = $CipherText
            $CipherText = ConvertTo-CipherText $PlainText
            $CipherText | Should -Be $OriginalCipherText

            $PlainText = ConvertTo-PlainText $CipherText
            $PlainText | Should -Be $OriginalPlainText
        }
        It "Should encode '<PlainText>' to '<CipherText>' and decode that value back to
        '<PlainText>'."
 -TestCases @(@{
                PlainText  = 'BOBBY'
                CipherText = 7037660
            }) {
            Param($PlainText, $CipherText)
            $OriginalPlainText = $PlainText
            $OriginalCipherText = $CipherText
            $CipherText = ConvertTo-CipherText $PlainText
            $CipherText | Should -Be $OriginalCipherText

            $PlainText = ConvertTo-PlainText $CipherText
            $PlainText | Should -Be $OriginalPlainText
        }
        It "Should encode '<PlainText>' to '<CipherText>' and decode that value back to
        '<PlainText>'."
 -TestCases @(@{
                PlainText  = 'HELLOA'
                CipherText = 851986720
            }) {
            Param($PlainText, $CipherText)
            $OriginalPlainText = $PlainText
            $OriginalCipherText = $CipherText
            $CipherText = ConvertTo-CipherText $PlainText
            $CipherText | Should -Be $OriginalCipherText

            $PlainText = ConvertTo-PlainText $CipherText
            $PlainText | Should -Be $OriginalPlainText
        }
        It "Should encode '<PlainText>' to '<CipherText>' and decode that value back to
        '<PlainText>'."
 -TestCases @(@{
                PlainText  = 'HELLOALICE'
                CipherText = 389337485346720
            }) {
            Param($PlainText, $CipherText)
            $OriginalPlainText = $PlainText
            $OriginalCipherText = $CipherText
            $CipherText = ConvertTo-CipherText $PlainText
            $CipherText | Should -Be $OriginalCipherText

            $PlainText = ConvertTo-PlainText $CipherText
            $PlainText | Should -Be $OriginalPlainText
        }
    }
}