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)
def method(text): """here's the main method for this module. it'll take some text, clean it up, see how far away it is from regular english and print the text that is closest to english, and return the key """ letters = prepare(text) key = [] for i in range(26): attempt = [] for letter in letters: j = (alpha.index(letter)+i)%len(alpha) attempt.append(alpha[j]) key.append((countInversions(attempt),i)) output = (325,26) for i in key: if i[0]<output[0]: output = (i[0],i[1]) print caesar.encrypt(text,output[1]) return output
def main(): jedi = open("/home/chuphay/python/projects/codes/text/jedi.txt").read() #print jedi zee = prepare(jedi) ciph = caesar.encrypt(jedi,1) ciff = prepare(ciph) #print ciph #print countInversions(zee) #print countInversions(ciff) print method(ciph) print 13*25
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.')
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"])
def encrypt(file, key): try: file_content = file_utils.get_file_content(file) if file_content: new_file = file_utils.store_file_content( caesar_cipher.encrypt(file_content, key), 'encriptado.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')
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}' }
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 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)
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_encrypt_3(): text = 'Sasquatch' expected = 'Ygywagzin' assert encrypt(text, 6) == expected
def test_encrypt_1(): text = 'Hello' expected = 'Jgnnq' assert encrypt(text, 2) == expected
from caesar_cipher import encrypt from public_key_cryptography import return_shared_secret alices_unencrypted_message = "TheBeerRunsAtMidnight" shared_secret = return_shared_secret() alices_encrypted_message = encrypt(alices_unencrypted_message, shared_secret["shared_secret_alice"]) # print(alices_encrypted_message) bob_decrypts_alices_message = encrypt(alices_encrypted_message, -shared_secret["shared_secret_bob"]) # print(bob_decrypts_alices_message) print(f""" Alice's original message: {alices_unencrypted_message} Alice's encrypted message: {alices_encrypted_message} Bob's decryption of Alice's encrypted message: {bob_decrypts_alices_message}""")
def test_encrypt_2(): text = 'Peanut' expected = 'Tieryx' assert encrypt(text, 4) == expected
def test_encrypt_XYZ(self): self.assertEqual(encrypt("XYZ"), "ABC")
def test_encrypt_ABC(self): self.assertEqual(encrypt("ABC"), "DEF")
def test_encrypt_xyz(self): self.assertEqual(encrypt("xyz"), "abc")
# REQUEST INFORMATION FROM SERVER with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect(('localhost', port)) s.send("request server name".encode()) data = s.recv(1024).decode() print(f"Client received this information from server: {data}") # REQUEST INFORMATION FROM CA with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as t: t.connect((host, ca_port)) t.send(data.encode()) key = t.recv(1024).decode() print(f"Client received this from CA: {key}") t.close() # VERIFY KEY WITH SERVER encrypted_message = encrypt('sessioncipherkey', key) s.send(encrypted_message.encode()) print("Client sent encrypted message to server") # RECEIVE ENCRYPTED MESSAGE AND VERIFY MESSAGE data = s.recv(1024).decode() if data == encrypt("sessioncipherkeyacknowledgment", key): # ENCRYPT MESSAGE USING KEY message = encrypt('Final transmission', key) s.send(message.encode()) print("Sending final message") s.close()
def test_encrypt_abc(self): self.assertEqual(encrypt("abc"), "def")
def test_encryption(): sent = 'The rain in Spain falls mainly on the plain.' expected = 'vjg tckp kp urckp hcnnu ockpna qp vjg rnckp.' assert encrypt(sent, 2) == expected
def test_encrypt_empty(self): self.assertEqual(encrypt(""), "")
def test_encrypt_none(self): self.assertEqual(encrypt(None), "")
def test_encrypt_sentence(self): sentence = "the quick brown fox jumps over the lazy dog" encrypted_sentence = "wkh#txlfn#eurzq#ira#mxpsv#ryhu#wkh#odcb#grj" self.assertEqual(encrypt(sentence), encrypted_sentence)
import caesar_cipher as CAESAR import vigenere_cipher as VIGENERE import common ''' 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")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ca, ca_port)) s.send(servername.encode()) s.close() # CREATE SOCKET FOR CLIENT ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ss.bind((host, port)) ss.listen() c, addr = ss.accept() while True: data = c.recv(1024).decode() # PROVIDE CLIENT WITH SERVER NAME if data == "request server name": print(f"Server received this request from client: {data}") c.send(servername.encode()) # VALIDATE AND SEND CLIENT ACKNOWLEDGMENT elif data == encrypt('sessioncipherkey', key): print(f"Server received proper key from client") acknowledgment = encrypt("sessioncipherkeyacknowledgment", key) c.send(acknowledgment.encode()) print("Ready for transmission...") elif data == encrypt("Final transmission", key): print("Final message received") # CLOSE SERVER else: print(f"Server done receiving") s.close() break