Exemplo n.º 1
0
def hillCipher_cipher_run(mode: str):
    key = hillCipher_cipher_get_key()

    cipher = HillCipher(key)

    if mode == "Encrypt":
        plaintext = plaintext_read_prompt()

        ciphertext = cipher.encrypt(plaintext)

        ciphertext_write_prompt(ciphertext)

    elif mode == "Decrypt":
        ciphertext = ciphertext_read_prompt()

        plaintext = cipher.decrypt(ciphertext)

        plaintext_write_prompt(plaintext)
Exemplo n.º 2
0
    def test_encrypt(self):
        key = [
            [17, 17, 5],
            [21, 18, 21],
            [2, 2, 19]
        ]

        cipher = HillCipher(key)

        plaintext = "paymoremoney"

        ciphertext = cipher.encrypt(plaintext)

        self.assertEqual(ciphertext, "LNSHDLEWMTRW")

        key = [
            [1, 5],
            [3, 4],
        ]

        cipher = HillCipher(key)

        plaintext = "kucingunyu"

        ciphertext = cipher.encrypt(plaintext)

        self.assertEqual(ciphertext, "GGQMRLHIUW")
Exemplo n.º 3
0
 def test_HillCipherError4(self):
     algorithm = HillCipher()
     key = algorithm.Analyse3By3Key(self.mainPlain, self.cipherS3)
     self.assertEqual(key, self.keyS3)
Exemplo n.º 4
0
    def test_HillCipherError1(self):
        algorithm = HillCipher()

        key2 = algorithm.Analyse(self.mainPlainError, self.mainCipherError)
Exemplo n.º 5
0
    def test_HillCipherError3(self):
        algorithm = HillCipher()

        key2 = algorithm.Decrypt(self.plain, self.keyError)
Exemplo n.º 6
0
 def test_HillCipherTestNew2By2Analysis(self):
     algorithm = HillCipher()
     key = algorithm.Analyse(self.newPlain, self.newCipher)
     self.assertEqual(key, self.newKey)
Exemplo n.º 7
0
def classicalCipher(cipherType):
    if cipherType == 1:  # Cesar
        print("You choose Cesar Cipher")
        plainText = input("Enter PlainText\n")
        key = int(input("Enter key number\n"))
        cipher = cesar()
        cipherText = cipher.encrypt(plainText, key)
        print("Cipher Text: " + str(cipherText))

    elif cipherType == 2:  # playFair
        print("You choose PlayFair Cipher")
        plainText = input("Enter PlainText\n")
        key = str(
            input(
                "Note: Key is a string value in playfair cipher\nEnter key\n"))
        cipher = playFair()
        cipherText = cipher.encrypt(plainText, key)
        print("Cipher Text: " + str(cipherText))

    elif cipherType == 3:  # Hill Cipher
        print("You choose Hill Cipher")
        cipher = HillCipher()
        plainText = input("Enter PlainText\n")
        cipherText = ''
        keyType = int(
            input("Enter Key Type\n" + "1: 2*2 key matrix\n" +
                  "2: 3*3 key matrix\n"))
        if keyType == 1:
            key = array([[5, 8], [17, 3]])
            print("we choose this key matrix")
            print(key)
            val = int(
                input("1: continue with this matrix\n" +
                      "2: change matrix\n"))  # val--> user decision
            if val == 2:
                for i in range(2):
                    for j in range(2):
                        key[i][j] = int(
                            input("Enter key Element [" + str(i) + "][" +
                                  str(j) + "]: "))
            cipherText = cipher.encrypt(plainText, key)
        elif keyType == 2:
            key = array([[2, 4, 12], [9, 1, 6], [7, 5, 3]])
            print("we choose this key matrix")
            print(key)
            val = int(
                input("1: continue with this matrix\n" +
                      "2: change matrix\n"))  # val--> user decision
            if val == 2:
                for i in range(3):
                    for j in range(3):
                        key[i][j] = int(
                            input("Enter key Element [" + str(i) + "][" +
                                  str(j) + "]: "))
            cipherText = cipher.encrypt(plainText, key)
        print("Cipher Text: " + str(cipherText))
    elif cipherType == 4:  # vigenere cipher
        print("You choose Vigenere Cipher")
        plainText = input("Enter PlainText\n")
        key = input("Enter key\n")
        mode = bool(
            input("Enter your mode\n" + "True: Auto key mode\n" +
                  "False: Repeat key mode\n"))
        cipher = Vigenere()
        cipherText = cipher.encrypt(plainText, key, mode)
        print("Cipher Text: " + str(cipherText))

    else:  # Vernam Cipher
        print("You choose Vernam Cipher")
        plainText = input("Enter PlainText\n")
        # key = "SPARTANS"  # key Generator
        key = input("Enter Key\n")
        while len(key) < len(plainText):
            print("Please enter key with the same length of plainText\n")
            key = input("Enter Key\n")
        cipher = vernam()
        cipherText = cipher.encrypt(plainText, key)
        print("Cipher Text: " + str(cipherText))
