c语言维吉尼亚加密算法(维吉尼亚密码加密解密算法)

2023-02-27 20:36:22 密码用途 思思

维吉尼亚密码c语言求改。

#include stdlib.h

#include stdio.h

#include string.h

#define N 10000

void function(char message[],char key[],int mode); //加解密函数

int main()

{

int choose;

char m[N],key[N];

printf("维吉尼亚加密,请输入1;解密,请输入2:\n");

scanf("%d",choose);

getchar();

if (choose == 1 || choose == 2)

{

if (choose == 1)

printf("输入明文:\n");

if (choose == 2)

printf("输入密文:\n");

gets(m);

printf("输入密钥:\n");

gets(key);

function(m,key,choose);

}

else

printf("输入错误!\n");

return 0;

}

void function(char message[],char key[],int mode) //加解密函数

{

int i, j = 0; //j控制key的轮回

int len_k = strlen(key); //密钥长度

char s[N];

for(i=0; message[i]!='\0'; i++)

{

if(message[i] == 32) //判断空格

s[i]=' ';

else

{

if (mode == 1)

s[i]=(int(message[i]-'a')+int(key[j%len_k]-'a'))%26+97;

if (mode == 2)

s[i]=(int(message[i]-'a')-int(key[j%len_k]-'a')+26)%26+97;

j++;

}

printf("%c",s[i]);

}

printf("\n");

}

gets(l);//不加这句M就输入不了为什么?

是因为没有这句的话,按的回车键就输成m了。

连用两个输入语句时,需要考虑回车键,就像我代码里的getchar()。

(c语言)求密码

360问答

求古典密码学的c语言代码

guiyids0033 LV122014-01-16

各位高手小弟急需一个维吉尼亚密码的程序。帮帮忙

满意答案

aa7777777777qq

LV12

2014-01-16

给: 维吉尼亚密码的C语言源代码 设m表示明文序列,k表示密钥序列 #include ctype.h #include stdio.h #include stdlib.h #include string.h #include conio.h void crypt(char m[],char k[],char r[]) { int i,j,s=0; j=strlen(k); for(i=0;m[i];i++) m[i]=tolower(m[i]); for(i=0;m[i];i++) if(isalpha(m[i])) { r[i]=(m[i]-'a'+k[s%j]-'a')%26+'a'; s++;/* s用来跳过明文中的空格字符 */ } else r[i]=m[i]; r[i]=0;/* 密文字符串结束符 */ for(i=0;r[i];i++) r[i]=toupper(r[i]); } void decrypt(char c[],char k[],char m[]) { int i,j,s=0; j=strlen(k); for(i=0;c[i];i++) c[i]=tolower(c[i]); for(i=0;c[i];i++) if(isalpha(c[i])) { m[i]=(c[i]-k[s%j]+26)%26+'a'; s++; } else m[i]=c[i]; m[i]=0; } void main() { char m[]="welcome to my blog.i am bugeyes.";//我这里是赋值了一个固定的字符串为明文序列,你也可以做成用户输入的 char k[]="bugeyeswuyan";//我这里是赋值了一个固定的字符串为密钥序列,你也可以做成用户输入的 char c[80]; char d[80]; system("cls");; crypt(m,k,c); decrypt(c,k,d); puts(m); puts(k); puts(c); puts(d); }

c语言维吉尼亚加密算法(维吉尼亚密码加密解密算法) 第1张

维吉尼亚加密算法如何加密任意格式的文件?

维吉尼亚算法如果仅仅作用于字符集在A到Z

a到z,0到9这样的范围,就叫对“英文文本”实施加密

这个英文文本的范围,A到Z等于字节值65到90,

a到z等于字节值97到122,0到9等于字节值48到57。中间是断续的几个区间

算法如果改动一下,把字符集扩大到0到255,相当于对256个字母重新编排密码本,而不局限于英文文本的26个字母。这样就能对一个字节的所有情况进行加密。能对字节加密,所有的文件都以字节为基本存储单位,也就实现了对任何文件、任何数据的加密。

如果之前英文文本的处理是自己写的,稍加改动就能变成处理256个字母。

写一个维吉尼亚加密和解码的C语言程序,具体要求如下。不要用GOTO, 不要有MAGIC NUMBER。

#include stdio.h

#define MINCHAR 32

#define CHARSUM 94

int encode(char* key, char* source, char* dest);

int decode(char* key, char* source, char* dest);

char table[CHARSUM][CHARSUM];

int main()

