数据结构与算法中维吉利亚加密
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct num
{
struct num* next;
char data;
}stack;
//创建循环链表
stack* creatList()
{
stack* head = (stack*)malloc(sizeof(stack));
stack* s;
s = (stack*)malloc(sizeof(stack));
stack* p = NULL;
p = head;
int i = 0;
while (i <= 26)
{
s = (stack*)malloc(sizeof(stack));
s->data = 'a'+i;
p->next = s;
p = s;
i++;
}
s->next = head->next;
free(head);
return s->next;
}
//依次定位数据位置,根据随机数进行偏移
move(stack* s,int* e,char *ch)
{
int i,j,len;
stack* p ;
p = NULL;
j = i = 0;
len = strlen(ch);
printf("\n加密为:");
while (1)
{
p = s;
if (i >= len)
break;
while (p->data!=ch)
{
p = p->next;
}
for (j = 0; j < e; j++)
{
p = p->next;
}
printf("%c", p->data);
i++;
}
printf("\n");
}
int main()
{
stack* s= creatList();
char ch;
printf("输入原文: ");
scanf("%s", ch);
int x,i,a;
printf("秘钥为:");
for (i = 0; i < strlen(ch); i++)
{
x = rand() % 26;
printf("%d ", x);
a =x ;
}
move(s, a, ch);
return 0;
}
页:
[1]