維吉尼亞加密

Vigenere(維吉尼亞)加密:


當輸入明文,自動生成隨機密匙匹配明文中每個字母並移位加密。

#include<stdio.h>
#include<malloc.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
	struct dualnode
	{
		char data;     //elementype表示一種數據類型,可能是int/char等等
	 struct dualnode *next;   //next 指針,用於鏈表結構指向下一個節點
		struct dualnode *prior;
	};
	typedef struct dualnode dualnode; //重定義struct node類型爲node

	void Out(int i,dualnode *head);
	dualnode* Creat(int n);
	int main()
	{
		dualnode* head;
		srand(time(NULL));
		head = Creat(26);
		char str[10] = "";
		int a[10];
		int n = 0;
		do
		{
			scanf("%c",&str[n]);
			a[n] = rand()%100;
			n++;
		}while((n < 10)&&(str[n-1]!='\n'));
		n = 0;
		/*加密輸出*/
		while((n < 10)&&(str[n] != '\n'))
		{
			printf("%d->",a[n]);
			Out((int)(str[n]-'a')+a[n],head);
			n++;
		}
		/*解謎輸出*/
		printf("\n");
		n = 0;
		while((n < 10)&&(str[n] != '\n'))
		{
			Out((int)(str[n]-'a'),head);
			n++;
		}
		return 0;
	}
	dualnode* Creat(int n)
	{
		dualnode *p = NULL;
		dualnode *head;
		head =(dualnode *)malloc(sizeof(dualnode));
		p = head;
		dualnode *s;
		int i = 0;
		if(0 != n)
		{
			while(i < n)
			{
				s = (dualnode*)malloc(sizeof(dualnode));
				s->data = 'a'+i;
				p->next = s;
				s->prior = p;
				p = s;
				i++;
			}
			s->next = head->next;
			head->next->prior = s;
		}
		free(head);
		return s->next;
	}
void Out(int i,dualnode *head)
{

		while(i)
		{
			head = head->next;
			i--;
		}
		printf("%c",head->data);
}


發佈了41 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章