cryptosploit_modules.symmetric.affine
View Source
0from math import gcd 1from os.path import isfile 2from re import compile 3 4from cryptosploit.cprint import Printer 5from cryptosploit_modules import BaseModule 6 7 8class Affine(BaseModule): 9 def __init__(self): 10 super().__init__() 11 self.env.check_var = self.check_var 12 13 def check_var(self, name, value): 14 match name: 15 case "key": 16 if not value.isdigit(): 17 return False, "Value must be a natural number!" 18 if ( 19 gcd( 20 len(self.env.get_var("alphabet").value), 21 int(value), 22 ) 23 != 1 24 ): 25 return False, "Key must be coprime with alphabet length" 26 case "offset": 27 if not value.isdigit(): 28 return False, "Value must be a natural number!" 29 case "mode": 30 if value not in ("decrypt", "encrypt", "attack"): 31 return False, "No such mode!" 32 case "alphabet": 33 if ( 34 gcd( 35 len(value), 36 int(self.env.get_var("key").value), 37 ) 38 != 1 39 ): 40 return False, "Alphabet length must be coprime with key" 41 return True, "" 42 43 def encrypt(self): 44 result = "" 45 key = int(self.env.get_var("key").value) 46 offset = int(self.env.get_var("offset").value) 47 alphabet = self.env.get_var("alphabet").value.upper() 48 inp = self.env.get_var("input").value 49 if isfile(inp): 50 with open(inp) as f: 51 inp = f.read() 52 for ind, char in enumerate(inp.upper()): 53 if char in alphabet: 54 res_char = alphabet[ 55 (key * alphabet.find(char) + offset) % len(alphabet) 56 ] 57 result += res_char if inp[ind].isupper() else res_char.lower() 58 else: 59 result += char 60 return result 61 62 def decrypt(self): 63 result = "" 64 key = int(self.env.get_var("key").value) 65 offset = int(self.env.get_var("offset").value) 66 alphabet = self.env.get_var("alphabet").value.upper() 67 key = pow(key, -1, len(alphabet)) 68 inp = self.env.get_var("input").value 69 if isfile(inp): 70 with open(inp) as f: 71 inp = f.read() 72 for ind, char in enumerate(inp.upper()): 73 if char in alphabet: 74 res_char = alphabet[ 75 (key * (alphabet.find(char) - offset)) % len(alphabet) 76 ] 77 result += res_char if inp[ind].isupper() else res_char.lower() 78 else: 79 result += char 80 return result 81 82 def attack(self): 83 results = [] 84 contains = self.env.get_var("contains").value 85 if isfile(contains): 86 with open(contains) as f: 87 contains = f.read() 88 pattern = compile(contains or ".*") 89 alphabet = self.env.get_var("alphabet").value.upper() 90 prev_key, prev_offset = ( 91 self.env.get_var("key").value, 92 self.env.get_var("offset").value, 93 ) 94 for key in range(1, len(alphabet)): 95 if gcd(key, len(alphabet)) == 1: 96 self.env.set_var("key", str(key)) 97 for offset in range(len(alphabet)): 98 self.env.set_var("offset", str(offset)) 99 if pattern.match(r := self.decrypt()): 100 results.append(r) 101 self.env.set_var("key", prev_key) 102 self.env.set_var("offset", prev_offset) 103 return "\n".join(set(results)) 104 105 def run(self): 106 func = getattr(self, self.env.get_var("mode").value) 107 result = func() 108 if result: 109 Printer.positive("Result:\n" + result) 110 else: 111 Printer.negative("Result:\nNone") 112 113 114module = Affine
View Source
9class Affine(BaseModule): 10 def __init__(self): 11 super().__init__() 12 self.env.check_var = self.check_var 13 14 def check_var(self, name, value): 15 match name: 16 case "key": 17 if not value.isdigit(): 18 return False, "Value must be a natural number!" 19 if ( 20 gcd( 21 len(self.env.get_var("alphabet").value), 22 int(value), 23 ) 24 != 1 25 ): 26 return False, "Key must be coprime with alphabet length" 27 case "offset": 28 if not value.isdigit(): 29 return False, "Value must be a natural number!" 30 case "mode": 31 if value not in ("decrypt", "encrypt", "attack"): 32 return False, "No such mode!" 33 case "alphabet": 34 if ( 35 gcd( 36 len(value), 37 int(self.env.get_var("key").value), 38 ) 39 != 1 40 ): 41 return False, "Alphabet length must be coprime with key" 42 return True, "" 43 44 def encrypt(self): 45 result = "" 46 key = int(self.env.get_var("key").value) 47 offset = int(self.env.get_var("offset").value) 48 alphabet = self.env.get_var("alphabet").value.upper() 49 inp = self.env.get_var("input").value 50 if isfile(inp): 51 with open(inp) as f: 52 inp = f.read() 53 for ind, char in enumerate(inp.upper()): 54 if char in alphabet: 55 res_char = alphabet[ 56 (key * alphabet.find(char) + offset) % len(alphabet) 57 ] 58 result += res_char if inp[ind].isupper() else res_char.lower() 59 else: 60 result += char 61 return result 62 63 def decrypt(self): 64 result = "" 65 key = int(self.env.get_var("key").value) 66 offset = int(self.env.get_var("offset").value) 67 alphabet = self.env.get_var("alphabet").value.upper() 68 key = pow(key, -1, len(alphabet)) 69 inp = self.env.get_var("input").value 70 if isfile(inp): 71 with open(inp) as f: 72 inp = f.read() 73 for ind, char in enumerate(inp.upper()): 74 if char in alphabet: 75 res_char = alphabet[ 76 (key * (alphabet.find(char) - offset)) % len(alphabet) 77 ] 78 result += res_char if inp[ind].isupper() else res_char.lower() 79 else: 80 result += char 81 return result 82 83 def attack(self): 84 results = [] 85 contains = self.env.get_var("contains").value 86 if isfile(contains): 87 with open(contains) as f: 88 contains = f.read() 89 pattern = compile(contains or ".*") 90 alphabet = self.env.get_var("alphabet").value.upper() 91 prev_key, prev_offset = ( 92 self.env.get_var("key").value, 93 self.env.get_var("offset").value, 94 ) 95 for key in range(1, len(alphabet)): 96 if gcd(key, len(alphabet)) == 1: 97 self.env.set_var("key", str(key)) 98 for offset in range(len(alphabet)): 99 self.env.set_var("offset", str(offset)) 100 if pattern.match(r := self.decrypt()): 101 results.append(r) 102 self.env.set_var("key", prev_key) 103 self.env.set_var("offset", prev_offset) 104 return "\n".join(set(results)) 105 106 def run(self): 107 func = getattr(self, self.env.get_var("mode").value) 108 result = func() 109 if result: 110 Printer.positive("Result:\n" + result) 111 else: 112 Printer.negative("Result:\nNone")
View Source
14 def check_var(self, name, value): 15 match name: 16 case "key": 17 if not value.isdigit(): 18 return False, "Value must be a natural number!" 19 if ( 20 gcd( 21 len(self.env.get_var("alphabet").value), 22 int(value), 23 ) 24 != 1 25 ): 26 return False, "Key must be coprime with alphabet length" 27 case "offset": 28 if not value.isdigit(): 29 return False, "Value must be a natural number!" 30 case "mode": 31 if value not in ("decrypt", "encrypt", "attack"): 32 return False, "No such mode!" 33 case "alphabet": 34 if ( 35 gcd( 36 len(value), 37 int(self.env.get_var("key").value), 38 ) 39 != 1 40 ): 41 return False, "Alphabet length must be coprime with key" 42 return True, ""
View Source
44 def encrypt(self): 45 result = "" 46 key = int(self.env.get_var("key").value) 47 offset = int(self.env.get_var("offset").value) 48 alphabet = self.env.get_var("alphabet").value.upper() 49 inp = self.env.get_var("input").value 50 if isfile(inp): 51 with open(inp) as f: 52 inp = f.read() 53 for ind, char in enumerate(inp.upper()): 54 if char in alphabet: 55 res_char = alphabet[ 56 (key * alphabet.find(char) + offset) % len(alphabet) 57 ] 58 result += res_char if inp[ind].isupper() else res_char.lower() 59 else: 60 result += char 61 return result
View Source
63 def decrypt(self): 64 result = "" 65 key = int(self.env.get_var("key").value) 66 offset = int(self.env.get_var("offset").value) 67 alphabet = self.env.get_var("alphabet").value.upper() 68 key = pow(key, -1, len(alphabet)) 69 inp = self.env.get_var("input").value 70 if isfile(inp): 71 with open(inp) as f: 72 inp = f.read() 73 for ind, char in enumerate(inp.upper()): 74 if char in alphabet: 75 res_char = alphabet[ 76 (key * (alphabet.find(char) - offset)) % len(alphabet) 77 ] 78 result += res_char if inp[ind].isupper() else res_char.lower() 79 else: 80 result += char 81 return result
View Source
83 def attack(self): 84 results = [] 85 contains = self.env.get_var("contains").value 86 if isfile(contains): 87 with open(contains) as f: 88 contains = f.read() 89 pattern = compile(contains or ".*") 90 alphabet = self.env.get_var("alphabet").value.upper() 91 prev_key, prev_offset = ( 92 self.env.get_var("key").value, 93 self.env.get_var("offset").value, 94 ) 95 for key in range(1, len(alphabet)): 96 if gcd(key, len(alphabet)) == 1: 97 self.env.set_var("key", str(key)) 98 for offset in range(len(alphabet)): 99 self.env.set_var("offset", str(offset)) 100 if pattern.match(r := self.decrypt()): 101 results.append(r) 102 self.env.set_var("key", prev_key) 103 self.env.set_var("offset", prev_offset) 104 return "\n".join(set(results))
View Source
Required to be overridden in the child class. Function called by the user
Inherited Members
View Source
9class Affine(BaseModule): 10 def __init__(self): 11 super().__init__() 12 self.env.check_var = self.check_var 13 14 def check_var(self, name, value): 15 match name: 16 case "key": 17 if not value.isdigit(): 18 return False, "Value must be a natural number!" 19 if ( 20 gcd( 21 len(self.env.get_var("alphabet").value), 22 int(value), 23 ) 24 != 1 25 ): 26 return False, "Key must be coprime with alphabet length" 27 case "offset": 28 if not value.isdigit(): 29 return False, "Value must be a natural number!" 30 case "mode": 31 if value not in ("decrypt", "encrypt", "attack"): 32 return False, "No such mode!" 33 case "alphabet": 34 if ( 35 gcd( 36 len(value), 37 int(self.env.get_var("key").value), 38 ) 39 != 1 40 ): 41 return False, "Alphabet length must be coprime with key" 42 return True, "" 43 44 def encrypt(self): 45 result = "" 46 key = int(self.env.get_var("key").value) 47 offset = int(self.env.get_var("offset").value) 48 alphabet = self.env.get_var("alphabet").value.upper() 49 inp = self.env.get_var("input").value 50 if isfile(inp): 51 with open(inp) as f: 52 inp = f.read() 53 for ind, char in enumerate(inp.upper()): 54 if char in alphabet: 55 res_char = alphabet[ 56 (key * alphabet.find(char) + offset) % len(alphabet) 57 ] 58 result += res_char if inp[ind].isupper() else res_char.lower() 59 else: 60 result += char 61 return result 62 63 def decrypt(self): 64 result = "" 65 key = int(self.env.get_var("key").value) 66 offset = int(self.env.get_var("offset").value) 67 alphabet = self.env.get_var("alphabet").value.upper() 68 key = pow(key, -1, len(alphabet)) 69 inp = self.env.get_var("input").value 70 if isfile(inp): 71 with open(inp) as f: 72 inp = f.read() 73 for ind, char in enumerate(inp.upper()): 74 if char in alphabet: 75 res_char = alphabet[ 76 (key * (alphabet.find(char) - offset)) % len(alphabet) 77 ] 78 result += res_char if inp[ind].isupper() else res_char.lower() 79 else: 80 result += char 81 return result 82 83 def attack(self): 84 results = [] 85 contains = self.env.get_var("contains").value 86 if isfile(contains): 87 with open(contains) as f: 88 contains = f.read() 89 pattern = compile(contains or ".*") 90 alphabet = self.env.get_var("alphabet").value.upper() 91 prev_key, prev_offset = ( 92 self.env.get_var("key").value, 93 self.env.get_var("offset").value, 94 ) 95 for key in range(1, len(alphabet)): 96 if gcd(key, len(alphabet)) == 1: 97 self.env.set_var("key", str(key)) 98 for offset in range(len(alphabet)): 99 self.env.set_var("offset", str(offset)) 100 if pattern.match(r := self.decrypt()): 101 results.append(r) 102 self.env.set_var("key", prev_key) 103 self.env.set_var("offset", prev_offset) 104 return "\n".join(set(results)) 105 106 def run(self): 107 func = getattr(self, self.env.get_var("mode").value) 108 result = func() 109 if result: 110 Printer.positive("Result:\n" + result) 111 else: 112 Printer.negative("Result:\nNone")
View Source
14 def check_var(self, name, value): 15 match name: 16 case "key": 17 if not value.isdigit(): 18 return False, "Value must be a natural number!" 19 if ( 20 gcd( 21 len(self.env.get_var("alphabet").value), 22 int(value), 23 ) 24 != 1 25 ): 26 return False, "Key must be coprime with alphabet length" 27 case "offset": 28 if not value.isdigit(): 29 return False, "Value must be a natural number!" 30 case "mode": 31 if value not in ("decrypt", "encrypt", "attack"): 32 return False, "No such mode!" 33 case "alphabet": 34 if ( 35 gcd( 36 len(value), 37 int(self.env.get_var("key").value), 38 ) 39 != 1 40 ): 41 return False, "Alphabet length must be coprime with key" 42 return True, ""
View Source
44 def encrypt(self): 45 result = "" 46 key = int(self.env.get_var("key").value) 47 offset = int(self.env.get_var("offset").value) 48 alphabet = self.env.get_var("alphabet").value.upper() 49 inp = self.env.get_var("input").value 50 if isfile(inp): 51 with open(inp) as f: 52 inp = f.read() 53 for ind, char in enumerate(inp.upper()): 54 if char in alphabet: 55 res_char = alphabet[ 56 (key * alphabet.find(char) + offset) % len(alphabet) 57 ] 58 result += res_char if inp[ind].isupper() else res_char.lower() 59 else: 60 result += char 61 return result
View Source
63 def decrypt(self): 64 result = "" 65 key = int(self.env.get_var("key").value) 66 offset = int(self.env.get_var("offset").value) 67 alphabet = self.env.get_var("alphabet").value.upper() 68 key = pow(key, -1, len(alphabet)) 69 inp = self.env.get_var("input").value 70 if isfile(inp): 71 with open(inp) as f: 72 inp = f.read() 73 for ind, char in enumerate(inp.upper()): 74 if char in alphabet: 75 res_char = alphabet[ 76 (key * (alphabet.find(char) - offset)) % len(alphabet) 77 ] 78 result += res_char if inp[ind].isupper() else res_char.lower() 79 else: 80 result += char 81 return result
View Source
83 def attack(self): 84 results = [] 85 contains = self.env.get_var("contains").value 86 if isfile(contains): 87 with open(contains) as f: 88 contains = f.read() 89 pattern = compile(contains or ".*") 90 alphabet = self.env.get_var("alphabet").value.upper() 91 prev_key, prev_offset = ( 92 self.env.get_var("key").value, 93 self.env.get_var("offset").value, 94 ) 95 for key in range(1, len(alphabet)): 96 if gcd(key, len(alphabet)) == 1: 97 self.env.set_var("key", str(key)) 98 for offset in range(len(alphabet)): 99 self.env.set_var("offset", str(offset)) 100 if pattern.match(r := self.decrypt()): 101 results.append(r) 102 self.env.set_var("key", prev_key) 103 self.env.set_var("offset", prev_offset) 104 return "\n".join(set(results))
View Source
Required to be overridden in the child class. Function called by the user