Exemplo n.º 1
0
def main(argv):
    input_file = ''
    output_file = 'output.txt'
    key = 0
    is_decrypt = '-d' in argv
    try:
        opts, _args = getopt.getopt(argv, "dhi:k:o:")
    except getopt.GetoptError:
        help()

    for opt, arg in opts:
        if opt == '-h':
            help()
        elif opt == '-i':
            input_file = arg
        elif opt == '-o':
            output_file = arg
        elif opt == '-k':
            key = int(arg)

    if input_file:
        file_to_process = open(input_file, '+r')
        file_str = unidecode(file_to_process.read())

        if (is_decrypt):
            message = decrypt(file_str, key)
        else:
            message = encrypt(file_str, key)

        output = open(output_file, '+w')
        output.write(message)
Exemplo n.º 2
0
def main():
    key = int(input('\nInsert a number to be used as key: '))
    message = input('Your message: ')
    option = input('Would you like to encrypt or decrypt? ')

    if (option == 'encrypt'):

        print('\nYour message is now: ' + cipher.encrypt(message, key))
    elif (option == 'decrypt'):
        print('\nYour message is now: ' + cipher.decrypt(message, key))
    else:
        print('\nWrong argument.')
Exemplo n.º 3
0
    def helper_submission(self,text=None,key=None,option=None):
        import caesar_cipher as CAESAR
        import vigenere_cipher as VIGENERE
        ctext = ""
        if option=="CAESAR ENCRYPTION":
            ctext = CAESAR.encrypt(text,int(key)) #display
        elif option=="CAESAR DECRYPTION":
            ctext = CAESAR.decrypt(text,int(key))
        elif option=="VIGENERE ENCRYPTION":
            ctext = VIGENERE.encrypt(text,key)
        elif option=="VIGENERE DECRYPTION":
            ctext = VIGENERE.decrypt(text,key)
        elif option=="BREAK CAESAR":
            key = CAESAR.breakCaesar(text)
            ctext = CAESAR.decrypt(text,key)
            self.submit_key(key)
        else:
            key = VIGENERE.breakVigenere(text)
            ctext = VIGENERE.decrypt(text,key)
            self.submit_key(key)

        self.submit_text(ctext)
def caesar():
    '''
        render respective templates along with function calls for respective encryption, decryption & cracking for Caesar Cipher.
    '''
    if request.method == 'POST':
        plain_text = request.form['plain_text']
        cipher_text = request.form['cipher_text']
        function = request.form.get('choices')
        key_size = int(request.form.get('key_choices'))
        if str(function) == "Encrypt":
            return render_template('caesar.html', data=[25, plain_text, caesar_cipher.encrypt(plain_text, key_size)])
        if str(function) == "Decrypt":
            return render_template('caesar.html', data=[25, caesar_cipher.decrypt(cipher_text, key_size), cipher_text])
        if str(function) == "Crack":
            return render_template('caesar.html', data=[25, caesar_cipher.crack(cipher_text), cipher_text])
    return render_template("caesar.html", data=[25, "Plain Text", "Cipher Text"])
Exemplo n.º 5
0
def decrypt(file, key):
    try:
        file_content = file_utils.get_file_content(file)

        if file_content:
            new_file = file_utils.store_file_content(
                caesar_cipher.decrypt(file_content, key), 'decripted.txt')
            print(f'Content stored on {new_file}')

        else:
            print(f'The file {file} is empty')

    except FileNotFoundError:
        print(f'The file {file} does not exists')
    except ValueError:
        print('The selected key does not exists in the alphabet')
Exemplo n.º 6
0
def caesar():
    '''
    :input:
        {
            'process_type': 'encryption'\'decryption',
            'key': <integer value>,
            'text': <raw/encrypted message>
        }
    :return:
        {
            'status': 'success'/'failure',
            'msg': NA/<error message>,
            'text': NA/<encrypted/decrypted message>
    '''
    try:
        req_body = request.get_json()
        process_type = req_body.get('process_type')
        if process_type == 'encryption':
            cipher = caesar_cipher.encrypt(req_body.get('text'), int(req_body.get('key')))
            return {
                'status': 'success',
                'text': cipher
            }
        elif process_type == 'decryption':
            cipher = caesar_cipher.decrypt(req_body.get('text'), int(req_body.get('key')))
            return {
                'status': 'success',
                'text': cipher
            }
        else:
            return {
                'status': 'failure',
                'msg': 'invalid process type! Process type can only be encryption or decryption for caesar cipher.'
            }
    except Exception as e:
        return {
            'status': 'failure',
            'msg': f'Process failed! Reason: {e}'
        }
Exemplo n.º 7
0
def frequencyAnalysis(ciphertext=None):
    '''
    X = sum( (Observed-Expected)**2/Expected)
    return min X
    '''
    try:
        import math
        import caesar_cipher as CAESAR
        minKey = -1 #most likely key
        minChiDist = math.inf
        #Get chi distances for all keys (0->25 shifts)
        for shift in range(26):
            #shift text (caesar DECRYPTION)
            shiftedText = CAESAR.decrypt(ciphertext,shift)
            chiDist = chiSquareSummation(shiftedText)
            if chiDist<minChiDist:
                minChiDist=chiDist
                minKey = shift
        return minKey
    except Exception as e:
        print(e)

    return None
