版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36278071/article/details/79888497
硬破解仿射密码
目录
仿射密码加解密公式
需要破解的原文
Ptfxgj Jnno-afv wn Htzaixojv Tjtxg
Af Yd Mqxzn
Kvif bdxvwsf X wtlv zf svtkv
Tp bdxvwsf tp X htzv qviv;
Bdxvwsf X rtkv jnno-afv
Wn wqv inpf hsndop xg wqv rvpwvig plf.
Wqv jnsovg rxssnrp af wqv ixkvipxov
Tiv fndgj aixovp xg wqv pvwwxgj pdg;
Wqvxi ivcsvhwxngp ng wqv pqxzzvixgj rtkvp
Tsrtfp sxgjvi xg wqv ovuwq nc zf qvtiw.
Wqv csntwxgj qvtiw jinrxgj xg wqv psdojv
Prtfp svxpdivsf dgovi wqv rtwvi;
Xg wqv jvgwsv rtkvp nc Htzaixojv
X rndso av t rtwvi ustgw!
Wqtw unns dgovi wqv pqtov nc vsz wivvp
Qnsop gnw rtwvi adw wqv itxganr cinz wqv plf;
Pqtwwvivo wn uxvhvp tzngj wqv odhlrvvop
Xp wqv pvoxzvgw nc t itxganr-sxlv oivtz.
Wn pvvl t oivtz? Edpw wn unsv t antw dupwivtz
Wn rqviv wqv jivvg jitpp xp zniv kviotgw;
Ni wn qtkv wqv antw cdssf sntovo rxwq pwtisxjqw
Tgo pxgj tsndo xg wqv pusvgoni nc pwtisxjqw.
Adw X htggnw pxgj tsndo
Bdxvwgvpp xp zf ctivrvss zdpxh;
Vkvg pdzzvi xgpvhwp qvtu pxsvghv cni zv
Pxsvgw xp Htzaixojv wngxjqw!
Kvif bdxvwsf X wtlv zf svtkv
Tp bdxvwsf tp X htzv qviv;
Jvgwsf X csxhl zf psvvkvp
Gnw vkvg t rxpu nc hsndo rxss X aixgj trtf
破解的算法分析
- 第一步:由仿射加密可知k必须与26互素;
- 第二步:k_1与k互为逆元,b可取任意值;
- 第三步:取不同的k和b进行遍历输出结果,观察出能够读懂的语句,则此语句对应的k和b就是正确的k和b;
- 第四步:使用网站输入对应的k和b,破解整篇文章的内容仿射加密网站。
C语言代码
C语言代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 26
//判断是否互素
int isPrimeNum(int a)
{
int i=2;
for(;i<=a;i++)
{
if(0==a%i && 0==N%i)
{
return 0;
}
}
return 1;
}
//获取a的逆元
int geta_(int a)
{
int i=0;
do{
i++;
}while(1!=a*i%N);
return i;
}
//解密算法
char *decode(char *p_str,int _a,int b,int n)
{
char c_str[40]="";
int j=0;
int i;
while('\0'!=*p_str)
{
if(*p_str>='A' && *p_str<='Z')
{
i=*p_str-'A';
i=((i-b+n)*_a)%n;
c_str[j]=i+'A';
}
else if(*p_str>='a' && *p_str<='z')
{
i=*p_str-'a';
i=((i-b+n)*_a)%n;
c_str[j]=i+'a';
}
else
{
c_str[j]=*p_str;
}
p_str++;
j++;
}
return &c_str[0];
}
int main()
{
int a=1,b=1,a_;
char p_str[40]="Ptfxgj Jnno-afv wn Htzaixojv Tjtxg";
char c_str[40]="";
for(;a<=25;a+=2)
{
if(0==isPrimeNum(a)){
continue;
}
else{
a_=geta_(a);
}
for(b=-10;b<=10;b++)
{
strcpy(c_str,decode(p_str,a_,b,N));
printf("%d %d",a,b);
printf("\t%s\n",c_str);
}
}
return 0;
}
python代码
##判断是否与26互素
def isPN(a):
for num in range(2,a):
if 0==a%num and 0==26%num:
return False
return True
##求得逆元a_
def get_a_(a):
i=1
while (1!=(i*a)%26):
i=i+1
return i
##解密函数
def decodeMstr(mStr,a_,b):
yStr=""
for Char in mStr:
if Char>='A' and Char<='Z':
Int=ord(Char)-ord('A')
Int=((Int-b+26)*a_)%26
yStr=yStr+chr(Int+ord('A'))
elif Char>='a' and Char<='z':
Int=ord(Char)-ord('a')
Int=((Int-b+26)*a_)%26
yStr=yStr+chr(Int+ord('a'))
else:
yStr=yStr+Char
return yStr
mStr="Ptfxgj Jnno-afv wn Htzaixojv Tjtxg"
for a in range(1,25,2):
if False==isPN(a):
continue
else:
a_=get_a_(a)
for b in range(-10,10):
yStr=decodeMstr(mStr,a_,b)
print(yStr,end="\t")
print(a,b)
效果展示
原文
Saying Good-bye to Cambridge Again
By Xu Zhimo
Very quietly I take my leave
As quietly as I came here;
Quietly I wave good-bye
To the rosy clouds in the western sky.
The golden willows by the riverside
Are young brides in the setting sun;
Their reflections on the shimmering waves
Always linger in the depth of my heart.
The floating heart growing in the sludge
Sways leisurely under the water;
In the gentle waves of Cambridge
I would be a water plant!
That pool under the shade of elm trees
Holds not water but the rainbow from the sky;
Shattered to pieces among the duckweeds
Is the sediment of a rainbow-like dream.
To seek a dream? Just to pole a boat upstream
To where the green grass is more verdant;
Or to have the boat fully loaded with starlight
And sing aloud in the splendor of starlight.
But I cannot sing aloud
Quietness is my farewell music;
Even summer insects heap silence for me
Silent is Cambridge tonight!
Very quietly I take my leave
As quietly as I came here;
Gently I flick my sleeves
Not even a wisp of cloud will I bring away