示例#1
0
    def breakForAllLangs(self, encrypted):
        max = 0
        keyLength = 0
        language = ''
        ch = ''
        punc = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
        for key in self.dictionaries:
            char = self.mostCommonCharIn(self.dictionaries[key])
            for klength in range(1, 100):
                keys = self.tryKeyLength(encrypted, klength, char)
                vc = VigenereCipher(keys)
                message = vc.decrypt(encrypted)
                for c in message:
                    if c in punc:
                        message = message.replace(c, '')
                count = self.countWords(message, self.dictionaries[key])
                if count > max:
                    max = count
                    keyLength = klength
                    if language != key:
                        language = key
                        ch = char

        print("language: {}, valid word: {}, key length: {}".format(
            language, max, keyLength))
        keys = self.tryKeyLength(encrypted, keyLength, ch)

        vc = VigenereCipher(keys)
        return vc.decrypt(encrypted)
# -*- coding: utf-8 -*-

import unittest
from VigenereCipher import VigenereCipher

abc = "abcdefghijklmnopqrstuvwxyz"
key = "password"
c = VigenereCipher(key, abc)

j = VigenereCipher('カタカナ',
                   'アイウエオァィゥェォカキクケコサシスセソタチツッテトナニヌネノハヒフヘホマミムメモヤャユュヨョラリルレロワヲンー')


class VigenereCipherTest(unittest.TestCase):
    def test(self):
        self.assertEqual(c.encode('codewars'), 'rovwsoiv')
        self.assertEqual(c.decode('rovwsoiv'), 'codewars')
        self.assertEqual(c.encode('waffles'), 'laxxhsj')
        self.assertEqual(c.decode('laxxhsj'), 'waffles')
        self.assertEqual(c.encode('CODEWARS'), 'CODEWARS')
        self.assertEqual(c.decode('CODEWARS'), 'CODEWARS')
        self.assertEqual(c.encode('it\'s a shift cipher!'),
                         'xt\'k o vwixl qzswej!')
        self.assertEqual(c.decode('xt\'k o vwixl qzswej!'),
                         'it\'s a shift cipher!')
        self.assertEqual(j.encode('ドモアリガトゴザイマス'), 'ドオカセガヨゴザキアニ')
        self.assertEqual(j.decode('ドオカセガヨゴザキアニ'), 'ドモアリガトゴザイマス')
示例#3
0
from VigenereCipher import VigenereCipher

keys = [2, 3, 4]

vc = VigenereCipher(keys)
input = 'Try vigenere cipher'
encrypted = vc.encrypt(input)
decrypted = vc.decrypt(encrypted)
print (encrypted, decrypted)
print (vc)
示例#4
0
#Tests for vigenere_cipher.py
from VigenereCipher import VigenereCipher
VC = VigenereCipher()
assert(VC.cipher('take', 'care') == 'VABI')
assert(VC.decipher('VABI', 'care') == 'TAKE')

assert(VC.cipher('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') == 'ACEGIKMOQSUWYACEGIKMOQSUWY')
assert(VC.decipher('ACEGIKMOQSUWYACEGIKMOQSUWY', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
 def test_decode(self):
     cipher = VigenereCipher("TRAIN")
     decoded = cipher.decode("XECWQXUIVCRKHWA")
     assert decoded == "ENCODEDINPYTHON"
示例#6
0
from VigenereCipher import VigenereCipher
import socket

HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 6332  # Port to listen on (non-privileged ports are > 1023). port should be an integer from 1-65535 (0 is reserved)
keys = [2, 3, 4, 5]
vc = VigenereCipher(keys)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    encrypted = ''
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            encrypted += str(data, 'utf-8')
            if not data:
                print(vc.decrypt(encrypted))
                break
            conn.sendall(data)
 def test_encode_spaces(self):
     cipher = VigenereCipher("TRAIN")
     encoded = cipher.encoded("ENCODED IN PYTHON")
     assert encoded == "XECWQXUIVCRKHWA"
 def test_encode_lowercase(self):
     cipher = VigenereCipher("TRain")
     encoded = cipher.encoded("encoded in Python")
     assert encoded == "XECWQXUIVCRKHWA"
 def test_extend_keyword(self):
     cipher = VigenereCipher("TRAIN")
     extended = cipher.extend_keyword(16)
     assert extended == "TRAINTRAINTRAINT"
 def test_separate_character(self):
     cipher = VigenereCipher("TRAIN")
     assert cipher.separate_character("X", "T") == "E"
     assert cipher.separate_character("E", "R") == "N"
 def test_encode_character(self):
     cipher = VigenereCipher("TRAIN")
     encoded = cipher.encode("E")
     assert encoded == "X"
 def test_combine_character(self):
     cipher = VigenereCipher("TRAIN")
     assert cipher.combine_character("E", "T") == "X"
     assert cipher.combine_character("N", "R") == "E"
示例#13
0
from MatrixCipher import MatrixCipher
from AtBashCipher import AtBashCipher
from XORCipher import XORCipher

if __name__ == "__main__":
    print("Cryptography")
    cc_encoded = CaesarCipher.encode(
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
    )
    print('Ceaser cipher encoded: ', cc_encoded)
    cc_decoded = CaesarCipher.decode(cc_encoded)
    print('Ceaser cipher decoded: ', cc_decoded)

    key = 'verysecrectkey'
    vc_encoded = VigenereCipher.encode(
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
        key)
    print('Vigenere cipher encoded: ', vc_encoded)
    vc_decoded = VigenereCipher.decode(vc_encoded, key)
    print('Vigenere cipher decoded: ', vc_decoded)

    mc_encoded = MatrixCipher.encode('Ala ma kota')
    mc_decoded = MatrixCipher.decode(mc_encoded)
    print('Matrix cipher encoded: ', mc_encoded)
    print('Matrix cipher decoded: ', mc_decoded)

    atc_encoded = AtBashCipher.encode('Ala ma kota')
    atc_decoded = AtBashCipher.decode(atc_encoded)
    print('AtBash cipher encoded: ', atc_encoded)
    print('AtBash cipher decoded: ', atc_decoded)