输入界面为什么没有反应呢?
实验内容:
1、编写Diffie-Hellman程序,协商出用于收、发双方的加、解密密钥K;
2、模拟发送端A,用协商出的密钥K,通过对称加密算法(具体算法不限)将文本文件加密;模拟接收端B,用协商出的密钥K,对接收到的密文解密。
跑出来之后输完AB端公钥之后各个窗口是这个显现是为什么?
//A端代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int Xa = 3, Xb = 5, Ya, Yb;
int usera(int alpha, int q)
{
int k1 = pow(Yb, Xa);
k1 = k1 % q;
return k1;
}
int userb(int alpha, int q)
{
int k2 = pow(Ya, Xb);
k2 = k2 % q;
return k2;
}
void InitSbox(unsigned char sbox[])
{
int i;
for (i = 0; i < 256; i++)
sbox[i] = i;
}
void KeyExpansion(unsigned char key[], char *k, int len)
{
int i;
if (len <= 256)
{
for (i = 0; i < 256; i++)
key[i] = k[i % len];
}
if (len > 256)
{
for (i = 0; i < 256; i++)
key[i] = k[i];
}
}
void UpsetSbox(unsigned char sbox[], unsigned char key[])
{
int j = 0, i;
unsigned char temp;
int n;
for (i = 0; i < 256; i++)
{
n = j + (int)sbox[i] + (int)key[i];
j = n % 256;
temp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = temp;
}
}
void DataProcess(unsigned char sbox[], FILE *fp1, FILE *fp2)
{
int write_file(char name[], char data[], int size);
int i = 0, j = 0, size = 0;
int temp2, temp1, t;
char k, cipherchar, data_Stream[100] = {0x00};
unsigned char temp;
char ch = fgetc(fp1);
while (ch != EOF)
{
i = (i + 1) % 256;
temp2 = j + (int)sbox[i];
j = temp2 % 256;
temp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = temp;
temp1 = (int)sbox[i] + (int)sbox[j];
t = temp1 % 256;
data_Stream[size++] = k = sbox[t];
cipherchar = ch ^ k;
fputc(cipherchar, fp2);
ch = fgetc(fp1);
printf("%c", ch);
}
write_file("Stream.txt", data_Stream, size);
}
void DataEncrypt(unsigned char *k, unsigned char *key, unsigned char *sbox, FILE *fp1, FILE *fp2)
{
int len = strlen((char*)k);
KeyExpansion(key,(char*)k, len);
InitSbox(sbox);
UpsetSbox(sbox, key);
printf("\n明文为:");
DataProcess(sbox, fp1, fp2);
fclose(fp1);
fclose(fp2);
printf("\n加密成功,加密内容存于“miwen.txt”中\n\n");
}
int write_file(char name[], char data[], int size)
{
FILE *file = fopen(name, "ab+");
if (file == NULL)
return 0;
fwrite(data, sizeof(char), size, file);
rewind(file);
fclose(file);
return 1;
}
int read_file(unsigned char name[], char data[], int size)
{
FILE *file = fopen((const char*)name, "rb+");
if (file == NULL)
return 0;
rewind(file);
fread(data, sizeof(char), size, file);
fclose(file);
return 1;
}
int main(int argc, const char *argv[])
{
int q = 19, alpha = 11;
int Ka;
unsigned char key1[25] = {0x00}, key2[25] = {0x00};
unsigned char key[256] = {0x00};
unsigned char sbox[256] = {0x00};
FILE *fp1, *fp2;
int flag = 1, i;
printf("A用户端\n");
printf("q=19,a=11\n");
Ya = pow(alpha, Xa);
Ya = Ya % q;
printf("计算得到Ya = %d\n", Ya);
printf("请输入B端的公钥Yb:");
scanf("%d", &Yb);
Ka = usera(alpha, q);
printf("计算得到最终公钥K=%d", Ka);
key1[0] = Ka;
fp1 = fopen("mingwen.txt", "r");
if (fp1 == NULL)
{
printf("打开源文件失败!\n");
getchar();
exit(0);
}
DataEncrypt(key1, key, sbox, fp1, fp2);
return 0;
}
//B端代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int Xb = 5, Ya, Yb;
/*函数声明*/
void InitSbox(unsigned char sbox[]);
void KeyExpansion(unsigned char key[], char *k, int len);
void UpsetSbox(unsigned char sbox[], unsigned char key[]);
void DataProcess(unsigned char sbox[], FILE *fp1, FILE *fp2);
void DataEncrypt(char *k, unsigned char *key, unsigned char *sbox, FILE *fp1, FILE *fp2);
void DataDecrypt(char *k1, unsigned char *key, unsigned char *sbox, FILE *fp1, FILE *fp2);
int write_file(char name[], char data[], int size);
int read_file(char name[], char data[], int size);
/*初始化S盒*/
void InitSbox(unsigned char sbox[]){
int i;
for(i = 0; i < 256; i++) sbox[i] = i;
}
/*密钥填充256数组*/
void KeyExpansion(unsigned char key[], char *k, int len){
int i;
if(len <= 256){
for(i = 0; i < 256; i++) key[i] = k[i % len];
}
if(len > 256){
for(i = 0; i < 256; i++) key[i] = k[i];
}
}
/*打乱S盒*/
void UpsetSbox(unsigned char sbox[], unsigned char key[]){
int j = 0, i;
unsigned char temp;
int n;
for(i = 0; i < 256; i++){
n = j + (int)sbox[i] + (int)key[i];
j = n % 256;
temp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = temp;
}
}
/*加解密数据*/
void DataProcess(unsigned char sbox[], FILE *fp1, FILE *fp2){//0加密,1解密
int i = 0, j = 0, size = 0;
int temp2, temp1, t;
char k, cipherchar, data_Stream[100] = {0x00};
unsigned char temp;
char ch = fgetc(fp1);
while(ch != EOF){
i = (i + 1) % 256;
temp2 = j + (int)sbox[i];
j = temp2 % 256;
temp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = temp;
temp1 = (int)sbox[i] + (int)sbox[j];
t = temp1 % 256;
data_Stream[size++] = k = sbox[t];
cipherchar = ch ^ k;
fputc(cipherchar, fp2);
ch = fgetc(fp1);
printf("%c", ch);
}
write_file("Stream.txt", data_Stream, size);
}
/*解密总函数*/
void DataDecrypt(unsigned char *k1, unsigned char *key, unsigned char *sbox, FILE *fp1, FILE *fp2) {
int len = strlen((char*)k1);
KeyExpansion(key, (char*)k1, len);
InitSbox(sbox);
UpsetSbox(sbox, key);
printf("\n密文为:");
DataProcess(sbox, fp1, fp2);
fclose(fp1);
fclose(fp2);
printf("\n解密成功,内容存于“jiemi.txt”中!\n\n");
}
/*******文件的写操作**********/
int write_file(char name[], char data[], int size){
FILE *file = fopen(name, "ab+");
if(file == NULL)
return 0;
fwrite(data, sizeof(char), size, file);//写入一个数组
rewind(file);//移动指针到开头
fclose(file);
return 1;
}
/******文件的操作*******/
int read_file(unsigned char name[], char data[], int size){
FILE *file = fopen((const char*)name, "rb+");
if(file == NULL)
return 0;
rewind(file);//移动指针到开头
fread(data, sizeof(char), size, file);//读出一个数组
fclose(file);
return 1;
}
/**********B端通过Ya计算Kb*************/
int userb(int alpha, int q){
int k2 = pow(Ya, Xb);
k2 = k2 % q;
return k2;
}
int main(int argc, const char * argv[]) {
int q = 19, alpha =11 ;
int Kb;
unsigned char key1[25] = {0x00}, key2[25] = {0x00};
unsigned char key[256] = {0x00};
unsigned char sbox[256] = {0x00};
FILE *fp1, *fp2;
int flag = 1, i;
printf("B用户端\n");
printf("q=19, a=11\n");
//Calculating Yb
Yb = pow(alpha, Xb);
Yb = Yb % q;
printf("计算得到Yb = %d\n", Yb);
printf("请输入A端的公钥Ya:");
scanf("%d", &Ya);
Kb = userb(alpha, q);
printf("计算得到最终秘钥K=%d\n", Kb);
key2[0] = Kb;
/*********解密**********/
fp1 = fopen("miwen.txt", "r");
if(fp1 == NULL){
printf("打开加密后文件失败!\n");
getchar();
exit(0);
}
fp2 = fopen("jiemi.txt", "w");
if(fp2 == NULL){
printf("打开解密后文件失败!\n");
getchar();
exit(0);
}
DataDecrypt(key2, key, sbox, fp1, fp2);
return 0;
}