单表置换加解密

/*
0.3版本
该版本集成了caesar和单表置换。
修复了0.2版本的致命错误。
writed by wintersun 2014/10/27
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define size 500

char x[size],y[size];
int int_key;
char str_key[100];
char str_passtable[30];//单表置换的密码表。
int main()
{
	int meau();
	while(meau());
	exit(1);
}

int meau()
{
	void caesar_lock(char*x,char*y,int int_key);
	void caesar_unlock(char*x,char*y,int int_key);
	void displace_lock();
	void displace_unlock();
	void press();
	char c;
	printf("请输入您想要的操作。\n1.caesar加密\n2.caesar解密\n3.displace加密\n4.displace解密\n5.退出\n");
	c=getchar();
	switch (c)
	{
	case '1':
			system("cls");//先清屏
			printf("请输入您的int_key!\n");
			scanf("%d",&int_key);
			printf("请输入您的明文!\n");
			scanf("%s",x);
			caesar_lock(x,y,int_key);
			printf("密文是:%s\n",y);
			press();//用于等待按键。
			return 1;
	case '2':
			system("cls");
			printf("请输入您的int_key!\n");
			scanf("%d",&int_key);
			printf("请输入您的密文!\n");
			scanf("%s",&x);
			caesar_unlock(x,y,int_key);
			printf("明文是:%s\n",y);
			press();
			return 1;
	case '3':
			system("cls");
			printf("请输入您的str_key!\n");
			scanf("%s",str_key);
			printf("请输入您的明文!\n");
			scanf("%s",&x);
			displace_lock();
			printf("密文是:%s\n",y);
			press();
			return 1;
	case '4':
			system("cls");
			printf("请输入您的str_key!\n");
			scanf("%s",str_key);
			printf("请输入您的密文!\n");
			scanf("%s",&x);
			displace_unlock();
			printf("明文是:%s\n",y);
			press();
			return 1;
	case '5':
			return 0;
	default:
		printf("输入有误!\n");
		press();
		return 1;
	}

}


void caesar_lock(char*x,char*y,int int_key)//加密函数
{
	while(*x)
	{
		if(*x>='a'&&*x<='z')
		{
			*y='a'+(*x-'a'+int_key)%26;
			y++;
			x++;
			continue;
		}
		if(*x>='A'&&*x<='Z')
		{
			*y='A'+(*x-'A'+int_key)%26;
			y++;	
			x++;
			continue;
		}
		*y=*x;
		y++;
		x++;
	}
	*y='\0';
}

void caesar_unlock(char*x,char*y,int int_key)//解密函数
{
	while(*x)
	{
		if(*x>='a'&&*x<='z')
		{
			*y='a'+(*x-'a'-int_key+26)%26;
			y++;
			x++;
			continue;
		}
		if(*x>='A'&&*x<='Z')
		{
			*y='A'+(*x-'Z'-int_key+26)%26;
			y++;	
			x++;
			continue;
		}
		*y=*x;
		y++;
		x++;
	}
	*y='\0';
}

void displace_lock()
{
	//先过滤出密码表。
	int str_len=strlen(str_key);
	for(int i=0;i<str_len;i++)//转换为小写。
	{
		if(str_key[i]>='A'&&str_key[i]<='Z')
			str_key[i]+=32;
		if(x[i]>='A'&&x[i]<='Z')
			x[i]+=32;
	}
	str_len--;
	for(char a='a';a<='z';a++)//填充字母表。
		str_key[++str_len]=a;
	str_key[++str_len]='\0';
	str_len++;
	int len_table=0;
	int flag=0;
	str_passtable[0]=str_key[0];
	for(int j=1;j<str_len;j++)
	{
		flag=0;
		for(int i=0;i<=len_table;i++)
			if(str_key[j]==str_passtable[i])
			{
				flag=1;
				break;
			}
		if(flag==0)
		{
			str_passtable[++len_table]=str_key[j];
		}
	}
	//以上密码表制作完成,明文对照密文输出。
	int len_x=strlen(x);
	for(int i=0;i<len_x;i++)
		y[i]=str_passtable[x[i]-'a'];
	y[len_x]='\0';
}
char f(int i)
{
	for(int x=0;x<26;x++)
		if(str_passtable[x]==(i+'a'))return (x+'a');
}
void displace_unlock()
{
	//先过滤出密码表。
	int str_len=strlen(str_key);
	for(int i=0;i<str_len;i++)//转换为小写。
	{
		if(str_key[i]>='A'&&str_key[i]<='Z')
			str_key[i]+=32;
		if(x[i]>='A'&&x[i]<='Z')
			x[i]+=32;
	}
	str_len--;
	for(char a='a';a<='z';a++)//填充字母表。
		str_key[++str_len]=a;
	str_key[++str_len]='\0';
	str_len++;
	
	int len_table=0;
	int flag=0;
	str_passtable[0]=str_key[0];
	for(int j=1;j<str_len;j++)
	{
		flag=0;
		for(int i=0;i<=len_table;i++)
			if(str_key[j]==str_passtable[i])
			{
				flag=1;
				break;
			}
		if(flag==0)
		{
			str_passtable[++len_table]=str_key[j];
		}
	}
	//以上密码表制作完成,密文对照明文输出。
	char unpass[27];
	for(int i=0;i<26;i++)
		unpass[i]=f(i);
	//置换成解密表
	int len_x=strlen(x);
	for(int i=0;i<len_x;i++)
		y[i]=unpass[x[i]-'a'];
	y[len_x]='\0';
}

void press()//该函数用来等待用户按键并清屏。
{
	printf("please press any int_key to continue!\n");
	system("pause");
	system("cls");
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值