Exemple #1
0
    def encryptMessage(self, m) -> int:
        n, e = self.getPublicKeyPair()

        # if type(m) is str:
        #     message_int = self.convert_word_to_int(m)

        c = modular_Exponentiation(m, e, n)

        return c
Exemple #2
0
    def decryptMessage(self, c: int):
        message_int = modular_Exponentiation(c, self._d, self._n)
        # message_int_str = "0"+str(message_int)

        # decrypted_message = ""
        # step through message for every two  digits
        # for i in range(0, len(message_int_str), 2):
        #     subStr = message_int_str[i:i+2]
        #     decrypted_message += self._alphabet_cipher[subStr]

        return message_int
Exemple #3
0
    def encryptMessage(self, m) -> int:
        ''''
        Performs encryption on message
        If message is a string, perform string to int conversion
        Returns encrypted message c <int>
        '''
        n, e = self.getPublicKeyPair()

        if type(m) is str:
            m = self.convert_word_to_int(m)

        c = modular_Exponentiation(m, e, n)

        return c
Exemple #4
0
    def _calculate_decryption_exponent(self, p, q, e) -> int:
        '''
        Compute decryption exponent which is the modular inverse of e, 
        so e⋅d modφ(n) = 1. The inverse is calculated as d = λ modφ(n), 
        where λ is a result of extended Euclidean algorithm equation 
        e⋅λ+φ(n)⋅μ = gcd(e,φ(n))
        '''

        # calculate Euler totient φ
        φ_n = (p - 1) * (q - 1)

        # find modular inverse of the encryption exponent using the totient
        # this is our decryption exponent
        d = modular_Exponentiation(e, -1, φ_n)
        # d = modInverse(e, φ_n)

        return d
Exemple #5
0
    def decryptMessage(self, c: int, alphabetic=True):
        '''
        Decrypts message
        If original message was an integer and wasn't converted to an integer based on the alphabet cipher, 
        skip conversion step
        Returns (decrypted_message_int, decrypted_message_str)
        '''
        message_int = modular_Exponentiation(c, self._d, self._n)
        message_str = ""

        # conversion step
        if alphabetic:
            message_int_str = "0" + str(message_int)

            # step through message for every two  digits
            for i in range(0, len(message_int_str), 2):
                subStr = message_int_str[i:i + 2]
                message_str += self._alphabet_cipher[subStr]

        return (message_int, message_str)
Exemple #6
0
    def decryptMessage(self, c: int):
        message_int = modular_Exponentiation(c, self._d, self._n)

        return message_int
Exemple #7
0
    def encryptMessage(self, m) -> int:
        n, e = self.getPublicKeyPair()

        c = modular_Exponentiation(m, e, n)

        return c