{

int i, j;

char key[256];

char source[256];

char destination[256];

int operation;

FILE *fp;

/* Initial the Vigenere table */

for(i = 0; i CHARSUM; i++)

for(j = 0; j CHARSUM; j++)

table[i][j] = MINCHAR + (i + j) % CHARSUM;

printf("please choose one operation code:\n");

printf("1. Encode; 2. Decode; Others. Exit.\n");

scanf("%d", operation);

fflush(stdin);

switch (operation)

{

case 1:

printf("please input the key code:\n");

gets(key);

fflush(stdin);

printf("please input the source code you want to encode:\n");

gets(source);

fflush(stdin);

encode(key, source, destination);

printf("after encode is: \n");

printf("%s\n", destination);

fp=fopen("key.txt", "w");

fprintf(fp, "%s", key);

fclose(fp);

fp=fopen("source.txt", "w");

fprintf(fp, "%s", source);

fclose(fp);

fp=fopen("destination.txt", "w");

fprintf( fp, "%s",destination);

fclose(fp);

break;

case 2:

printf("please input the key code:\n");

gets(key);

fflush(stdin);

printf("please input the source code you want to decode:\n");

gets(source);

fflush(stdin);

decode(key, source, destination);

printf("after decode is: \n");

printf("%s\n", destination);

fp=fopen("key.txt", "w");

fprintf(fp, "%s", key);

fclose(fp);

fp=fopen("source.txt", "w");

fprintf(fp, "%s", source);

fclose(fp);

fp=fopen("destination.txt", "w");

fprintf(fp, "%s", destination);

fclose(fp);

break;

default:

return 0;

}

return 0;

}

int encode(char* key, char* source, char* dest)

{

char* tempSource = source;

char* tempKey = key;

char* tempDest = dest;

do

{

*tempDest = table[(*tempKey) - MINCHAR][(*tempSource) - MINCHAR];

tempDest++;

if (!(*(++tempKey)))

tempKey = key;

} while(*tempSource++);

dest[strlen(source)] = '\0';

return 1;

}

int decode(char* key, char* source, char* dest)

{

char* tempSource = source;

char* tempKey = key;

char* tempDest = dest;

char offset;

do

{

offset = (*tempSource) - (*tempKey);

offset = offset = 0 ? offset : offset + CHARSUM;

*tempDest = MINCHAR + offset;

tempDest++;

if (!(*(++tempKey)))

tempKey = key;

} while(*++tempSource);

dest[strlen(source)] = '\0';

return 1;

}

维吉尼亚密码 C++

给,网上的C++的基本都有问题,我给你改好一个,已经编译运行确认,

#includeiostream

using namespace std;

#define MINCHAR 32

#define CHARSUM 94

char table[CHARSUM][CHARSUM];

bool Init();

bool Encode(char* key, char* source, char* dest);

bool Dncode(char* key, char* source, char* dest);

int main()

{

if(!Init())

{

cout "初始化错误!" endl;

return 1;

}

char key[256];

char str1[256];

char str2[256];

int operation;

while(1)

{

do

{

cout "请选择一个操作:1. 加密; 2. 解密; -1. 退出\n";

cin operation;

}while(operation != -1 operation != 1 operation != 2);

if(operation == -1)

return 0;

else if(operation == 1)//加密

{

cout "请输入密钥:";

cin key;

cout "请输入待加密字符串:";

cin str1;

Encode(key, str1, str2);

cout "加密后的字符串:" str2 endl;

}

else if(operation == 2)//解密

{

cout "请输入密钥:";

cin key;

cout "请输入待解密字符串:";

cin str1;

Dncode(key, str1, str2);

cout "解密后的字符串:" str2 endl;

}

cout endl;

}

return 0;

}

// 初始化维吉尼亚方阵

bool Init()

{

int i, j;

for(i = 0; i CHARSUM; i++)

{

for(j = 0; j CHARSUM; j++)

{

table[i][j] = MINCHAR + (i + j) % CHARSUM;

}

}

return true;

}

// 加密

// key:密钥

// source:待加密的字符串

// dest:经过加密后的字符串

bool Encode(char* key, char* source, char* dest)

{

char* tempSource = source;

char* tempKey = key;

char* tempDest = dest;

do

{

*tempDest = table[(*tempKey) - MINCHAR][(*tempSource) - MINCHAR];

tempDest++;

if(!(*(++tempKey)))

tempKey = key;

}while(*tempSource++);

dest[strlen(source)] = 0;

return true;

}

// 解密

// key:密钥

// source:待解密的字符串

// dest:经过解密后的字符串

bool Dncode(char* key, char* source, char* dest)

{

char* tempSource = source;

char* tempKey = key;

char* tempDest = dest;

char offset;

do

{

offset = (*tempSource) - (*tempKey);

offset = offset = 0 ? offset : offset + CHARSUM;

*tempDest = MINCHAR + offset;

tempDest++;

if(!(*(++tempKey)))

tempKey = key;

}while(*++tempSource);

dest[strlen(source)] = 0;

return true;

}

