def __init__(self, pathname=None, encrypted=False, key=None, threshold=0.3): self.threshold = threshold self.encrypted = encrypted self.key = key #temporary self.pathname = pathname if (pathname != None): with open(pathname, 'rb') as f: self.content = f.read() print('read:', self.content) self.content_length = len(self.content) self.file_name, self.file_extension = os.path.splitext(pathname) self.file_name = self.file_name.split('/')[-1] # encrypt file if needed if (encrypted and key != None): # added by Zhengyu for checks print('Content to encrypt: ', self.content) self.content = vigenere_cipher.encrypt(self.content, key)
def vigenere(): ''' Approach: same as that of Caesar cipher listed above. ''' if request.method == 'POST': plain_text = request.form['plain_text'] keyword = str(request.form["keyword"]) return render_template('vigenere.html', data=[plain_text, vigenere_cipher.encrypt(plain_text, keyword)]) return render_template("vigenere.html", data=["", ""])
def vigenere(): ''' Approach: same as that of Caesar cipher listed above. ''' if request.method == 'POST': plain_text = request.form['plain_text'] cipher_text = request.form['cipher_text'] function = str(request.form.get('choices')) keyword = str(request.form["keyword"]) if function == "Encrypt": return render_template('vigenere.html', data=[plain_text, vigenere_cipher.encrypt(plain_text, keyword)]) if function == "Decrypt": return render_template('vigenere.html', data=[vigenere_cipher.decrypt(cipher_text, keyword), cipher_text]) if str(function) == "Crack": return render_template('genetic_algo.html', data=[cipher_text]) return render_template("vigenere.html", data=["Plain Text", "Cipher Text"])
def create_message_header(self): msg_header_string = "" msg_header_string += self.file_name + ";" msg_header_string += self.file_extension + ";" msg_header_string += str(self.content_length) + ";" #print(msg_header_string) self.header = msg_header_string.encode('utf-8') self.header_length = len(msg_header_string) if (self.encrypted and self.key != None): self.header = vigenere_cipher.encrypt(self.header, self.key) header_binary = self.to_binary(self.header) self.header_bitplane = self.to_bitplane(header_binary) return self.header_bitplane
def __init__(self, pathname=None, encrypted=False, key=None, threshold=0.3): self.threshold = threshold self.encrypted = encrypted self.key = key if (pathname != None): with open(pathname, 'rb') as f: self.content = f.read() self.content_length = len(self.content) self.file_name, self.file_extension = os.path.splitext(pathname) self.file_name = self.file_name.split('/')[-1] # encrypt file if needed if (encrypted and key != None): self.content = vigenere_cipher.encrypt(self.content, key)
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 vignere(): ''' :input: { 'process_type': 'encryption'\'decryption', 'key': <string>, '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 = vigenere_cipher.encrypt(req_body.get('text'), req_body.get('key')) return { 'status': 'success', 'text': cipher } elif process_type == 'decryption': cipher = vigenere_cipher.decrypt(req_body.get('text'), 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 Vigenere cipher.' } except Exception as e: return { 'status': 'failure', 'msg': f'Process failed! Reason: {e}' }
def test_encrypt(self): self.assertEqual(encrypt("hello", "abc"), "hfnlp")
letter = ntoa[(i + ALPH_LEN * 10 - shift) % ALPH_LEN] p = FREQUENCIES[letter] sum += (text.count(ntoa[i]) - len(text) * p)**2 / (len(text) * p) return sum def analyze_encrypted_text(text): text = text.lower() text = [l for l in text if l in ntoa] key_length = analyze_key_length(text) # print("Key length: {}".format(key_length)) key = [0 for i in range(key_length)] for i in range(key_length): chunk = text[i::key_length] chsq = [INF for j in range(ALPH_LEN)] for shift in range(ALPH_LEN): chsq[shift] = chi_squared(chunk, shift) key[i] = ntoa[chsq.index(min(chsq))] # print("Key: {}".format("".join(key))) proposed = vc.decrypt(text, ''.join(key)) return proposed if __name__ == "__main__": with codecs.open("plain_ukr.txt", encoding="utf-8") as f: text = f.read() print(analyze_encrypted_text(vc.encrypt(text, "поттер")))
def test_encrypt_lowercase(self): enctypted_text = encrypt('blockchain', 'iitibm') self.assertEqual(enctypted_text, 'JTHKLOPIBV')
def test_encrypt(self): enctypted_text = encrypt('BLOCKCHAIN', 'IITIBM') self.assertEqual(enctypted_text, 'JTHKLOPIBV')