Exemple #1
0
class Affine(Cipher):
    """Implements cipher via the affine method"""
    def __init__(self, key, alphabet_size):
        super().__init__("Affine", alphabet_size, key)
        mkey, ckey = key
        self.multiplication = Multiplication(mkey, alphabet_size)
        self.caesar = Caesar(ckey, alphabet_size)

    def encode(self, message):
        caesared = self.caesar.encode(message)
        multiplied = self.multiplication.encode(caesared)
        return multiplied

    def decode(self, encrypted):
        multiplied = self.multiplication.decode(encrypted)
        caesared = self.caesar.decode(multiplied)
        return caesared

    def candidate_keys(self):
        caesar_candidates = self.caesar.candidate_keys()
        multiplication_candidates = self.multiplication.candidate_keys()

        return product(multiplication_candidates, caesar_candidates)

    @staticmethod
    def generate_key(alphabet_size):
        return Multiplication.generate_key(alphabet_size), Caesar.generate_key(
            alphabet_size)
Exemple #2
0
 def decode_chi(cipher):
     d2 = {}
     arr = []
     for j in range(26): 
         k = (26-j)%26
         temp = Caesar.decode(cipher,j)
         sum = FrequencyAnalyzer.chisquaredstatistic(temp)
         d2[k] = sum
     return sort_dict_by_values(d2)[:3]
Exemple #3
0
def run(params):


	function, infile, outfile, key = parser(params) 

	if(function==0):
		return help()
	else:

		cypher = Caesar(key,infile,outfile) # initialize object 
		cypher.encode() if(function==1) else cypher.decode()
Exemple #4
0
class Affine:
    """ Affine cipher """
    def __init__(self):
        self.caesar = Caesar()
        self.multiplication = Multiplication()

    def generate_keys(self):
        """ Generates encryption and decryption key """
        multiplication_encrypt, multiplication_decrypt = self.multiplication.generate_keys(
        )
        caesar_encrypt, caesar_decrypt = self.caesar.generate_keys()
        encryption_key = (multiplication_encrypt, caesar_encrypt)  # (48, 90)
        decryption_key = (multiplication_decrypt, caesar_decrypt)  # (2, 5)
        return encryption_key, decryption_key

    def encode(self, message, key):
        """ Encode message with given key """
        multiplication_key = key[0]
        caesar_key = key[1]

        # Encode with Multiplication then Caesar
        partially_encoded = self.multiplication.encode(message,
                                                       multiplication_key)
        encoded_message = self.caesar.encode(partially_encoded, caesar_key)

        return encoded_message

    def decode(self, encoded_message, decryption_keys):
        """ Decode the encoded message with the decryption key """
        multiplication_key = decryption_keys[0]
        caesar_key = decryption_keys[1]

        # Decode with Caesar then Multiplication
        partially_decoded = self.caesar.decode(encoded_message, caesar_key)
        decoded_message = self.multiplication.decode(partially_decoded,
                                                     multiplication_key)

        return decoded_message
Exemple #5
0
 def decode(self, cypher_text, receivers_key):
     caesar = Caesar()
     multiplicative = Multiplicative()
     multiplicative_cyphered = caesar.decode(cypher_text, receivers_key[1])
     plain_text = multiplicative.decode(multiplicative_cyphered, receivers_key[0])
     return plain_text