|
| 1 | +""" |
| 2 | +Encryption/Decryption Algorithm, conceived and implemented by: ALAOUI Mehdi 2013 |
| 3 | +Code reviewed/cleaned: 2018 |
| 4 | +""" |
| 5 | +import random |
| 6 | + |
| 7 | +''' |
| 8 | +RandomEncryption object is an 'API' to encrypt/decrypt string |
| 9 | +useable methods: |
| 10 | + - encrypt(text, filename): returns a string containing encrypted value of 'text' |
| 11 | + if filename specified, will write the result in it. |
| 12 | + - encrypt_from_file(input_filename, output_filename): same use as 'encrypt' method, the first argument is a filename in which the text to encrypt will be read. |
| 13 | +
|
| 14 | + - decrypt(text, filename): same use as 'encrypt' method, for decrypting. |
| 15 | + - decrypt_from_file(input_filename, output_filename): same use as 'encrypt_from_file', for decrypting. |
| 16 | +''' |
| 17 | + |
| 18 | +class RandomEncryption: |
| 19 | + |
| 20 | + def decrypt_from_file(self, input_filename: str, output_filename: str = None) -> str: |
| 21 | + text_to_decrypt = self._read(input_filename) |
| 22 | + return self.decrypt(text_to_decrypt, output_filename) |
| 23 | + |
| 24 | + |
| 25 | + def decrypt(self, text_to_decrypt: str, output_filename: str = None) -> str: |
| 26 | + decrypted_text = '' |
| 27 | + while len(text_to_decrypt) > 0: |
| 28 | + i = 1 |
| 29 | + while i < len(text_to_decrypt) and ord(text_to_decrypt[i]) < 97: |
| 30 | + i += 1 |
| 31 | + encrypted_sequence, text_to_decrypt = text_to_decrypt[1:i], text_to_decrypt[i:] |
| 32 | + first_basis, second_basis = ( |
| 33 | + self._string_hex_to_int(encrypted_sequence[0]), |
| 34 | + self._string_hex_to_int(encrypted_sequence[1]), |
| 35 | + ) |
| 36 | + char_to_decrypt = encrypted_sequence[2:] |
| 37 | + decrypted_char = self._revert_to_decimal_from_basis(first_basis, |
| 38 | + self._revert_to_decimal_from_basis(second_basis, char_to_decrypt) |
| 39 | + ) |
| 40 | + decrypted_text += chr(int(decrypted_char)) |
| 41 | + if output_filename: |
| 42 | + self._write(output_filename, decrypted_text) |
| 43 | + return decrypted_text |
| 44 | + |
| 45 | + def encrypt_from_file(self, input_filename: str, output_filename: str = None) -> str: |
| 46 | + text_to_encrypt = self._read(input_filename) |
| 47 | + return self.encrypt(text_to_encrypt, output_filename) |
| 48 | + |
| 49 | + def encrypt(self, text_to_encrypt: str, output_filename: str = None) -> str: |
| 50 | + encrypted_text = '' |
| 51 | + for char in text_to_encrypt: |
| 52 | + first_basis, second_basis = self._get_random_int(2, 10), self._get_random_int(2, 16) |
| 53 | + delimiter = self._get_random_char() |
| 54 | + encrypted_char = self._revert_from_decimal_to_basis(second_basis, int( |
| 55 | + self._revert_from_decimal_to_basis(first_basis, ord(char)) |
| 56 | + )) |
| 57 | + first_basis = self._get_hex_value(first_basis) |
| 58 | + second_basis = self._get_hex_value(second_basis) |
| 59 | + encrypted_text += delimiter + first_basis + second_basis + encrypted_char |
| 60 | + if output_filename: |
| 61 | + self._write(output_filename, encrypted_text) |
| 62 | + return encrypted_text |
| 63 | + |
| 64 | + def _get_random_char(self, uppercase: bool = False): |
| 65 | + _ascii = self._get_random_int(65, 90) if uppercase else self._get_random_int(97, 122) |
| 66 | + return chr(_ascii) |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def _get_random_int(min_value: int, max_value: int) -> int: |
| 70 | + return random.randint(min_value, max_value) |
| 71 | + |
| 72 | + def _revert_from_decimal_to_basis(self, final_basis: int, number: int) -> str: |
| 73 | + output_value = '' |
| 74 | + while number > 0: |
| 75 | + (rest, number) = (number % final_basis, number // final_basis) |
| 76 | + rest = self._get_hex_value(rest) |
| 77 | + output_value = rest + output_value |
| 78 | + return output_value |
| 79 | + |
| 80 | + def _revert_to_decimal_from_basis(self, initial_basis: int, number: int) -> str: |
| 81 | + number, n = str(number), len(number) |
| 82 | + output_value = 0 |
| 83 | + for index, digit in enumerate(number): |
| 84 | + digit_value = self._string_hex_to_int(digit) |
| 85 | + output_value += digit_value * (initial_basis ** (n - 1 - index)) |
| 86 | + return str(output_value) |
| 87 | + |
| 88 | + @staticmethod |
| 89 | + def _string_hex_to_int(hexa_value: str) -> int: |
| 90 | + ''' |
| 91 | + Returns integer value of a hexa string. Ex. '1' -> 1, 'B' -> 11. |
| 92 | + ''' |
| 93 | + return int(hexa_value) if ord(hexa_value) < 65 else (ord(hexa_value) - 55) |
| 94 | + |
| 95 | + @staticmethod |
| 96 | + def _get_hex_value(int_value: int) -> str: |
| 97 | + return str(int_value) if int_value < 10 else chr(int_value + 55) |
| 98 | + |
| 99 | + @staticmethod |
| 100 | + def _read(filename): |
| 101 | + _file = open(filename, 'r') |
| 102 | + file_content = _file.read() |
| 103 | + _file.close() |
| 104 | + return file_content |
| 105 | + |
| 106 | + @staticmethod |
| 107 | + def _write(filename, value): |
| 108 | + _file = open(filename, 'w') |
| 109 | + _file.write(value) |
| 110 | + _file.close() |
0 commit comments