Adam____ 2024-05-09 22:07 采纳率: 75%
浏览 3

关于#Diffie#的问题,如何解决?(标签-算法|关键词-#include)

输入界面为什么没有反应呢?
实验内容:
1、编写Diffie-Hellman程序,协商出用于收、发双方的加、解密密钥K;
2、模拟发送端A,用协商出的密钥K,通过对称加密算法(具体算法不限)将文本文件加密;模拟接收端B,用协商出的密钥K,对接收到的密文解密。
跑出来之后输完AB端公钥之后各个窗口是这个显现是为什么?

img

//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;
}
  • 写回答

4条回答 默认 最新

  • DTcode7 前端领域优质创作者 2024-05-10 22:55
    关注

    答案引用自chatgpt生成,望对题主有所帮助/启发;若有帮助,还望采纳!

    关于 Diffie 的问题,您提到的标签是算法,关键词是 #include。从这些信息来看,您可能在询问关于 Diffie-Hellman 密钥交换算法的实现问题,以及如何在 C/C++ 代码中包含必要的头文件。

    Diffie-Hellman 是一种在公开通信通道上生成共享秘密的方法,广泛用于安全通信协议中。以下是使用 C/C++ 实现 Diffie-Hellman 密钥交换算法的基本步骤:

    1. 选择参数: 选择两个大的质数 g 和 p,其中 p 是一个大质数,g 是 p 的一个原根。

    2. 生成私钥: 每个用户选择一个私有整数 a 和 b,保持它们的私密性。

    3. 计算公钥: 用户 A 计算 A = g^a mod p,并将其发送给用户 B。同样,用户 B 计算 B = g^b mod p,并将其发送给用户 A。

    4. 生成共享密钥: 用户 A 计算 s = B^a mod p,用户 B 计算 s = A^b mod p。由于离散对数问题的困难性,s 对于两者来说应该是相同的,并且可以用来作为对称加密的密钥。

    在 C/C++ 中实现时,通常需要使用一些数学库来处理大数运算,比如 OpenSSL。要包含 OpenSSL 库,您需要在您的 C/C++ 文件中包含相应的头文件:

    #include <openssl/dh.h>
    #include <openssl/bn.h>
    

    然后,您可以使用 OpenSSL 提供的函数来创建 Diffie-Hellman 参数、生成密钥对、计算共享密钥等。

    如果您遇到具体的问题或错误,提供详细的错误信息或代码片段将有助于得到更具体的帮助。如果您是在寻找如何在您的项目中包含 OpenSSL 库,或者如何编译使用了 OpenSSL 的程序,请提供更多上下文信息。

    评论

报告相同问题?

问题事件

  • 创建了问题 5月9日

悬赏问题

  • ¥20 如何再GIS用海岸线建立缓冲区
  • ¥15 codeblock遇到问题了,求帮助😭
  • ¥15 Qt6.8.0加载网页MSVC2022
  • ¥15 360浏览器m2的这个值
  • ¥15 国内有哪些厂商做automlops的?
  • ¥15 skynet pb mysql
  • ¥15 笔记本外接显示器分辨率太低各种方法都用过了调不高
  • ¥15 Redstone R0697-F00 D2020 交换机 OS
  • ¥50 H5+js 动态数字画廊怎么做?
  • ¥20 外向内全景图像拼接相关项目和论文咨询