|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本人刚接触数据结构与算法,写了个自认为还可以的加密与解密的代码希望和大家一起分享这种快乐。
这代码仅仅针对26个小写字母的加密与解密,如果有兴趣的朋友可以改成支持大小写字母的或者支持整个ASCII的也可以,应该不难吧,毕竟1字节==2^8......呵呵
源码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define TSIZE 27
#define SIZE 26
struct node//存储用户输入数据
{
int mishi;
char ch;
struct node* next;
}L_size;
typedef struct node *Ptr;
typedef Ptr List;
typedef Ptr position;
struct table//字母表数据结构
{
char w;
struct table *next;
struct node * pioneer;
}T_size;
typedef struct table *wPtr;
typedef wPtr t_list;
typedef wPtr t_position;
t_list CreatWord(void);//创建一个小写英文字母表
List CreatList(char *str);//创建一个具有存储密文密匙功能的单链表
List AddPassword(List L);//给输入的密文随机加密
List ExplainList(char *strw);//给输入的密文解密,同时输入密文和密匙
void PrintList(List L);//打印密文或者明文
void PrintMdata(List L);//打印密匙或密码
int main(void)
{
srand(time(0));
char s[1000]={'\0'},c;
List tmp,tmp1;
printf("\t\t\t维吉尼亚加密与解密\n");
printf("1.加密明文\t\t2.打印已加密明文的密匙和密文\n");
printf("3.解密密文并打印\t4.退出程序\n请输入操作数:");
c=getchar();
while(c!='4')
{
switch(c)
{
case '1':
printf("请输入明文:");
scanf("%s",s);
tmp=CreatList(s);
tmp=AddPassword(tmp);
break;
case '2':
printf("密文:");
PrintList(tmp);
printf("\n");
printf("密匙:");
PrintMdata(tmp);
break;
case '3':
printf("请输入密文:");
scanf("%s",s);
tmp1=ExplainList(s);
PrintList(tmp1);
break;
default:
printf("输入错误,重新输入操作数!\n");
break;
}
printf("请输入操作数:");
c=getchar();
c=getchar();
}
return 0;
}
t_list CreatWord(void)
{
int c1;
char c2='a';
t_list head,r;
t_position p;
head=(t_list)malloc(sizeof(T_size));
if(!head)
{
printf("No Space In t_head!\n");
return NULL;
}
r=head;
for(c1=0;c1<SIZE;c1++)
{
p=(t_list)malloc(sizeof(T_size));
if(!p)
{
printf("No Space In tS!\n");
return NULL;
}
p->w=c2;
r->next=p;
p->pioneer=r;
r=p;
c2++;
}
r->next=head->next;
head->next->pioneer=r;
return (head->next);
}
List CreatList(char *str)
{
int ca,n=strlen(str);
List head,r;
position p;
head=(List)malloc(sizeof(L_size));
if(!head)
{
printf("No Space In head!\n");
return NULL;
}
r=head;
for(ca=0;ca<n;ca++)
{
p=(List)malloc(sizeof(L_size));
if(!p)
{
printf("No Space In S!\n");
return NULL;
}
r->next=p;
p->ch=str[ca];
r=p;
}
r->next=NULL;
return head;
}
List AddPassword(List L)
{
int j,cA;
position Ap;
t_position tp=CreatWord();
Ap=L->next;
if(!Ap)
{
printf("链表无效 in AddPassword();!!\n");
return NULL;
}
while(Ap&&tp)
{
Ap->mishi=rand()%100+1;
j=Ap->mishi;
j%=SIZE;
while(Ap->ch!=tp->w)
tp=tp->next;
for(cA=0;cA<j;cA++)
tp=tp->next;
Ap->ch=tp->w;
Ap=Ap->next;
}
return(L);
}
void PrintList(List L)
{
position p=L->next;
if(!p)
{
printf("链表无效 in PrintList();!!\n");
}
while(p)
{
printf("%c ",p->ch);
p=p->next;
}
printf("\n");
}
void PrintMdata(List L)
{
position p=L->next;
if(!p)
{
printf("链表无效 in PrintMdata();!!\n");
}
while(p)
{
printf("%d ",p->mishi);
p=p->next;
}
printf("\n");
}
List ExplainList(char *strw)
{
List Tmp;
position p;
t_position tp;
int i,cE;
tp=CreatWord();
Tmp=CreatList(strw);
p=Tmp->next;
printf("请输入密匙:");
while(p&&tp)
{
scanf("%d",&(p->mishi));
i=p->mishi;
i%=SIZE;
while(p->ch!=tp->w)
tp=tp->next;
for(cE=0;cE<i;cE++)
tp=tp->pioneer;
p->ch=tp->w;
p=p->next;
}
return Tmp;
}
|
|