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()。
2. 数据文件的维吉尼亚加密程序
#include
#include
void menu()/*菜单*/
{
clrscr();
printf("\n===============================================================================");
printf("\n1.Encrypt the file");
printf("\n2.Decrypt the file");
printf("\n3.Quit\n");
printf("===============================================================================\n");
printf("Please select a item:");
return;
}
char encrypt(char ch1,char ch2)/*加密程序*/
{
if(ch1>='A'&&ch1<='Z'&&ch2>='A'&&ch2<='Z')
{
return 'A'+(ch1-'A'+ch2-'A')%26;
}
if(ch1>='a'&&ch1<='z'&&ch2>='a'&&ch2<='z')
{
return 'a'+(ch1-'a'+ch2-'a')%26;
}
return ch1;
}
char decrypt(char ch1,char ch2)/*解密程序*/
{
if(ch1>='A'&&ch1<='Z'&&ch2>='A'&&ch2<='Z')
{
return 'A'+(ch1-'A'+26-(ch2-'A'))%26;
}
if(ch1>='a'&&ch1<='z'&&ch2>='a'&&ch2<='z')
{
return 'a'+(ch1-'a'+26-(ch2-'a'))%26;
}
return ch1;
}
main()
{
int i;
char key[50];
char ch;
FILE *in,*out;
char infile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
clrscr();
sleep(3);
menu();
ch=getch();
while(ch!='3')
{
if(ch=='1')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the key:");
scanf("%s",key);/*输入加密密码*/
printf("Please input the outfile:");
scanf("%s",outfile);/*输入加密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
i=0;
while(!feof(in))
{
fputc(encrypt(fgetc(in),key[i%strlen(key)]),out);
i++;
}
fclose(in);
fclose(out);
printf("\nEncrypt successful!\n");
sleep(1);
}
if(ch=='2')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the key:");
scanf("%s",key);/*输入解密密码*/
printf("Please input the outfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
i=0;
while(!feof(in))
{
fputc(decrypt(fgetc(in),key[i%strlen(key)]),out);
i++;
}
fclose(in);
fclose(out);
printf("\nDecrypt successful!\n");
sleep(1);
}
menu();
ch=getch();
}
clrscr();
logo();
printf("\nGood Bye!\n");
sleep(3);
3. 求古典密码学的c语言代码
给:
维吉尼亚密码的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);
}
4. 维吉尼亚密码 C++
给,网上的C++的基本都有问题,我给你改好一个,已经编译运行确认,
#include<iostream>
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;
}
5. C语言古罗马加密算法
// An highlighted block
#include <stdio.h>
int N=26;
int exgcd(int a, int n) {
int p = a, q = n;
int x = 0, y = 1;
int z = q / p;
while (p != 1 && q != 1) {
int t = p;
p = q % p;
q = t;
t = y;
y = x - y * z;
x = t;
z = q / p;
}
y %= n;
if (y < 0) y += n;
return y;
}
void main()
{ int a,b;
char ch[1000];printf("输入明文");gets (ch);
printf("输入秘钥ab\n");
scanf("%d",&a);
scanf("%d",&b);
d=exgcd(a,26);
int i=0;
char re[1000];
while (ch [i]!= '\0')
{
if ((int)ch [i]>= 65 && (int)ch[i] <= 90)//判断字符是否介于<A,Z>
{
re[i] = (char)(((a*((int)ch [i]- 65) + b) % N) + 65);
printf("%c",re[i]);
}
if ((int)ch [i]>= 97 && (int)ch [i]<= 122)//判断字符是否介于<a,z>
{
re[i] = (char)(((a*((int)ch [i]- 97) + b) % N) + 97);
printf("%c",re[i]);
}
else
i++;//若不是字母则不加处理
i++;
}
printf("\n");
i=0;
char ch1;
while (re[i]!='\0')
{
if ((int)re[i] >= 65 && (int)re [i]<= 90)
{
ch1 = (char)(((d*(((int)re [i]- 65) - b)+N) % N) + 65);
printf("%c",ch1);
}
if ((int)re [i]>= 97 && (int)re [i]<= 122)
{
ch1 = (char)(((d*(((int)re [i]- 97) - b)+N) % N) + 97);
printf("%c",ch1);
}
else
i++;
i++;
}
printf("\n");
}
维吉尼亚密码
#include <stdio.h>
#include <string.h>
void main()
{
char ch[1000],re[1000],key[1000],r;
int key1[1000],n=0,k,m;
int i=0;
printf("输入密文");
gets(ch);
printf("输入密钥");
gets(key);
while(key[i]!='\0')
{key1[i]=(int)key[i]-97;
i++;
}
int c=strlen(ch);
// printf("%d",c);printf("%d",i);
int j;
if(c%i>0)j=c/i+1;
else j=c/i;
//printf("%d",j);
for(k=0;k<j;k++)
{
for( m=0;m<i;m++)
{
if(ch[n]!='\0'){
re[n]=(((int)ch[n]-97+key1[m])%26+97)<=122 ? (char)(((int)ch[n]-97+key1[m])%26+97):(char)(((int)ch[n]-97+key1[m])%26+97-26);
printf("%c",re[n]);
n++;
}
}
}n=0;
printf("\n");
for(k=0;k<j;k++)
{
for(m=0;m<i;m++)
{
if(re[n]!='\0'){
r=(((int)re[n]-97-key1[m])%26+97)>=97?(char)(((int)re[n]-97-key1[m])%26+97):(char)(((int)re[n]-97-key1[m])%26+97+26);
printf("%c",r);
n++;
}
}
}
}
47
48
49
50
6. 如何用C语言写仿射密码和维吉尼亚密码只用C语言函数完成代码,谢谢。
void *(* func) (void *){}
一般写函数的时候不会在函数名前面加*再括号括起来的。如果是函数指针
那么就是返回void*类型。如果真的是函数定义的话,
感觉不会这么写的,还不如写void ** func (void *) 这种名字呢。
7. 维吉尼亚密码该 怎么用
人们在单一恺撒密码的基础上扩展出多表密码,称为“维吉尼亚”密码。它是由16世纪法国亨利三世王朝的布莱瑟·维吉尼亚发明的,其特点是将26个恺撒密表合成一个,见下表:
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
A 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
B 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 A
C C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
D D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
E E F G H I J K L M N O P Q R S T U V W X Y Z A B C D
F F G H I J K L M N O P Q R S T U V W X Y Z A B C D E
G G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
H H I J K L M N O P Q R S T U V W X Y Z A B C D E F G
I I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
J J K L M N O P Q R S T U V W X Y Z A B C D E F G H I
K K L M N O P Q R S T U V W X Y Z A B C D E F G H I J
L L M N O P Q R S T U V W X Y Z A B C D E F G H I J K
M M N O P Q R S T U V W X Y Z A B C D E F G H I J K L
N N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
O O P Q R S T U V W X Y Z A B C D E F G H I J K L M N
P P Q R S T U V W X Y Z A B C D E F G H I J K L M N O
Q Q R S T U V W X Y Z A B C D E F G H I J K L M N O P
R R S T U V W X Y Z A B C D E F G H I J K L M N O P Q
S S T U V W X Y Z A B C D E F G H I J K L M N O P Q R
T T U V W X Y Z A B C D E F G H I J K L M N O P Q R S
U U V W X Y Z A B C D E F G H I J K L M N O P Q R S T
V V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
W W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
X X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
Y Y Z A B C D E F G H I J K L M N O P Q R S T U V W X
Z Z 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
维吉尼亚密码引入了“密钥”的概念,即根据密钥来决定用哪一行的密表来进行替换,以此来对抗字频统计。假如以上面第一行代表明文字母,左面第一列代表密钥字母,对如下明文加密:
TO BE OR NOT TO BE THAT IS THE QUESTION
当选定RELATIONS作为密钥时,加密过程是:明文一个字母为T,第一个密钥字母为R,因此可以找到在R行中代替T的为K,依此类推,得出对应关系如下:
密钥:RELAT IONSR ELATI ONSRE LATIO NSREL
明文:TOBEO RNOTT OBETH ATIST HEQUE STION
密文:KSMEH ZBBLK SMEMP OGAJX SEJCS FLZSY
8. 设密钥字是LOVE,使用维吉尼亚密码加密明文串是JACKOZOO,求密文。
维吉尼亚(Vigenère)密码是一种周期多表代换密码, 由1858年法国密码学家维吉尼亚提出
维吉尼亚密码常常使用英文单词作为密钥字,密钥则是密钥字的重复
维吉尼亚密码加密过程简述如下:
--写下明文,表示为数字形式;
--在明文之上重复写下密钥字,也表示为数字形式;
--加密相对应的明文:给定一个密钥字母k和一个明文字母m,那么密文字母则是(m+k)mod 26计算结果所对应的字母
在明文下面重复写密钥字,组成密钥。
明文M: JACKOZOO
密钥K: LOVELOVE
将明文和密钥转化为数字
明文M=(9,0,2,10,14,25,14,14)
密钥K=(11,14,21,4,11,14,21,4)
对每个明文数字和对应的密钥数字,使用ci=(mi+ki )mod 26加密
得到密文数字为
C=(20,14,23,14,25,13,9,18)
于是密文为:
UOXOZNJS
9. 写一个维吉尼亚加密和解码的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: ");
printf("1. Encode; 2. Decode; Others. Exit. ");
scanf("%d", &operation);
fflush(stdin);
switch (operation)
{
case 1:
printf("please input the key code: ");
gets(key);
fflush(stdin);
printf("please input the source code you want to encode: ");
gets(source);
fflush(stdin);
encode(key, source, destination);
printf("after encode is: ");
printf("%s ", 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: ");
gets(key);
fflush(stdin);
printf("please input the source code you want to decode: ");
gets(source);
fflush(stdin);
decode(key, source, destination);
printf("after decode is: ");
printf("%s ", 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)] = '