c语言简单古典加密算法

c语言仿射密码和维吉尼亚密码

仿射密码

// An highlighted block
#include 
int N=26;
int exgcd(int a, int n) {
	int p = a, q = n;
	int x = 0, y = 1;
	int z = q / p;
	while (p != 1 && q != 1) {
		int t = p;
		p = q % p;
		q = t;
		t = y;
		y = x - y * z;
		x = t;
		z = q / p;
	}
	y %= n;
	if (y < 0) y += n;
	return y;
}
void main()
{   int a,b;
    char ch[1000];printf("输入明文");gets (ch);
	printf("输入秘钥ab\n");
    scanf("%d",&a);
	scanf("%d",&b);
	d=exgcd(a,26);
	int	i=0;
	char re[1000];
	
	while (ch [i]!= '\0')
	{
		if ((int)ch [i]>= 65 && (int)ch[i] <= 90)//判断字符是否介于
		{
			re[i] = (char)(((a*((int)ch [i]- 65) + b) % N) + 65);
			printf("%c",re[i]);
		}
		if ((int)ch [i]>= 97 && (int)ch [i]<= 122)//判断字符是否介于
		{
			re[i] = (char)(((a*((int)ch [i]- 97) + b) % N) + 97);
			printf("%c",re[i]);
		}
		else
			i++;//若不是字母则不加处理
		i++;
	}
	printf("\n");
	i=0;
	char ch1;
while (re[i]!='\0')
	{
		if ((int)re[i] >= 65 && (int)re [i]<= 90)
		{
			ch1 = (char)(((d*(((int)re [i]- 65) - b)+N) % N) + 65);
			printf("%c",ch1);
		}
		if ((int)re [i]>= 97 && (int)re [i]<= 122)
		{
			ch1 = (char)(((d*(((int)re [i]- 97) - b)+N) % N) + 97);
			printf("%c",ch1);
		}
		else
			i++;
		i++;
	}
printf("\n");

	

	
}



维吉尼亚密码

#include 
#include 
void main()
{
	char ch[1000],re[1000],key[1000],r;
	int key1[1000],n=0,k,m;
	int i=0;
	printf("输入密文");
	gets(ch);
	printf("输入密钥");
	gets(key);
	while(key[i]!='\0')
	{key1[i]=(int)key[i]-97;
     i++;
	}
   int c=strlen(ch);
//	printf("%d",c);printf("%d",i);
	int j;
	if(c%i>0)j=c/i+1;
	else j=c/i;
	//printf("%d",j);
	for(k=0;k=97?(char)(((int)re[n]-97-key1[m])%26+97):(char)(((int)re[n]-97-key1[m])%26+97+26);
			printf("%c",r);
				n++;
			}
				
			
		}
	}

}

新人发表慢慢改进

你可能感兴趣的:(c语言简单古典加密算法)