Exemplo n.º 8
0
    def test_HillCipherTestDec6(self):
        algorithm = HillCipher()

        plain2 = algorithm.Decrypt(self.cipher4, self.key4)
        for i in range(len(self.plain4)):
            self.assertEqual(self.plain4[i], plain2[i])
Exemplo n.º 9
0
    def test_HillCipherTestDec4(self):
        algorithm = HillCipher()

        plain2 = algorithm.Decrypt(self.cipher3, self.key3)
        for i in range(len(self.plain)):
            self.assertEqual(self.plain[i], plain2[i])
Exemplo n.º 10
0
 def test_HillCipherTestDec5(self):
     algorithm = HillCipher()
     plain = algorithm.Decrypt(self.mainCipher3, self.mainKey3)
     self.assertEqual(plain, self.mainPlain3)
Exemplo n.º 11
0
 def test_HillCipherTestEnc5(self):
     algorithm = HillCipher()
     cipher = algorithm.Encrypt(self.mainPlain3, self.mainKey3)
     self.assertEqual(cipher, self.mainCipher3)
Exemplo n.º 12
0
 def test_HillCipherTestDec3(self):
     algorithm = HillCipher()
     plain = algorithm.Decrypt(self.cipherS3, self.keyS3)
     self.assertEqual(plain, self.mainPlain)
Exemplo n.º 13
0
 def test_HillCipherTestEnc3(self):
     algorithm = HillCipher()
     cipher = algorithm.Encrypt(self.mainPlain, self.keyS3)
     self.assertEqual(cipher, self.cipherS3)
Exemplo n.º 14
0
 def test_HillCipherTest2By2Analysis1(self):
     algorithm = HillCipher()
     key = algorithm.Analyse(self.mainPlain, self.mainCipher)
     self.assertEqual(key, self.mainKey)
Exemplo n.º 15
0
    def test_HillCipherTest2By2Analysis2(self):
        algorithm = HillCipher()

        key2 = algorithm.Analyse(self.plain, self.cipher)
        for i in range(len(self.key)):
            self.assertEqual(self.key[i], key2[i])
Exemplo n.º 16
0
    def test_HillCipherTestEnc4(self):
        algorithm = HillCipher()

        cipher2 = algorithm.Encrypt(self.plain, self.key3)
        for i in range(len(self.cipher3)):
            self.assertEqual(self.cipher3[i], cipher2[i])
Exemplo n.º 17
0
 def test_HillCipherTest3By3Analysis1(self):
     algorithm = HillCipher()
     key = algorithm.Analyse3By3Key(self.mainPlain3, self.mainCipher3)
     self.assertEqual(key, self.mainKey3)
Exemplo n.º 18
0
    def test_HillCipherTestEnc6(self):
        algorithm = HillCipher()

        cipher2 = algorithm.Encrypt(self.plain4, self.key4)
        for i in range(len(self.cipher4)):
            self.assertEqual(self.cipher4[i], cipher2[i])
Exemplo n.º 19
0
 def test_HillCipherTestNewEnc(self):
     algorithm = HillCipher()
     cipher = algorithm.Encrypt(self.newPlain, self.newKey)
     self.assertEqual(cipher, self.newCipher)
Exemplo n.º 20
0
    def test_HillCipherTest3By3Analysis2(self):
        algorithm = HillCipher()

        key2 = algorithm.Analyse3By3Key(self.plain4, self.cipher4)
        for i in range(len(self.key4)):
            self.assertEqual(self.key4[i], key2[i])
Exemplo n.º 21
0
 def test_HillCipherTestNewDec(self):
     algorithm = HillCipher()
     plain = algorithm.Decrypt(self.newCipher, self.newKey)
     self.assertEqual(plain, self.newPlain)