//功能:仿射加密 密钥:[3][5][9][11][15][17][19][21][23]有逆元的数
例子:pku c=7,d=3 输出 :EVN
编辑环境:vc++6.0
include <stdio.h>
int str_len(char *str)
{
int len = 0;
while(*(str++))
len++;
return len;
}
int main(int argc, char *argv[])
{
char a[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char b[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char str[50];
int c,d;
printf("Please input the words :");
gets(str);
printf("Please input the number c [3][5][9][11][15][17][19][21][23] and d:");
scanf("%d %d",&c,&d);
int len = str_len(str);
//两层循环,明白排序的重要性了!!
for(int i = 0;i<len;i++)
for(int j = 0;j<26;j++)
{
if(str[i]==a[j])
str[i] = b[(c*j+d)%26];
else
str[i] = str[i];
}
printf("%s\n",str);
return 0;
}
//解密
#include <stdio.h>
int str_len(char *str)
{
int len = 0;
while(*(str++))
len++;
return len;
}
int mulny(int k)
{
int t,i;
for( i = 0;i<26;i++)
{
t=k*i;
if(t%26==1)
return i;
}
}
int main(int argc, char *argv[])
{
char a[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char b[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char str[50];
int c,d;
printf("Please input the words :");
gets(str);
printf("Please input the number c [3][5][9][11][15][17][19][21][23] and d:");
scanf("%d%d",&c,&d);
int p = mulny(c);
int len = str_len(str);
//理解凯撒密码!
for(int i = 0;i<len;i++)
for(int j = 0;j<26;j++)
{
if(str[i]==b[j])
str[i] = a[(p*(j-d))%26];//利用数组来完成替换,加密
else
str[i] = str[i];
}
printf("%s\n",str);
return 0;
}
/*
Today I write two passages about information security.Those both were that I wrote yesterday and the day before yesterday.Just now, I came back to my room,I took part in a activity about landscape,and I was enjoyable.I was writing a code about "密钥短语" all day.I am so tired now.Last night,the internet was broken off.This night ,I will release my program about "密钥短语" 。
*/