Exemplo n.º 8
0
'''
CAESAR CIPHER TESTS
'''
print("***CAESAR ENCRYPTION***")
print(CAESAR.encrypt("a",26*1222)=="a")
print(CAESAR.encrypt("a",27)=="b")
print(CAESAR.encrypt("a",25)=="z")
print(CAESAR.encrypt("z",25)=="y")
print(CAESAR.encrypt("z",27)=="a")
print(CAESAR.encrypt("a",26*26)=="a")
print(CAESAR.encrypt("a",27*27)=="b")
print(CAESAR.encrypt("a",25)=="z")
print(CAESAR.encrypt("a",625)=="b")
print(CAESAR.encrypt("z",27*3)=="c")
print("\n***CAESAR DECRYPTION***")
print(CAESAR.decrypt("a",26*1222)=="a")
print(CAESAR.decrypt("b",27)=="a")
print(CAESAR.decrypt("z",25)=="a")
print(CAESAR.decrypt("y",25)=="z")
print(CAESAR.decrypt("a",27)=="z")
print(CAESAR.decrypt("a",26*26)=="a")
print(CAESAR.decrypt("b",27*27)=="a")
print(CAESAR.decrypt("z",25)=="a")
print(CAESAR.decrypt("b",625)=="a")
print(CAESAR.decrypt("c",27*3)=="z")
print("\n***VIGENERE DECRYPTION***")
print(VIGENERE.decrypt("DM FR QS XU CF DM MZSY M BRUHK MFSY XU CF DM FR QS XU JRMDZ ZY SXF UFQ XIEL DM FR QS XU CF DM MDZJR UCTFS QZYZ YY CF DM FR QS XU CF KUGGQW MM TFF NY EJPF NZRW KILWW RYVQAZA SQMQ DLXL XYK F KYCCJ TQAZS ZMJGD LTCELK ICCQ UAGV YG KIL XG EGLWX KILWKQFW F YDCE TGAIFT A TUKJ KYOIKK UFC LWF SFZ AXF XJL MFC TX KIL NX UNJ YZQ FRXL FBZSY U YMJJ PI YJZQBVMW XU CF DM FR QS XU ETO KIL PFAQ KMW FOEJ QAOCQ TQ MDZJRCEL KAIE","smurf")
=="la la la la la la sing a happy song la la la la la la smurf it all day long la la la la la la smurf along with me la la la la la la simple as can be next time youre feeling blue just let a smile begin happy things will come to you so smurf yourself a grin oooooo i hate smurfs ill get you ill get all of you if its the last thing i ever do hehehehe la la la la la la now you know the tune youll be smurfing soon")
print("\n***VIGENERE BREAK***")
print(VIGENERE.breakVigenere("DM FR QS XU CF DM MZSY M BRUHK MFSY XU CF DM FR QS XU JRMDZ ZY SXF UFQ XIEL DM FR QS XU CF DM MDZJR UCTFS QZYZ YY CF DM FR QS XU CF KUGGQW MM TFF NY EJPF NZRW KILWW RYVQAZA SQMQ DLXL XYK F KYCCJ TQAZS ZMJGD LTCELK ICCQ UAGV YG KIL XG EGLWX KILWKQFW F YDCE TGAIFT A TUKJ KYOIKK UFC LWF SFZ AXF XJL MFC TX KIL NX UNJ YZQ FRXL FBZSY U YMJJ PI YJZQBVMW XU CF DM FR QS XU ETO KIL PFAQ KMW FOEJ QAOCQ TQ MDZJRCEL KAIE")=="smurf")
def test_decrypt_random():
    sent = 'Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they were perfectly normal, thank you very much'
    encrypted = encrypt(sent, random.randint(1, 26))
    assert sent == decrypt(encrypted)
def test_decryption_known():
    expected = 'the rain in spain falls mainly on the plain.'
    encrypted = 'vjg tckp kp urckp hcnnu ockpna qp vjg rnckp.'
    assert decrypt(encrypted) == expected
Exemplo n.º 11
0
def test_decrypt_random():
    org_sent = '“i hope that in this year to come, you make mistakes. because if you are making mistakes, then you are making new things, trying new things, learning, living, pushing yourself, changing yourself, changing your world. you’re doing things you’ve never done before, and more importantly, you’re doing something.”'
    encrypted = encrypt(org_sent, random.randint(1, 26))
    assert org_sent == decrypt(encrypted)
Exemplo n.º 12
0
def test_decrypt_1():
    encrypted_text = 'Jgnnq'
    expected = 'Hello'
    assert decrypt(encrypted_text, 2) == expected
Exemplo n.º 13
0
def test_decrypt_3():
    encrypted_text = 'Ygywagzin'
    expected = 'Sasquatch'
    assert decrypt(encrypted_text, 6) == expected
Exemplo n.º 14
0
def test_decrypt_2():
    encrypted_text = 'Tieryx'
    expected = 'Peanut'
    assert decrypt(encrypted_text, 4) == expected