维吉尼亚密码编程实现(维吉尼亚密码编程c语言实现)

2023-02-28 0:53:41 密语知识 思思

维吉尼亚密码(Vigenère cipher)

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

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

与凯撒密码类似,进行一下运算两次即可

维吉尼亚密码编程实现(维吉尼亚密码编程c语言实现) 第1张

维吉尼亚密码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/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;

}

维吉尼亚密码进行加密明文为helloeverybodygoodafternoon,密钥为how are you ,对应的密文为?

C++编程实现维吉尼亚密码加密解密

编程实现维吉尼亚密码加密解密

要求:用户可以输入密钥

#include

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[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;

}

用matlab实现凯撒密码,仿射密码,维吉尼亚密码,素数判定和大数分解

function Y=caesarCode(plaintext, shift)

chars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n',...  

      'o','p','q','r','s','t','u','v','w','x','y','z'];  

L=length(plaintext);  

for i=1:L  

      for j=1:26  

          if plaintext(i)==chars(j)  

              k=mod(j+shift,26);  

              if k~= 0  

                  Y(i)=chars(k);  

              else  

                  Y(i)=chars(26);  

              end  

          end  

      end  

end

凯撒密码,来自 Canhui WANG