Exemplo n.º 1
0
def main():
    print()
    cipher = None

    # Determine which type of cipher
    if cipher_name == 'PLF':
        cipher = Playfair()
    elif cipher_name == 'RTS':
        cipher = RowTransposition()
    elif cipher_name == 'RFC' or cipher_name == 'CES':
        try:
            shift_amt = int(key)
        except ValueError:
            print("ERR: Please enter a number as the key for a Caesar/Railfence cipher!\n")
            return
        cipher = Railfence() if cipher_name == 'RFC' else Caesar()
    elif cipher_name == 'VIG':
        cipher = Vigenere()
    else:
        print("ERR: Not a valid cipher type! Please use:")
        print("\tPLF, RTS, RFC, VIG, or CES\n")
        return
    
    # Set the key
    if not cipher.setKey(key):
        print("ERR: Invalid key, try again.\n")
        return

    inFile = None
    outFile = None
    
    # Attempt to read in the text the user wants to encrypt/decrypt
    try:
        inFile = open(input_file, "r")
    except FileNotFoundError:
        print("ERR: '", input_file, "' cannot be opened! Try a valid file\n")
        return

    outFile = open(output_file, 'w')
    
    # Perfrom the encryption/decryption
    if method == "ENC":
        plaintext = inFile.read()
        ciphertext = cipher.encrypt(plaintext)
        outFile.write(ciphertext)
        print("Encryption was successfull!")

    elif method == "DEC":
        ciphertext = inFile.read()
        plaintext = cipher.decrypt(ciphertext)
        outFile.write(plaintext)
        print("Decryption was successfull!")

    else:
        print("ERR: Incorrect method. Please enter 'ENC' or 'DEC'\n")
        return

    print("Thank you for using this program.\n")
Exemplo n.º 2
0
def _():
    message = "This text is a sample to be encode and decode with various encryption methods."
    cesar = Cesar(3)  # Offset = 3
    vigenere = Vigenere("password")  # Key = password
    rot13 = ROT13()
    vernam = Vernam(len(message))  # RandomKey length = length of message
    rsa = RSA(33967, 917227)  # p et q prime number

    @it('should test the constructor of Cesar encryption module')
    def testConstructorOfCesarModule():
        assert cesar.offset == 3

    @it('should test the constructor of Vigenere encryption module')
    def testConstructorOfVigenereModule():
        assert vigenere.key == "password"

    @it('should test the constructor of ROT13 encryption module')
    def testConstructorOfROT13Module():
        assert rot13.rotation == 13

    @it('should test the constructor of Vernam encryption module')
    def testConstructorOfVernamModule():
        assert len(vernam.key) == len(message)

    @it('should test the constructor of RSA encryption module')
    def testConstructorOfRSAModule():
        assert rsa.p == 33967
        assert rsa.q == 917227

    @it('should test the Cesar encryption method')
    def testCesarEncryptionMethod():
        encoded = cesar.encrypt(message)
        decoded = cesar.decrypt(encoded)
        assert decoded == message

    @it('should test the Vigenere encryption method')
    def testVigenereEncryptionMethod():
        encoded = vigenere.encrypt(message)
        decoded = vigenere.decrypt(encoded)
        assert decoded == message

    @it('should test the ROT13 encryption method')
    def testROT13EncryptionMethod():
        encoded = rot13.encrypt(message)
        decoded = rot13.decrypt(encoded)
        assert decoded == message

    @it('should test the Vernam encryption method')
    def testVernamEncryptionMethod():
        encoded = vernam.encrypt(message)
        decoded = vernam.decrypt(encoded)
        assert decoded == message

    @it('should test the RSA encryption method')
    def testRSAEncryptionMethod():
        encoded = rsa.encrypt(message)
        decoded = rsa.decrypt(encoded)
        assert decoded == message
