-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHillTest.py
More file actions
90 lines (63 loc) · 3.27 KB
/
HillTest.py
File metadata and controls
90 lines (63 loc) · 3.27 KB
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
'''
@author William Ray Johnson
9/7/17
'''
import unittest
import Matrix
import Hill
class HillTest(unittest.TestCase):
def setUp(self):
self.key2x2 = Matrix.Matrix(2, 2, [7, 19, 8, 3])
self.plainText = "test"
self.cipherText = "BITT"
self.cipher2x2 = Hill.Hill([7, 19, 8, 3])
self.cipher3x3 = Hill.Hill([6,24,1,13,16,10,20,17,15])
def testConvertPlainTextToNumber(self):
expectedPTNumbers = [19, 4, 18, 19]
actualPTNumbers = self.cipher2x2.convertToNumbers(self.plainText)
self.assertEqual(actualPTNumbers, expectedPTNumbers)
def testSplitPlainTextIntoMatrices(self):
expectedMatrices = [Matrix.Matrix(2,1,[19,4]), Matrix.Matrix(2,1,[18,19])]
actualMatricies = self.cipher2x2.splitPlainTextIntoMatrices(self.plainText)
self.assertEqual(actualMatricies, expectedMatrices)
def testConvertPlainTextToNumberWithPadding2x2(self):
expectedMatrices = [Matrix.Matrix(2,1,[19,4]), Matrix.Matrix(2,1,[18,19]),
Matrix.Matrix(2,1,[8, 13]), Matrix.Matrix(2,1,[6,23])]
actualMatricies = self.cipher2x2.splitPlainTextIntoMatrices("testing")
self.assertEqual(actualMatricies, expectedMatrices)
def testConvertPlainTextToNumberWithPadding3x3(self):
expectedMatrices = [Matrix.Matrix(3,1,[19,4,18]), Matrix.Matrix(3,1,[19,23,23])]
actualMatricies = self.cipher3x3.splitPlainTextIntoMatrices("test")
self.assertEqual(actualMatricies, expectedMatrices)
def testConvertNumbersToText(self):
expectedText = "test"
actualText = self.cipher2x2.convertToText([19,4,18,19])
self.assertEqual(actualText, expectedText)
def testEncrypt(self):
expectedEncryption = "BITT"
actualEncryption = self.cipher2x2.encrypt(self.plainText)
self.assertEqual(actualEncryption, expectedEncryption)
def testEncryptWithWikipediaExample(self):
wikiCipher = Hill.Hill([3,3,2,5])
expectedEncryption = ("HIAT")
actualEncryption = wikiCipher.encrypt("help")
self.assertEqual(actualEncryption, expectedEncryption)
def testCalcDecryptionKeyWithWikipediaExample(self):
cipher = Hill.Hill(([3,3,2,5]))
expectedDecryptKey = Matrix.Matrix(2,2, [15,17,20,9])
actualDecryptKey = cipher.getDecryptKey()
self.assertEqual(actualDecryptKey.getMatrixValue(), expectedDecryptKey.getMatrixValue())
def testCalcDecryptionKey(self):
expectedDecryptKey = Matrix.Matrix(3,3, [8,5,10,21,8,21,21,12,8])
actualDecryptKey = self.cipher3x3.getDecryptKey()
self.assertEqual(actualDecryptKey.getMatrixValue(), expectedDecryptKey.getMatrixValue())
def testDecrypt(self):
expectedDecryption = "test"
actualDecryption = self.cipher2x2.decrypt(self.cipherText)
self.assertEqual(actualDecryption, expectedDecryption)
def testInvertNumber(self):
expectedInversion = 3
actualInversion = self.cipher2x2.invertNumber(9)
self.assertEqual(actualInversion, expectedInversion)
if __name__ == '__main__':
unittest.main()