c++代码执行效率比py 高效十几倍, 脑子有问题才会把c 转py. 加密算法特耗费资源,别转了,骚
第一段:
def Miyaolenth(c:str):
klen = 1
clen = len(c)
while True:
IC = [float()] * klen
avgIC = float(0)
for i in range(0, klen):
out = [int()] * 26
j = int(0)
while i + j * klen < clen:
out[(int)(ord(c[i + j * klen]) - ord('A'))] += 1
j += 1
e = float(0)
L = int(0)
for k in range(0, 26):
L += out[k]
L *= (L - 1)
for k in range(0, 26):
if out[k] != 0:
e = e + (out[k] * (out[k] - 1)) / L
IC[i] = e
for i in range(0, klen):
avgIC += IC[i]
avgIC /= klen
if avgIC >= 0.06:
break
else:
klen += 1
print("密钥长度为:" + klen)
第二段:
p = [0.082, 0.015, 0.028, 0.043, 0.127, 0.022, 0.02, 0.061, 0.07, 0.002, 0.008, 0.04, 0.024, 0.067, 0.075, 0.019, 0.001, 0.06, 0.063, 0.091, 0.028, 0.01, 0.023, 0.001, 0.02, 0.001]
key = [int()] * 100
for i in range(0, klen):
g = int(0)
for t in range(0, 26):
x = float(0)
out = [int()] * 26
j = 0
while i + j * klen < clen:
out[ord(c[i + j * klen]) - ord('A')] += 1
j += 1
L = int(0)
for k in range(0, 26):
L += out[k]
for k in range(0, 26):
x = x + p[k] * out[(k + g) % 26]
if x / L > 0.055:
key[i] = g
break
else:
g += 1
out_str = "加密密钥为:"
for i in range(0, klen):
out_str += chr(ord('a') + key[i])
print(out_str)
我没有数据,你自己测试一下吧
int inthestr(char *s,char ch)
{
while(*s!='\0'){
if(*s==ch)
return 1;
s++;
}
return 0;
}