简单的加密算法——维吉尼亚密码

学号:16030140019

姓名:   莫益彰

【嵌牛导读】:凯撒密码是一种简单的加密方法,即将文本中的每一个字符都位移相同的位置。如选定位移3位:

原文:a b c

密文:d e f

由于出现了字母频度分析,凯撒密码变得很容易破解,因此人们在单一恺撒密码的基础上扩展出多表密码,称为“维吉尼亚”密码。

【嵌牛鼻子】密码学,计算机安全。

【嵌牛提问】维吉尼亚怎么破解,8位维吉尼亚是否可破?维吉尼亚算法的时间复杂度?

【嵌牛正文】

维吉尼亚密码的加密

维吉尼亚密码由凯撒密码扩展而来,引入了密钥的概念。即根据密钥来决定用哪一行的密表来进行替换,以此来对抗字频统计。假如以上面第一行代表明文字母,左面第一列代表密钥字母,对如下明文加密:

TO BE OR NOT TO BE THAT IS THE QUESTION

当选定RELATIONS作为密钥时,加密过程是:明文一个字母为T,第一个密钥字母为R,因此可以找到在R行中代替T的为K,依此类推,得出对应关系如下:

密钥:RE LA  TI  ONS  RE LA TION   SR ELA TIONSREL

明文:TO BE OR NOT TO BE THAT  IS  THE QUESTION

密文:KS ME HZ  BBL  KS ME MPOG AJ XSE JCSFLZSY

图解加密过程:

        在维吉尼亚(Vigenère)的密码中,发件人和收件人必须使用同一个关键词(或者同一文字章节),这个关键词或文字章节中的字母告诉他们怎么样才能前后改变字母的位置来获得该段信息中的每个字母的正确对应位置。

维吉尼亚密码的破解

维吉尼亚密码分解后实则就是多个凯撒密码,只要知道密钥的长度,我们就可以将其分解。

如密文为:ABCDEFGHIJKLMN

如果我们知道密钥长度为3,就可将其分解为三组:

组1:A D G J N

组2:B E H K

组3:C F I M

分解后每组就是一个凯撒密码,即组内的位移量是一致的,对每一组即可用频度分析法来解密。

所以破解维吉尼亚密码的关键就是确定密钥的长度。

确定密钥长度

确定密钥长度主要有两种方法,Kasiski 测试法相对简单很多,但Friedman 测试法的效果明显优于Kasiski 测试法。

Kasiski 测试法

在英文中,一些常见的单词如the有几率被密钥的相同部分加密,即原文中的the可能在密文中呈现为相同的三个字母。

在这种情况下,相同片段的间距就是密文长度的倍数。

所以我们可以通过在密文中找到相同的片段,计算出这些相同片段之间的间距,而密钥长度理论上就是这些间距的公约数。

然后我们需要知道重合指数(IC, index of coincidence)的概念。

重合指数表示两个随机选出的字母是相同的的概率,即随机选出两个A的概率+随机选出两个B的概率+随机选出两个C的概率+……+随机选出两个Z的概率。

对英语而言,根据上述的频率表,我们可以计算出英语文本的重合指数为

P(A)^2 + P(B)^2+……+P(Z)^2 = 0.65

利用重合指数推测密钥长度的原理在于,对于一个由凯撒密码加密的序列,由于所有字母的位移程度相同,所以密文的重合指数应等于原文语言的重合指数。

据此,我们可以逐一计算不同密钥长度下的重合指数,当重合指数接近期望的0.65时,我们就可以推测这是我们所要找的密钥长度。

举例来说,对密文ABCDEABCDEABCDEABC

首先测试密钥长度=1,对密文ABCDEABCDEABCDEABC统计每个字符出现的次数:

A: 4 B: 4 C: 4 D:3 E:3

那么对于该序列的重合指数就为:(4/18)^2 + (4/18)^2 + (4/18)^2 +(3/18)^2 +(3/18)^2 != 0.65

然后测试密钥长度=2,将密文ABCDEABCDEABCDEABC分解为两组:

组1:A C E B D A C E B

组2:B D A C E B D A C

我们知道如果密钥长度真的是2,那么组1,组2都是一个凯撒密码。对组1组2分别计算重合指数。

如果组1的重合指数接近0.65,组2的重合指数也接近0.65,那么基本可以断定密钥长度为2。

在知道了密钥长度n以后,就可将密文分解为n组,每一组都是一个凯撒密码,然后对每一组用字母频度分析进行解密,和在一起就能成功解密凯撒密码。

上文已经说到,自然语言的字母频度是一定的。字母频度分析就是将密文的字母频度和自然语言的自然频度排序对比,从而找出可能的原文。