🥥Python密码线性方程组和频率分析
Python | 密码 | 线性方程组频率分析 | Matplotlib | Numpy | 模运算 | 最大公约数 (GCD) | 群论 | 伪随机散 | 素散定理 | 学生素性测试 | 费马小定理 | 米勒-拉宾素数检验 | 欧拉定理 | 伪随机性
本文将了解:模算术;最大公约数 (GCD) 的重要性;群论;伪随机数;创建用于频率分析的 Python 脚本。
模运算和最大公约数
商余定理指出,对于每个整数 $A$ 和正数 $B$,存在不同的整数 $Q$ 和 $R$,使得:$A=B^{*} Q+R, 0=<r=<b$。 当 $a=95$ 且 $\vec{b}=10$ 时,$q$(商)和 $r$(余数)的唯一值是多少? 你发现商等于 9,余数等于 5。
一旦你理解了商余定理,就更容易理解我们的密码数学的第一部分:模运算。
质数(素数)
密码学中的素数对于我们的加密方案的安全性至关重要。 素数分解,也称为整数分解,是一个用于保护公钥加密方案的数学问题。 这是通过使用极大的半素数来实现的,这些半素数是两个素数相乘的结果。 您可能还记得,素数是任何只能被 1 和自身整除的数。 第一个质数是2。
基本群论
在抽象代数和其他数学领域,群论研究称为群的代数结构。 群的概念是抽象代数的核心:其他熟悉的代数结构,如向量空间、环和域,都可以作为具有附加运算和公理的群来执行。 当您探索 Diffie-Hellman 和 RSA 加密系统时,群论开始发挥作用。
折叠目录
模逆
扩展最大公约数
欧拉定律
伪随机性
线性方程组
频率分析
test_str = "We hold these truths to be self-evident, that all men are
created equal, "
test_str += "that they are endowed by their Creator with certain
unalienable Rights, "
test_str += "that among these are Life, "
test_str += "Liberty and the pursuit of Happiness."
all_freq = {}
for i in test_str:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
print()
print ("Count of all characters in the provided text is :\\n " + str(all_freq))
from collections import Counter
test_str = "We hold these truths to be self-evident, that all men are created equal, "
test_str += "that they are endowed by their Creator with certain unalienable Rights, "
test_str += "that among these are Life, "
test_str += "Liberty and the pursuit of Happiness."
res = Counter(test_str)
Python密码分析
Python密码协议释秘
示例
import binascii
def text2int(msg):
print (msg)
msg = msg.encode()
hexstr = binascii.hexlify(msg)
print (hexstr)
back2hex = format(integer_m, 'x')
print (back2hex)
evenpad = ('0' * (len(back2hex) % 2)) + back2hex
plaintext = binascii.unhexlify(evenpad)
print (plaintext)
text2int("Hello World")
Python密码混淆数据
示例
import sys
import numpy as np
def cipher_encryption(plain, key):
len_chk = 0
if len(plain) % 2 != 0:
plain += "0"
len_chk = 1
# msg to matrices
row = 2
col = int(len(plain)/2)
msg2d = np.zeros((row, col), dtype=int)
itr1 = 0
itr2 = 0
for i in range(len(plain)):
if i%2 == 0:
msg2d[0][itr1]= int(ord(plain[i]) - 65)
itr1 += 1
else:
msg2d[1][itr2] = int(ord(plain[i]) - 65)
itr2 += 1
# finding multiplicative inverse
for i in range(26):
temp_inv = deter * i
if temp_inv % 26 == 1:
mul_inv = i
break
else:
continue
if mul_inv == -1:
print("Invalid key")
sys.exit()
encryp_text = ""
itr_count = int(len(plain)/2)
if len_chk == 0:
for i in range(itr_count):
temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1]
encryp_text += chr((temp1 % 26) + 65)
temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1]
encryp_text += chr((temp2 % 26) + 65)
else:
for i in range(itr_count-1):
temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1]
encryp_text += chr((temp1 % 26) + 65)
temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1]
encryp_text += chr((temp2 % 26) + 65)
print("Encrypted text: {}".format(encryp_text))
return encryp_text
def cipher_decryption(cipher, key):
# if message length is an odd number, place a zero at the end.
len_chk = 0
if len(cipher) % 2 != 0:
cipher += "0"
len_chk = 1
# msg to matrices
row = 2
col = int(len(cipher)/2)
msg2d = np.zeros((row, col), dtype=int)
itr1 = 0
itr2 = 0
for i in range(len(cipher)):
if i%2 == 0:
msg2d[0][itr1]= int(ord(cipher[i]) - 65)
itr1 += 1
else:
msg2d[1][itr2] = int(ord(cipher[i]) - 65)
itr2 += 1
Python流密码和分组密码
Python隐藏和读取加密图像
Python消息验证码
Python公钥基础设施
Python PKI-RSA和ECC加密消息
Last updated