Large number arithmetic and DH algorithm(大整数的运算与DH算法的实现)

本文详细介绍了大整数的加减乘除运算算法,并实现了DH密钥交换协议。通过使用字符数组存储大整数,优化乘法运算,提供了一套完整的源代码。在DH算法部分,阐述了如何选择素数、生成私钥、公钥以及秘密密钥,确保了安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按:这是信息安全原理课程的第二次作业,作业要求是Write a +-*/ algorithm for large integers. Implement the DH algorithm.

2014-4-27更新:过了DDL了,重新公开原先隐藏的代码(只是现在不能在代码片中打开,有人能告诉我要怎么做吗?)。补上了所有的图片。

Large integer algorithm

How to use

Open “\SEC2014-HW2-3130000064-林涛\large_integer_algorithm\large_integer_algorithm.exe”,input the value of A and B (it can be as long as 800 numbers, negative is alsoavailable). You can see the results of addition, subtraction, multiplication,division and modulus.


Figure1     test for general largeintegers


Figure2     divide zero


Figure3     negative cases


Figure4     A is zero


Figure5     interesting case


Figure6     A has 300 numbers and Bhas 200 numbers

Algorithm

Use character array to store large number,and use the same way as primate school studens.

We can optimize the multiplicationalgorithm by calculating A*1, A*2 …, A*9 first, which don’t have to becalculated again and again.

Source code

large_integer_algorithm.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 800
char answer[2 * SIZE];
char remainder[SIZE];
//a,b都为自然数,且a>=b
void sum1(char *a, char *b)
{
    int carry = 0;
    int pa, pb;
    int temp;
    int i;
    int na = strlen(a);
    int nb = strlen(b);

    answer[na] = '\0';
    pa = na - 1; pb = nb - 1;
    while (pa >= 0) {
        temp = (a[pa] - '0') + carry;
        if (pb >= 0)temp += (b[pb] - '0');

        if (temp >= 10) {
            answer[pa] = temp - 10 + '0';
            carry = 1;
        }
        else {
            answer[pa] = temp + '0';
            carry = 0;
        }
        pa--; pb--;
    }
    if (carry) {
        for (i = na; i >= 0; i--)
            answer[i + 1] = answer[i];
        answer[0] = '1';
    }
}

void sub1(char *a, char *b)
{
    int borrow = 0;
    int pa, pb, pc;
    int temp;
    int i;
    int na = strlen(a);
    int nb = strlen(b);

    answer[na] = '\0';
    pa = na - 1; pb = nb - 1;
    while (pa >= 0) {
        temp = (a[pa] - '0') - borrow;
        if (pb >= 0)temp -= (b[pb] - '0');

        if (temp < 0) {
            answer[pa] = temp + 10 + '0';
            borrow = 1;
        }
        else {
            answer[pa] = temp + '0';
            borrow = 0;
        }
        pa--; pb--;
    }
    pc = 0;
    while (answer[pc] == '0')
        pc++;
    for (i = 0; i <= na - pc; i++)
        answer[i] = answer[i + pc];
}
//a,b都为自然数
void sum(char *a, char *b)
{
    int na = strlen(a);
    int nb = strlen(b);

    if (na >= nb)
        sum1(a, b);
    else
        sum1(b, a);
}

void sub(char *a, char *b)
{
    int na = strlen(a);
    int nb = strlen(b);

    if (strcmp(a, b) == 0)
        strcpy(answer, "0");
    else if (na > nb || na == nb && strcmp(a, b) > 0)
        sub1(a, b);
    else {
        printf("-");
        sub1(b, a);
    }
}

//a,b都是正整数
void mul(char *a, char *b)
{
    char muls[10][SIZE + 1];
    char temp[2 * SIZE];
    int i, j;
    int na = strlen(a);
    int nb = strlen(b);
    
    strcpy(muls[0], "0");

    strcpy(temp, "0");
    for (i = 1; i <= 9; i++) {
        sum(muls[i - 1], a);
        strcpy(muls[i], answer);
    }

    strcpy(answer, "0");

    for (i = nb - 1; i >= 0; i--) {
        if (b[i] != '0') {
            strcpy(temp, muls[b[i]-'0']);
            //后面加(nb-i-1)个零
            for (j = 0; j < nb - i - 1; j++)
                strcat(temp, "0");
            sum(answer, temp);
        }
    }

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

}
//a,b为自然数,若a>b返回1,不然0
int larger(char *a, char *b)
{
    int na, nb;
    na = strlen(a);
    nb = strlen(b);
    if (na > nb)
        return 1;
    else if (na == nb && strcmp(a, b) > 0)
        return 1;
    else
        return 0;
}

void divide(char *a, char*b)
{
    int na, nb; na = strlen(a); nb = strlen(b);
    char quotient[SIZE];
    char temp[2]; temp[1] = '\0';
    int tryq;
    int i;
    
    char muls[10][SIZE + 1];
    strcpy(muls[0], "0");
    strcpy(temp, "0");
    for (i = 1; i <= 9; i++) {
        sum(muls[i - 1], b);
        strcpy(muls[i], answer);
    }

    strcpy(quotient, "");
    strcpy(remainder, "");
    tryq = 1;

    i = 0;
    while (i != na) {
        temp[0] = a[i];
        strcat(remainder, temp);
        
        tryq = 1;
        //remainder>=muls[tryq]
        while (tryq!=10 && !larger(muls[tryq], remainder)) 
            tryq++;

        temp[0] = tryq -1 + '0';
        if (!(temp[0] == '0' && quotient[0] == '\0'))
            strcat(quotient, temp);

        sub(remainder, muls[tryq - 1]);
        strcpy(remainder, answer);
        if (remainder[0] == '0')
            remainder[0] = '\0';


        i++;
    }
    if (quotient[0] == '\0')
        strcpy(quotient, "0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值