def test_setupKeyFiles(self):
        crypto.setupKeyFiles(self.p, self.e1, self.d)

        with open(crypto.pubkeyfname, 'r') as f:
            splits = f.read().split()
            p_pubfname = int(splits[0], 10)
            e1_pubfname = int(splits[1], 10)
            e2_pubfname = int(splits[2], 10)

        with open(crypto.prikeyfname, 'r') as f:
            splits = f.read().split()
            p_prifname = int(splits[0], 10)
            e1_prifname = int(splits[1], 10)
            d_prifname = int(splits[2], 10)

        self.assertEqual(p_pubfname, p_prifname)
        self.assertEqual(p_pubfname, self.p)
        self.assertEqual(True, crypto.millerRabinPrimeTest(self.p))

        self.assertEqual(2, e1_pubfname)
        self.assertEqual(2, e1_prifname)

        self.assertEqual(e2_pubfname, pow(e1_pubfname, d_prifname, p_pubfname))
        self.assertEqual(self.e2, pow(2, 1007, self.p))

        self.assertEqual(self.d, d_prifname)
        print 'e2: ', pow(2, 1007, self.p)
Beispiel #2
0
def callAction(choice):
    if (choice == 0):
        auto_compute_p_and_generator_encryption_decryption()
        return 1

    if (choice == 1):
        print '_______________________________________________'
        print 'Public-key cryptography computes key files'
        m = input('Enter your computed modulo: ')
        g = input('Enter your computed generator: ')
        pri = input('Enter your private key: ')
        crypto.setupKeyFiles(m, g, pri)
        printKeyFiles()
        return 1

    if (choice == 2):
        print '_______________________________________________'
        print 'Public-key cryptography computes key files'
        pri = input('Enter your private key: ')
        if (crypto.computeKeyFiles(pri)): printKeyFiles()
        return 1

    if (choice == 3):
        print '_______________________________________________'
        print 'Write a plaintext'
        create_plaintext_file()
        with open(crypto.ptextfname, 'r') as f:
            print f.read()
        return 1

    if (choice == 4):
        print '_______________________________________________'
        print 'Public-key cryptography ENCRYPTION'
        if (not checkFileExist(crypto.pubkeyfname) or
            not checkFileExist(crypto.prikeyfname) or
            not checkFileExist(crypto.ptextfname)):
            print 'No required files for encryption'
            return 1
        crypto.encrypt()
        print 'Done'
        return 1

    if (choice == 5):
        print '_______________________________________________'
        print 'Public-key cryptography DECRYPTION'
        if (not checkFileExist(crypto.pubkeyfname) or
            not checkFileExist(crypto.prikeyfname) or
            not checkFileExist(crypto.ptextfname)):
            print 'No required files for decryption'
            return 1        
        crypto.decrypt()
        with open(crypto.dtextfname, 'r') as f:
            print f.read()
        print 'Done'
        return 1

    return 0
    def test_encrytion_short_msg(self):
        crypto.setupKeyFiles(self.p, self.e1, self.d)

        with open(crypto.ptextfname, 'w') as f:
            f.write(self.shorttext)

        crypto.encrypt()
        crypto.decrypt()

        with open(crypto.dtextfname, 'r') as f:
            lines = f.readlines()
            self.assertEqual(1, len(lines))
            self.assertEqual(self.shorttext, lines[0])
    def test_encrytion_two_lines(self):
        crypto.setupKeyFiles(self.p, self.e1, self.d)

        with open(crypto.ptextfname, 'w') as f:
            f.write(self.text)
            f.write('\n')
            f.write(self.text1)

        crypto.encrypt()
        crypto.decrypt()

        with open(crypto.dtextfname, 'r') as f:
            lines = f.readlines()
            self.assertEqual(2, len(lines))
            self.assertEqual(self.text, lines[0].replace('\n', ''))
            self.assertEqual(self.text1, lines[1])
Beispiel #5
0
def setup_key_files_encryption_decryption():
    print '_______________________________________________'
    print 'Public-key cryptography computes key files'
    crypto.setupKeyFiles(p, e1, prikey)
    printKeyFiles()

    print '_______________________________________________'
    print 'Write a plaintext'
    create_plaintext_file()
    with open(crypto.ptextfname, 'r') as f:
        print f.read()

    print '_______________________________________________'
    print 'Public-key cryptography ENCRYPTION'
    crypto.encrypt()

    print '_______________________________________________'
    print 'Public-key cryptography DECRYPTION'
    crypto.decrypt()
    with open(crypto.dtextfname, 'r') as f:
        print f.read()

    print '\nDone!!!'