Large number arithmetic and DH algorithm(大整数的运算与DH算法的实现)
2014-03-26 00:53
204 查看
按:这是信息安全原理课程的第二次作业,作业要求是Write a +-*/ algorithm for large integers. Implement the DH algorithm.
2014-4-27更新:过了DDL了,重新公开原先隐藏的代码(只是现在不能在代码片中打开,有人能告诉我要怎么做吗?)。补上了所有的图片。
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
We can optimize the multiplicationalgorithm by calculating A*1, A*2 …, A*9 first, which don’t have to becalculated again and again.
I have choose a prime 300 numbers long asthe prime, which is recommended in “RFC2412”. You don’t have to do anythingabout it anymore.
Figure 7 Generate private key
You can see a new file “private_key.txt” isbuild. The number in it is about 100 numbers long, and is generated randomly.
Figure8 private key
Figure9 Generate public key
You can see a new file “public_key.txt” isbuild. The number in it is about 300 numbers long, and is generated accordingto prime.txt, private_key.txt and primitive_root.txt.
Figure10 public key
You can put the private key and public keyinto a folder “A”, which means it’s the key for person A. And you can do thesame thing to a folder “B”.
Figure11 generate two pairs of keys
Put the private key of A and the public keyof B into the original folder and run “generate_secret_key.exe”.
Figure12 generate secret key
You can see a new file “public_secret.txt”is build. The number in it is about 300 numbers long, and is generated accordingto prime.txt, private_key.txt and public_key.txt.
Figure13 secret key (private A,public B)
Use the private key of B and the public keyof A to try again, you can see the same answer.
Figure14 secret key (private B,public A)
p.
It is impossible to calculate gadirectly when a is a large number, sowe should do “mod p” after eachmultiplication.
It is slow to multiply g for a times, we canreduce the time complexity intoO(log₂N)by diving into two parts each time.
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 andmodulus.
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"); if (remainder[0] == '\0') strcpy(remainder, "0"); printf("%s\n", quotient); } int main(void) { char a[SIZE]; char b[SIZE]; char ch; int na, nb; int flaga, flagb; while (1) { na = 0; nb = 0; flaga = 1; flagb = 1; printf("A = "); ch = getchar(); while (ch != '\n') { if (ch != '-') a[na++] = ch; else flaga = -1; ch = getchar(); } a[na] = '\0'; printf("B = "); ch = getchar(); while (ch != '\n') { if (ch != '-') b[nb++] = ch; else flagb = -1; ch = getchar(); } b[nb] = '\0'; printf("A + B = "); if (flaga == 1 && flagb == 1) sum(a, b); else if (flaga == 1 && flagb == -1) sub(a, b); else if (flaga == -1 && flagb == 1) sub(b, a); else if (flaga == -1 && flagb == -1) { printf("-"); sum(a, b); } printf("%s\n", answer); printf("A - B = "); if (flaga == 1 && flagb == 1) sub(a, b); else if (flaga == 1 && flagb == -1) sum(a, b); else if (flaga == -1 && flagb == 1) { printf("-"); sum(a, b); } else if (flaga == -1 && flagb == -1) sub(b, a); printf("%s\n", answer); printf("A * B = "); if (flaga == 1 && flagb == 1) { if (a[0] == '0' || b[0] == '0') printf("0\n"); else mul(a, b); } else if (flaga == 1 && flagb == -1) { if (a[0] == '0') printf("0\n"); else { printf("-"); mul(a, b); } } else if (flaga == -1 && flagb == 1) { if (b[0] == '0') printf("0\n"); else { printf("-"); mul(a, b); } } else if (flaga == -1 && flagb == -1) mul(a, b); printf("A / B = "); if (flaga == 1 && flagb == 1) { if (b[0] == '0') printf("Divide zero!!!\n"); else if (a[0] == '0') printf("0\n"); else divide(a, b); } else if (flaga == 1 && flagb == -1) { if (a[0] == '0') printf("0\n"); else { printf("-"); divide(a, b); } } else if (flaga == -1 && flagb == 1) { if (b[0] == '0') printf("Divide zero!!!\n"); else { printf("-"); divide(a, b); } } else if (flaga == -1 && flagb == -1) divide(a, b); printf("A %% B = "); if (flagb == 1 && b[0] == '0') printf("Divide zero!!!\n"); else if (flaga == 1 && a[0] == '0' || remainder[0] == '0') printf("0\n"); else if (flaga == 1) printf("%s\n", remainder); else printf("-%s\n", remainder); printf("=============\n"); } getchar(); }
DH algorithm
How to use
All the operations happens in the folder “\SEC2014-HW2-3130000064-林涛\DH”Choose prime
You should choose a prime and one of itscorrespondent primitive root, and put them in prime.txt and primitive_root.txtseparately.I have choose a prime 300 numbers long asthe prime, which is recommended in “RFC2412”. You don’t have to do anythingabout it anymore.
Generate private key
Run “generate_private_key.exe”.Figure 7 Generate private key
You can see a new file “private_key.txt” isbuild. The number in it is about 100 numbers long, and is generated randomly.
Figure8 private key
Generate public key
Run “generate_public_key.exe”.Figure9 Generate public key
You can see a new file “public_key.txt” isbuild. The number in it is about 300 numbers long, and is generated accordingto prime.txt, private_key.txt and primitive_root.txt.
Figure10 public key
Generate secret key
Secret key is the key user actuallyconveyed, by user’s private key and the other’s public key.You can put the private key and public keyinto a folder “A”, which means it’s the key for person A. And you can do thesame thing to a folder “B”.
Figure11 generate two pairs of keys
Put the private key of A and the public keyof B into the original folder and run “generate_secret_key.exe”.
Figure12 generate secret key
You can see a new file “public_secret.txt”is build. The number in it is about 300 numbers long, and is generated accordingto prime.txt, private_key.txt and public_key.txt.
Figure13 secret key (private A,public B)
Use the private key of B and the public keyof A to try again, you can see the same answer.
Figure14 secret key (private B,public A)
Algorithm
Just do as Diffie–Hellman algorithm,calculate A = ga modp.
It is impossible to calculate gadirectly when a is a large number, sowe should do “mod p” after eachmultiplication.
It is slow to multiply g for a times, we canreduce the time complexity intoO(log₂N)by diving into two parts each time.
Source code
generate_private_key.c
#include<stdio.h> int main(void) { FILE *fp; char prikey[200]; int i = 100; srand((unsigned)time(NULL)); printf("Generate private key\n"); printf("Press ENTER"); getchar(); fp = fopen("private_key.txt","w"); fprintf(fp,"%c",rand()%9+'1'); while(i--) fprintf(fp,"%c",rand()%10+'0'); if(fp != NULL) printf("Generate private key successfully!\n"); fclose(fp); }
generate_public_key.c
注:大量代码是与前面重复的,大概就只有main是新的。#include<stdio.h> #include<stdlib.h> #include<string.h> #define SIZE 800 char answer[2 * SIZE]; char remainder[SIZE]; char quotient[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 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"); if (remainder[0] == '\0') strcpy(remainder, "0"); } /* int main(void) { char a[SIZE]; char b[SIZE]; char ch; int na, nb; int flaga, flagb; while (1) { na = 0; nb = 0; flaga = 1; flagb = 1; printf("A = "); ch = getchar(); while (ch != '\n') { if (ch != '-') a[na++] = ch; else flaga = -1; ch = getchar(); } a[na] = '\0'; printf("B = "); ch = getchar(); while (ch != '\n') { if (ch != '-') b[nb++] = ch; else flagb = -1; ch = getchar(); } b[nb] = '\0'; printf("A + B = "); if (flaga == 1 && flagb == 1) sum(a, b); else if (flaga == 1 && flagb == -1) sub(a, b); else if (flaga == -1 && flagb == 1) sub(b, a); else if (flaga == -1 && flagb == -1) { printf("-"); sum(a, b); } printf("%s\n", answer); printf("A - B = "); if (flaga == 1 && flagb == 1) sub(a, b); else if (flaga == 1 && flagb == -1) sum(a, b); else if (flaga == -1 && flagb == 1) { printf("-"); sum(a, b); } else if (flaga == -1 && flagb == -1) sub(b, a); printf("%s\n", answer); printf("A * B = "); if (flaga == 1 && flagb == 1) { if (a[0] == '0' || b[0] == '0') printf("0\n"); else mul(a, b); } else if (flaga == 1 && flagb == -1) { if (a[0] == '0') printf("0\n"); else { printf("-"); mul(a, b); } } else if (flaga == -1 && flagb == 1) { if (b[0] == '0') printf("0\n"); else { printf("-"); mul(a, b); } } else if (flaga == -1 && flagb == -1) mul(a, b); printf("A / B = "); if (flaga == 1 && flagb == 1) { if (b[0] == '0') printf("Devide zero!!!\n"); else if (a[0] == '0') printf("0\n"); else div(a, b); } else if (flaga == 1 && flagb == -1) { if (a[0] == '0') printf("0\n"); else { printf("-"); div(a, b); } } else if (flaga == -1 && flagb == 1) { if (b[0] == '0') printf("Devide zero!!!\n"); else { printf("-"); div(a, b); } } else if (flaga == -1 && flagb == -1) div(a, b); printf("A %% B = "); if (flagb == 1 && b[0] == '0') printf("Devide zero!!!\n"); else if (flaga == 1 && a[0] == '0') printf("0\n"); else if (flaga == 1) printf("%s\n", remainder); else printf("-%s\n", remainder); printf("=============\n"); } getchar(); }*/ int main(void) { FILE *fp; int i; char prime[400]; char pubkey[800]; char prikey[200]; char binary[700]; char pr[10]; printf("Generate public key\n(According to prime.txt, private_key.txt and primitive_root.txt)\n"); fp = fopen("prime.txt","r"); if(fp == NULL){ printf("PLEASE INPUT prime.txt !!!\n"); return 0; } fscanf(fp,"%s",prime); fclose(fp); fp = fopen("private_key.txt","r"); if(fp == NULL){ printf("PLEASE INPUT private_key.txt !!!"); return 0; } fscanf(fp,"%s",prikey); fclose(fp); fp = fopen("primitive_root.txt","r"); if(fp == NULL){ printf("PLEASE INPUT primitive_root.txt !!!"); return 0; } fscanf(fp,"%s",pr); fclose(fp); printf("Press ENTER"); getchar(); i=0; while(prikey[0]!='0'){ divide(prikey,"2"); binary[i++] = remainder[0]; strcpy(prikey, quotient); } binary[i] = 0; strcpy(pubkey, "1"); for(i--;i>=0;i--){ mul(pubkey,pubkey); strcpy(pubkey, answer); if(binary[i] == '1'){ mul(pubkey,pr); strcpy(pubkey, answer); } divide(pubkey, prime); strcpy(pubkey, remainder); printf("%s\n",pubkey); } fp = fopen("public_key.txt","w"); fprintf(fp,"%s",pubkey); if(fp != NULL) printf("Generate public key successfully!\n"); fclose(fp); }
generate_secret_key.c
#include<stdio.h> #include<stdlib.h> #include<string.h> #define SIZE 800 char answer[2 * SIZE]; char remainder[SIZE]; char quotient[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 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"); if (remainder[0] == '\0') strcpy(remainder, "0"); } /* int main(void) { char a[SIZE]; char b[SIZE]; char ch; int na, nb; int flaga, flagb; while (1) { na = 0; nb = 0; flaga = 1; flagb = 1; printf("A = "); ch = getchar(); while (ch != '\n') { if (ch != '-') a[na++] = ch; else flaga = -1; ch = getchar(); } a[na] = '\0'; printf("B = "); ch = getchar(); while (ch != '\n') { if (ch != '-') b[nb++] = ch; else flagb = -1; ch = getchar(); } b[nb] = '\0'; printf("A + B = "); if (flaga == 1 && flagb == 1) sum(a, b); else if (flaga == 1 && flagb == -1) sub(a, b); else if (flaga == -1 && flagb == 1) sub(b, a); else if (flaga == -1 && flagb == -1) { printf("-"); sum(a, b); } printf("%s\n", answer); printf("A - B = "); if (flaga == 1 && flagb == 1) sub(a, b); else if (flaga == 1 && flagb == -1) sum(a, b); else if (flaga == -1 && flagb == 1) { printf("-"); sum(a, b); } else if (flaga == -1 && flagb == -1) sub(b, a); printf("%s\n", answer); printf("A * B = "); if (flaga == 1 && flagb == 1) { if (a[0] == '0' || b[0] == '0') printf("0\n"); else mul(a, b); } else if (flaga == 1 && flagb == -1) { if (a[0] == '0') printf("0\n"); else { printf("-"); mul(a, b); } } else if (flaga == -1 && flagb == 1) { if (b[0] == '0') printf("0\n"); else { printf("-"); mul(a, b); } } else if (flaga == -1 && flagb == -1) mul(a, b); printf("A / B = "); if (flaga == 1 && flagb == 1) { if (b[0] == '0') printf("Devide zero!!!\n"); else if (a[0] == '0') printf("0\n"); else div(a, b); } else if (flaga == 1 && flagb == -1) { if (a[0] == '0') printf("0\n"); else { printf("-"); div(a, b); } } else if (flaga == -1 && flagb == 1) { if (b[0] == '0') printf("Devide zero!!!\n"); else { printf("-"); div(a, b); } } else if (flaga == -1 && flagb == -1) div(a, b); printf("A %% B = "); if (flagb == 1 && b[0] == '0') printf("Devide zero!!!\n"); else if (flaga == 1 && a[0] == '0') printf("0\n"); else if (flaga == 1) printf("%s\n", remainder); else printf("-%s\n", remainder); printf("=============\n"); } getchar(); }*/ int main(void) { FILE *fp; int i; char prime[400]; char pubkey[400]; char prikey[200]; char binary[700]; char secretkey[1200]; printf("Gnerate secret key\n(According to prime.txt, private_key.txt and public_key.txt)\n"); fp = fopen("prime.txt","r"); if(fp == NULL){ printf("PLEASE INPUT prime.txt !!!\n"); return 0; } fscanf(fp,"%s",prime); fclose(fp); fp = fopen("private_key.txt","r"); if(fp == NULL){ printf("PLEASE INPUT private_key.txt !!!"); return 0; } fscanf(fp,"%s",prikey); fclose(fp); fp = fopen("public_key.txt","r"); if(fp == NULL){ printf("PLEASE INPUT public_key.txt !!!"); return 0; } fscanf(fp,"%s",pubkey); fclose(fp); printf("Press ENTER"); getchar(); i=0; while(prikey[0]!='0'){ divide(prikey,"2"); binary[i++] = remainder[0]; strcpy(prikey, quotient); } binary[i] = 0; strcpy(secretkey, "1"); for(i--;i>=0;i--){ mul(secretkey,secretkey); strcpy(secretkey, answer); if(binary[i] == '1'){ mul(secretkey,pubkey); strcpy(secretkey, answer); } divide(secretkey, prime); strcpy(secretkey, remainder); printf("%s\n",secretkey); } fp = fopen("secret_key.txt","w"); fprintf(fp,"%s",secretkey); if(fp != NULL) printf("Generate secret key successfully!\n"); fclose(fp); }
prime.txt
179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007
primitive_root.txt
2
相关文章推荐
- [Codeforces546D]Soldier and Number Game[dp][实现][素数筛][分解整数][数学]
- 位运算实现整数加减乘除
- C#实现超长位整数运算
- 用位运算实现两个整数的加法运算
- linux shell 实现 四则运算(整数及浮点) 简单方法
- 位运算之美——用+,-和位运算实现正整数除法和取模(一)
- 双向链表实现长整数运算
- 基本数据结构实现(Data Structures and Algorithm Analysis 2rd Edition)
- 位运算之美——用+,-和位运算实现正整数除法和取模(一)
- 大整数加减运算的C语言实现
- 不用+-*/%实现整数的+-*/%运算
- C++实现大正整数及其相关运算(长期更新)
- 自定义大数类,用数组实现任意超大整数的加减乘运算
- C语言实现大整数加减运算详解
- Data Structures and algorithm analysis—1.2.4&1.2.5Modular Arithmeti&The P Word(数据结构—模数运算&P字)
- 用位运算实现两个整数的加减乘除运算
- 只用位运算来实现整数的加减乘除四则运算
- 位运算之美——用+,-和位运算实现正整数除法和取模(二)
- shell 实现整数及浮点数四则运算的简单方法 by 程默
- 实现任意长的整数进行加法运算的演示程序