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)
{
return0;
}
}
return1;
}
//获取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';
}
elseif(*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);
}
}
return0;
}
python代码
##判断是否与26互素defisPN(a):for num in range(2,a):
if0==a%num and0==26%num:
returnFalsereturnTrue##求得逆元a_defget_a_(a):
i=1while (1!=(i*a)%26):
i=i+1return i
##解密函数defdecodeMstr(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):
ifFalse==isPN(a):
continueelse:
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 inthe western sky.
The golden willows bythe riverside
Are young brides inthe setting sun;
Their reflections onthe shimmering waves
Always linger inthe depth ofmy heart.
The floating heart growing inthe 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 butthe rainbow fromthe 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 wherethe green grass is more verdant;
Or to have the boat fully loaded with starlight
And sing aloud inthe splendor of starlight.
But I cannot sing aloud
Quietness ismy farewell music;
Even summer insects heap silence forme
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