def rsa_hijack_chinese(data, open_mix, e_value): data = int(crypto_tools.utf_decoder(data)) crypto_tools.cterm("output", f"Hijacking using chinese reminder method", "inf") tx_data = [data] open_mixes = [open_mix] for i in range(2, e_value + 1): tx_data.append(int(crypto_tools.utf_decoder(crypto_tools.get_data()))) open_mixes.append( int( crypto_tools.cterm('input', f'Enter {i} open(p * q) number: ', 'ans'))) result = 0 m_value = 1 for i in range(e_value): temp_m = 1 m_value *= open_mixes[i] for j in range(1, e_value): temp_m *= open_mixes[(i + j) % e_value] inverse_m = crypto_tools.inverse_modulo_numb(temp_m, open_mixes[i]) result += tx_data[i] * temp_m * inverse_m result = (result % m_value)**(1 / float(e_value)) return f"{result}"
def rsa_hijack_nokey(data, open_mix, e_value): data = int(crypto_tools.utf_decoder(data)) crypto_tools.cterm("output", f"Hijacking using nokey reading method", "inf") second_data = int(crypto_tools.utf_decoder(crypto_tools.get_data())) second_e_value = int( crypto_tools.cterm('input', 'Enter second open(e) number: ', 'ans')) gcd, r, s = crypto_tools.EGCD(e_value, second_e_value) c1_r = pow(data, r) % open_mix c2_s = pow(crypto_tools.inverse_modulo_numb(second_data, open_mix), -s) % open_mix result = (c1_r * c2_s) % open_mix return f"{result}"
def dummy_rsa(data): data = int(crypto_tools.utf_decoder(data)) e_value = int(crypto_tools.cterm('input', 'Enter open(e) value: ', 'ans')) p_value = int(crypto_tools.cterm('input', 'Enter first(p) number: ', 'ans')) q_value = int( crypto_tools.cterm('input', 'Enter second(q) number: ', 'ans')) return dummy_rsa_processing(data, p_value, q_value, e_value)
def vigenere(data): lang = crypto_tools.cterm('input', 'Data language: ', 'ans') key = crypto_tools.cterm('input', 'Enter key(str): ', 'ans') encrypt = crypto_tools.cterm('input', 'You want encrypt or decrypt: ', 'ans') if encrypt != "encrypt" and encrypt != "decrypt": raise ValueError("Incorrect action") data = crypto_tools.utf_decoder(data) return vigenere_processing(data, key, lang, encrypt)
def hill_processing(data, key, encrypt, hill_dict): data = crypto_tools.utf_decoder(data) mtx_side_size = ceil(sqrt(len(key))) key_mtx = gen_key_mtx(key, hill_dict, mtx_side_size) if encrypt == "decrypt": key_mtx = crypto_tools.inverse_mtx(key_mtx, True, len(hill_dict)) data_mtx = gen_data_mtx(data, hill_dict, mtx_side_size) res_mtx = crypto_tools.mtx_mult(key_mtx, data_mtx) result = mtx_to_str(res_mtx, hill_dict) return result
def freq_analys_processing(data, lang): data = crypto_tools.utf_decoder(data) laters = form_frequency_dict(data) langs = crypto_tools.download_json("crypto_storage/freqchars.json") if lang == "no": for lang in langs: check_char(laters, langs[lang], lang) else: check_char(laters, langs[lang], lang) return laters
def caesar_processing(data, lang, key): caesar_dict = crypto_tools.get_param_json_data("alphabets.json", lang) result = "" data = crypto_tools.utf_decoder(data) for char in data: try: index = caesar_dict.index(char) except ValueError as err: err_str = "There is no " + char + " character in alphabet" raise ValueError(err_str) from err index = (index + key) % len(caesar_dict) result = result + caesar_dict[index] return result
def elgamal(data): data = crypto_tools.utf_decoder(data) p_value = int(crypto_tools.cterm('input', 'Enter first(p) number: ', 'ans')) g_value = int( crypto_tools.cterm('input', 'Enter generator(g) number: ', 'ans')) x_value = int( crypto_tools.cterm('input', 'Enter closed(x) number: ', 'ans')) encrypt = crypto_tools.cterm('input', 'You want encrypt or decrypt: ', 'ans') if encrypt not in ("encrypt", "decrypt"): raise ValueError(f"Incorrect action {encrypt}") return elgamal_processing(data, p_value, x_value, encrypt, g_value)
def dsa(data): data = crypto_tools.utf_decoder(data) p_value = int(crypto_tools.cterm('input', 'Enter first(p) number: ', 'ans')) q_value = int( crypto_tools.cterm('input', 'Enter second(q) number: ', 'ans')) x_value = int( crypto_tools.cterm('input', 'Enter secret key(x) number: ', 'ans')) k_value = int( crypto_tools.cterm('input', 'Enter secret key(k) number: ', 'ans')) encrypt = crypto_tools.cterm('input', 'You want sign or verify: ', 'ans') if encrypt not in ("sign", "verify"): raise ValueError(f"Incorrect action {encrypt}") return dsa_processing(data, p_value, q_value, x_value, k_value, encrypt)
def random_processing(data, action): try: data = crypto_tools.utf_decoder(data) data = loads(data) if action == "calc": for i in range(len(data)): nrp_seq, bit_seq = calc(data[i]['m'], data[i]['c'], data[i]['a'], data[i]['f'], 20000) data[i]["sequence"] = nrp_seq data[i]["bin_sequence"] = "" for num in bit_seq: data[i]["bin_sequence"] += "{0:b}".format(num) elif action == "generate": data = generate(data) else: raise ValueError("Incorrect action") except (KeyError, TypeError, JSONDecodeError): raise ValueError("Incorrect input") return dumps(data, sort_keys=True, indent=4)
def elliptic(data): data = crypto_tools.utf_decoder(data) elliptic_curve = crypto_tools.cterm('input', 'Enter curve coefficients(a:b:p): ', 'ans') elliptic_curve = crypto_tools.decode_params(elliptic_curve, 3) g_value = crypto_tools.cterm('input', 'Enter generator point(x:y): ', 'ans') g_value = crypto_tools.decode_params(g_value, 2) secret_key = int(crypto_tools.cterm('input', 'Enter secret key: ', 'ans')) r_number = int(crypto_tools.cterm('input', 'Enter r number: ', 'ans')) encrypt = crypto_tools.cterm('input', 'You want encrypt or decrypt: ', 'ans') if encrypt not in ("encrypt", "decrypt"): raise ValueError(f"Incorrect action {encrypt}") return elliptic_processing(data, elliptic_curve, g_value, secret_key, r_number, encrypt)