Exemplo n.º 3
0
密文: 442315 4145241325 1242345233 213453 2445323543 442315 31115554 143422
#普莱菲尔密码
加密解密实例(ps:这里加解密也是横向编制密码表):
#!python
>>>from pycipher import Playfair
>>>Playfair('CULTREABDFGHIKMNOPQSVWXYZ').encipher('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG')
#输出'UKDNLHTGFLWUSEPWHLISNPCGCRGAUBVZAQIV'
>>>Playfair('CULTREABDFGHIKMNOPQSVWXYZ').decipher('UKDNLHTGFLWUSEPWHLISNPCGCRGAUBVZAQIV')
#输出'THEQUICKBROWNFOXIUMPSOVERTHELAZYDOGX'
#维吉尼亚密码
明文: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
密钥(循环使用,密钥越长相对破解难度越大): CULTURE
(2)已知密钥加解密
#!python
>>>from pycipher import Vigenere
>>>Vigenere('CULTURE').encipher('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG')
'VBPJOZGMVCHQEJQRUNGGWQPPKNYINUKRXFK'
>>>Vigenere('CULTURE').decipher('VBPJOZGMVCHQEJQRUNGGWQPPKNYINUKRXFK')
'THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG'
#格罗斯费尔德密码(Gronsfeld cipher)
#!python
>>>from pycipher import Gronsfeld
>>>Gronsfeld([2,20,11,45,20,43,4]).encipher('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG')
'VBPJOZGMVCHQEJQRUNGGWQPPKNYINUKRXFK'
>>>Gronsfeld([2,20,11,45,20,43,4]).decipher('VBPJOZGMVCHQEJQRUNGGWQPPKNYINUKRXFK')
'THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG'
#自动密钥密码
#Porta密码--Porta密码(Porta Cipher)是一个由意大利那不勒斯的医生Giovanni Battista della Porta发明的多表代换密码,Porta密码具有加密解密过程的是相同的特点
密表
#!shell
KEYS| A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Exemplo n.º 4
0
def choose_cipher(cipher_name, key, option, input_file, output_file):
    #Playfair
    if cipher_name == "PLF":
        if option == "ENC":
            pass

        elif option == "DEC":
            pass

        else:
            print("Invalid option! Options are ENC/DEC")

    #Row Transposition
    elif cipher_name == "RTS":
        rowtransposition = RowTransposition()
        rowtransposition.setKey(key)

        if option == "ENC":
            plain_text = read_file(input_file)
            cipher_text = rowtransposition.encrypt(plain_text)
            write_file(output_file, cipher_text)

        elif option == "DEC":
            cipher_text = read_file(input_file)
            plain_text = rowtransposition.decrypt(cipher_text)
            write_file(output_file, plain_text)

        else:
            print("Invalid option! Options are ENC/DEC")

    #Railfence
    elif cipher_name == "RFC":
        railfence = Railfence()
        railfence.setKey(key)

        if option == "ENC":
            plain_text = read_file(input_file)
            cipher_text = railfence.encrypt(plain_text)
            write_file(output_file, cipher_text)

        elif option == "DEC":
            cipher_text = read_file(input_file)
            plain_text = railfence.decrypt(cipher_text)
            write_file(output_file, plain_text)

        else:
            print("Invalid option! Options are ENC/DEC")

    #Vigenere
    elif cipher_name == "VIG":
        vigenere = Vigenere()
        vigenere.setKey(key)

        if option == "ENC":
            plain_text = read_file(input_file)
            cipher_text = vigenere.encrypt(plain_text)
            write_file(output_file, cipher_text)

        elif option == "DEC":
            cipher_text = read_file(input_file)
            plain_text = vigenere.decrypt(cipher_text)
            write_file(output_file, plain_text)

        else:
            print("Invalid option! Options are ENC/DEC")

    #Caesar
    elif cipher_name == "CES":
        caesar = Caesar()
        caesar.setKey(int(key))

        if option == "ENC":
            plain_text = read_file(input_file)
            cipher_text = caesar.encrypt(plain_text)
            write_file(output_file, cipher_text)

        elif option == "DEC":
            cipher_text = read_file(input_file)
            plain_text = caesar.decrypt(cipher_text)
            write_file(output_file, plain_text)

        else:
            print("Invalid option! Options are ENC/DEC")

    else:
        print("Invalid cipher name chosen!")
Exemplo n.º 5
0
#Checking user input
if len(sys.argv) < 6:
    print("You are missing arguments, check your input.")
else:
    #Playfair Cipher
    if str(sys.argv[1]) == 'PLF':
        cipher_type = Playfair.Playfair()
    #Row_Transposition Cipher
    elif str(sys.argv[1]) == 'RTS':
        cipher_type = Row_Transposition.Row_Transposition()
    #RailFence Cipher
    elif str(sys.argv[1]) == 'RFC':
        cipher_type = RailFence.Railfence()
    #Vigenere Cipher
    elif str(sys.argv[1]) == 'VIG':
        cipher_type = Vigenere.Vigenere()
    #Caesar Cipher
    elif str(sys.argv[1]) == 'CES':
        cipher_type = Caesar.Caesar()
    #Invalid Input
    else:
        print("Unrecognized cipher type, check your spelling and try again.")
        exit()
    #Setting key from inputfile
    if cipher_type.setKey(sys.argv[2]):
        inFile = open(str(sys.argv[4]), "r")
        inputText = inFile.read()
        if str(sys.argv[3]) == 'ENC':
            outputText = cipher_type.encrypt(inputText)
        elif str(sys.argv[3] == 'DEC'):
            outputText = cipher_type.decrypt(inputText)
Exemplo n.º 6
0
from Caesar import *
from Combined import *
from MorseCode import *
from OneTimePad import *
from Vigenere import *
from Binary import *
from Hex import *
from Octal import *

#create instances of each class
a1 = A1()
caesar = Caesar()
comb = Combined()
morse = Morse()
otp = OneTimePad()
vig = Vigenere()
binary = Binary()
hexa = Hexa()
octal = Octal()

#in order to avoid long if statements, functions/methods added to lists
encFunc = [
    vig.encrypt, otp.encrypt, caesar.encrypt, a1.encrypt, morse.encrypt,
    comb.encrypt
]
decFunc = [
    vig.decrypt, otp.decrypt, caesar.decrypt, a1.decrypt, morse.decrypt,
    comb.decrypt
]

#need encode and decode list and convert to and convert from lists