RSA加解密算法C语言的实现
RSA加密算法_源代码__C语言实现

RSA算法
1978年就出现了这种算法,它是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。算法的名字以发明者的名字命名:Ron Rivest, AdiShamir和Leonard Adleman。但RSA的安全性一直未能得到理论上的证明。
RSA的安全性依赖于大数难于分解这一特点。公钥和私钥都是两个大素数(大于100个十进制位)的函数。据猜测,从一个密钥和密文推断出明文的难度等同于分解两个大素数的积。
} b--; //c=a * c % n; //这里也会溢出,若把64位整数拆为两个32位整数不知是否可以解决这个问题。
c=MulMod(a, c, n);
} return c;
}/*
Rabin-Miller素数测试,通过测试返回1,否则返回0。
n是待测素数。
注意:通过测试并不一定就是素数,非素数通过测试的概率是1/4
5,
7,
11,
13,
17,
19,
23,
29,
RSA加密算法(C语言实现)

RSA加密算法(C语言实现)RSA(Rivest-Shamir-Adleman)算法是一种非对称加密算法,它是目前应用最广泛的加密算法之一、RSA算法基于两个大素数之间的乘积很难分解的特性,并使用公钥和私钥进行加密和解密。
在C语言中实现RSA算法需要进行以下步骤:1.生成大素数p和q:选择两个大素数p和q,它们需要满足p≠q。
这样选取p和q是为了使得计算n=p*q变得困难,保护私钥。
2.计算n:计算n=p*q,n即为公钥和私钥的参数之一3.计算欧拉函数φ(n):计算欧拉函数φ(n)=(p-1)*(q-1)。
4.选择e:选择一个与φ(n)互质且小于φ(n)的整数e作为加密指数,e即为公钥的参数。
5. 计算d:计算d = e^(-1) mod φ(n),d即为私钥的参数。
可以使用扩展欧几里得算法来计算d。
6. 加密:将明文M转换为整数m,加密后的密文C = m^e mod n。
7. 解密:解密密文C得到明文M = C^d mod n。
以下是C语言实现RSA加密算法的代码示例:```c#include <stdio.h>int gcd(int a, int b)if(b == 0)}return gcd(b, a % b);int extendedGcd(int a, int b, int *x, int *y) if(a == 0)*x=0;*y=1;return b;}int x1, y1;int gcd = extendedGcd(b % a, a, &x1, &y1);*x=y1-(b/a)*x1;*y=x1;return gcd;int modInverse(int a, int m)int x, y;int gcd = extendedGcd(a, m, &x, &y);if(gcd != 1)printf("Inverse doesn't exist\n");}return (x % m + m) % m;int powerMod(int x, unsigned int y, int m) if (y == 0)return 1;}int p = powerMod(x, y/2, m) % m;p=(p*p)%m;return (y%2 == 0) ? p : (x*p) % m;int maiint p, q, n, phiN, e, d;//选择两个大素数p和qp=31;q=17;//计算n和φ(n)n=p*q;phiN = (p - 1) * (q - 1);//选择加密指数ee=7;//计算解密指数dd = modInverse(e, phiN);int plaintext = 88;int ciphertext = powerMod(plaintext, e, n);int decryptedtext = powerMod(ciphertext, d, n);printf("Plaintext: %d\n", plaintext);printf("Ciphertext: %d\n", ciphertext);printf("Decryptedtext: %d\n", decryptedtext);return 0;```在上面的代码中,我们使用了几个辅助函数来实现扩展欧几里得算法、计算模反元素和快速幂算法。
C语言实现RSA算法

C语言实现RSA算法RSA算法是一种非对称加密算法,用于在网络通信中进行数据加密和解密。
下面我将给出C语言中RSA算法的实现。
首先,我们需要生成RSA密钥对,包括公钥和私钥。
以下是生成RSA 密钥对的C代码实现:```c#include <stdio.h>#include <stdlib.h>#include <math.h>//定义最大素数范围//定义RSA密钥结构体typedef structunsigned long long e; // 公钥指数unsigned long long d; // 私钥指数unsigned long long n; // 模数} RSAKey;//判断一个数是否为素数int isPrime(unsigned long long num)//小于等于1的数不是素数if (num <= 1) return 0;//判断是否存在因子for (unsigned long long i = 2; i <= sqrt(num); i++)if (num % i == 0)return 0;}}return 1;//生成一个指定范围内的随机素数unsigned long long generateRandomPrime(unsigned long long min, unsigned long long max)unsigned long long num;donum = rand( % (max - min + 1) + min;} while (!isPrime(num));return num;//求最大公约数unsigned long long gcd(unsigned long long a, unsigned long long b)unsigned long long temp;while (b != 0)temp = a % b;a=b;b = temp;}return a;//求模反元素unsigned long long modReverse(unsigned long long a, unsigned long long b)unsigned long long m0 = b, t, q;unsigned long long x0 = 0, x1 = 1;if (b == 1) return 0;while (a > 1)q=a/b;t=b;b=a%b;a=t;t=x0;x0=x1-q*x0;x1=t;}if (x1 < 0) x1 += m0;return x1;//生成RSA密钥对RSAKey generateRSAKeys(unsigned long long p, unsigned long long q)RSAKey keys;//计算模数keys.n = p * q;//计算欧拉函数值unsigned long long phi = (p - 1) * (q - 1);//选择公钥指数ekeys.e = generateRandomPrime(2, phi - 1);//计算私钥指数dkeys.d = modReverse(keys.e, phi);return keys;int mai//设置随机种子//生成两个不同的随机素数unsigned long long p = generateRandomPrime(2,MAX_PRIME_NUMBER);unsigned long long q = generateRandomPrime(2,MAX_PRIME_NUMBER);RSAKey keys = generateRSAKeys(p, q);printf("公钥指数e: %llu\n", keys.e);printf("私钥指数d: %llu\n", keys.d);printf("模数n: %llu\n", keys.n);return 0;```运行上述代码,即可生成RSA密钥对。
rsa加密解密算法c语言程序

rsa加密解密算法c语言程序RSA加密解密算法是一种公钥加密算法,发明于1977年,基于两个大质数的乘积难分解性,能够对短文本进行加解密。
以下是RSA加密解密算法的C语言程序。
一、密钥生成首先定义了一个结构体存储RSA密钥,该结构体包含三个元素:n、e和d。
- n = p * q,其中p和q为大质数;- e为与(p - 1) * (q - 1)互质的自然数,一般选取65537;- d为e模(p - 1) * (q - 1)的逆元素,即满足e * d ≡ 1 (mod (p - 1) * (q - 1)))的自然数。
generateRSAKey函数通过调用randomPrime函数生成两个大质数p和q,再通过Euclidean函数计算(p - 1) * (q - 1)的值phi,最后继续使用extendedEuclidean函数计算d的值,最终将生成的密钥存储在RSAKey结构体中。
```c#include <stdio.h>#include <stdlib.h>#include <time.h>#include <math.h>#define k 1024 // 密钥长度typedef struct {unsigned long long n;unsigned long long e;unsigned long long d;} RSAKey;unsigned long long randomPrime(unsigned long long n);unsigned long long gcd(unsigned long long a, unsigned long long b);unsigned long long Euclidean(unsigned long long a, unsigned long long b);RSAKey generateRSAKey();// 生成一个小于n的随机质数unsigned long long randomPrime(unsigned long long n) {unsigned long long p;do {p = rand() % n;while (!(p & 1)) // 确保p为奇数p = rand() % n;} while (gcd(p, n) != 1); // 确保p和n互质return p;}二、加密和解密下面定义了两个函数:encrypt和decrypt,其中encrypt函数用于将明文转换为密文,decrypt函数用于将密文转换为明文。
RSA的C语言算法实现

RSA的C语言算法实现RSA算法是一种非对称密码算法,用于加密和解密数据。
它是由三位数学家Rivest、Shamir和Adleman在1977年提出的,是目前最广泛使用的公钥加密算法之一RSA算法的实现需要以下步骤:1.选择两个大素数p和q,计算它们的乘积n=p*q。
n称为模数。
2.计算欧拉函数φ(n)=(p-1)*(q-1)。
3. 选择一个小于φ(n)的整数e,使得e与φ(n)互质,即gcd(e,φ(n)) = 1、e称为公钥指数。
4. 计算私钥指数d,满足(d * e) mod φ(n) = 1、d称为私钥指数。
5.公钥是(n,e),私钥是(n,d)。
6. 要加密消息m,计算c = m^e mod n,其中c是密文。
7. 要解密密文c,计算m = c^d mod n,其中m是原始消息。
下面是一个使用C语言实现RSA算法的示例:```c#include <stdio.h>#include <stdlib.h>typedef unsigned long long int ullong;ullong gcd(ullong a, ullong b)ullong temp;while (b != 0)temp = b;b=a%b;a = temp;}return a;ullong mod_inverse(ullong a, ullong m) ullong m0 = m;ullong y = 0, x = 1;if (m == 1)return 0;while (a > 1)ullong q = a / m;ullong t = m;m=a%m,a=t;t=y;y=x-q*y;x=t;}if (x < 0)x+=m0;return x;ullong mod_exp(ullong base, ullong exponent, ullong modulus) ullong result = 1;base = base % modulus;while (exponent > 0)if (exponent % 2 == 1)result = (result * base) % modulus;exponent = exponent >> 1;base = (base * base) % modulus;}return result;int mai//选择素数p和qullong p = 17;ullong q = 19;//计算模数n和欧拉函数φ(n)ullong n = p * q;ullong phi_n = (p - 1) * (q - 1);//选择公钥指数eullong e = 5;//计算私钥指数dullong d = mod_inverse(e, phi_n);//打印公钥和私钥printf("公钥: (%llu, %llu)\n", n, e); printf("私钥: (%llu, %llu)\n", n, d);//要加密的消息ullong m = 88;//加密消息ullong c = mod_exp(m, e, n);//打印加密结果printf("加密结果: %llu\n", c);//解密消息ullong decrypted_m = mod_exp(c, d, n); //打印解密结果printf("解密结果: %llu\n", decrypted_m);return 0;```这是一个简单的RSA实现示例,用于加密和解密一个整数。
加解密实验报告

一、实验模块1. 实验名称:加解密实验2. 实验目的:掌握基本的加密和解密方法,理解加密算法的工作原理,并能够使用C语言实现加解密功能。
3. 实验环境:Windows操作系统,C语言编译器(如Visual Studio)二、实验标题加解密实验:基于RSA算法和DES算法的加密与解密三、实验内容概述本次实验主要涉及两种加密算法:RSA算法和DES算法。
通过学习这两种算法的原理,使用C语言实现加解密功能,并验证加密和解密过程是否正确。
四、实验日期、实验操作者实验日期:2023年10月15日实验操作者:张三五、实验目的1. 理解RSA算法和DES算法的原理。
2. 使用C语言实现RSA算法和DES算法的加解密功能。
3. 验证加密和解密过程是否正确。
六、实验步骤1. RSA算法原理及实现(1)生成密钥对:使用C语言实现RSA算法的密钥生成过程,生成公钥和私钥。
(2)加密过程:使用公钥对明文进行加密,得到密文。
(3)解密过程:使用私钥对密文进行解密,得到明文。
2. DES算法原理及实现(1)密钥生成:生成DES算法的密钥,通常为56位。
(2)加密过程:使用DES算法对明文进行加密,得到密文。
(3)解密过程:使用DES算法对密文进行解密,得到明文。
3. 验证加密和解密过程(1)选择一段明文,分别使用RSA算法和DES算法进行加密,记录密文。
(2)使用相应的私钥或密钥对密文进行解密,得到明文。
(3)比较加密后的密文和解密后的明文,验证加密和解密过程是否正确。
七、实验环境1. 操作系统:Windows 102. 编译器:Visual Studio 20193. 编程语言:C语言八、实验过程1. RSA算法实验(1)生成密钥对```c#include <stdio.h>#include <stdlib.h>// RSA算法生成密钥对void generateKeys(int n, int e, int d, int p, int q) {// 初始化公钥和私钥p = 61;q = 53;n = p q;e = 17;// 计算私钥d = 2753;}int main() {int p, q, n, e, d;generateKeys(&p, &e, &d, &q, &n);printf("Public Key: (%d, %d)\n", n, e); printf("Private Key: (%d, %d)\n", n, d); return 0;}```(2)加密过程```c#include <stdio.h>#include <math.h>// RSA算法加密int encrypt(int m, int n, int e) {int c = pow(m, e) % n;return c;}int main() {int m, n, e, c;printf("Enter the message: ");scanf("%d", &m);printf("Public Key: (%d, %d)\n", n, e);c = encrypt(m, n, e);printf("Encrypted Message: %d\n", c);return 0;}```(3)解密过程```c// RSA算法解密int decrypt(int c, int n, int d) {int m = pow(c, d) % n;return m;}int main() {int c, n, d, m;printf("Enter the encrypted message: "); scanf("%d", &c);printf("Private Key: (%d, %d)\n", n, d); m = decrypt(c, n, d);printf("Decrypted Message: %d\n", m);return 0;}```2. DES算法实验(1)密钥生成```c#include <stdio.h>// DES算法密钥生成void generateDESKey(char key, char desKey) {// 将密钥转换为DES密钥for (int i = 0; i < 56; i++) {desKey[i] = key[i / 8] & (1 << (7 - (i % 8))); }}int main() {char key[8];char desKey[56];printf("Enter the DES key: ");scanf("%s", key);generateDESKey(key, desKey);printf("DES Key: ");for (int i = 0; i < 56; i++) {printf("%02x", desKey[i]);}printf("\n");return 0;}```(2)加密过程```c#include <stdio.h>// DES算法加密void encryptDES(char input, char key, char output) { // 使用DES算法对输入数据进行加密// 此处省略加密算法的具体实现}int main() {char input[64];char key[8];char output[64];printf("Enter the message: ");scanf("%s", input);printf("DES Key: ");scanf("%s", key);encryptDES(input, key, output);printf("Encrypted Message: %s\n", output);return 0;}```(3)解密过程```c#include <stdio.h>// DES算法解密void decryptDES(char input, char key, char output) { // 使用DES算法对输入数据进行解密// 此处省略解密算法的具体实现}int main() {char input[64];char key[8];char output[64];printf("Enter the encrypted message: ");scanf("%s", input);printf("DES Key: ");scanf("%s", key);decryptDES(input, key, output);printf("Decrypted Message: %s\n", output);return 0;}```3. 验证加密和解密过程(1)RSA算法验证```c#include <stdio.h>#include <math.h>// RSA算法加密int encrypt(int m, int n, int e) {int c = pow(m, e) % n;return c;}// RSA算法解密int decrypt(int c, int n, int d) {int m = pow(c, d) % n;return m;}int main() {int m, n, e, d, c, m2;printf("Enter the message: ");scanf("%d", &m);printf("Public Key: (%d, %d)\n", n, e);c = encrypt(m, n, e);printf("Encrypted Message: %d\n", c);printf("Enter the Private Key: (%d, %d)\n", n, d); m2 = decrypt(c, n, d);printf("Decrypted Message: %d\n", m2);if (m == m2) {printf("Encryption and Decryption are successful!\n"); } else {printf("Encryption and Decryption failed!\n");}return 0;}```(2)DES算法验证```c#include <stdio.h>// DES算法加密void encryptDES(char input, char key, char output) {// 使用DES算法对输入数据进行加密// 此处省略加密算法的具体实现}// DES算法解密void decryptDES(char input, char key, char output) {// 使用DES算法对输入数据进行解密// 此处省略解密算法的具体实现}int main() {char input[64];char key[8];char output[64];printf("Enter the message: ");scanf("%s", input);printf("DES Key: ");scanf("%s", key);encryptDES(input, key, output);printf("Encrypted Message: %s\n", output);decryptDES(output, key, input);printf("Decrypted Message: %s\n", input);if (strcmp(input, "Hello") == 0) {printf("Encryption and Decryption are successful!\n");} else {printf("Encryption and Decryption failed!\n");}return 0;}```九、实验结论通过本次实验,我们掌握了RSA算法和DES算法的原理,并使用C语言实现了加解密功能。
非对称加密解密算法RSA的C实现

非对称加密解密算法RSA的C实现RSA加密算法是一种非对称加密算法,常用于数据加密和数字签名。
其安全性基于大数分解的困难性,即质因数分解。
RSA加密算法的全称为Rivest-Shamir-Adleman加密算法,是由Ron Rivest、Adi Shamir和Leonard Adleman于1977年共同提出的。
RSA算法的加密过程如下:1.选择两个不同的质数p和q,计算它们的乘积n=p*q。
2.选择一个整数e,满足1<e<φ(n),且e和φ(n)互质,其中φ(n)=(p-1)*(q-1)。
3. 计算e关于φ(n)的模反元素d,即满足(e*d)mod φ(n) = 14.公钥为(n,e),私钥为(n,d)。
5. 对于要加密的明文m,使用公钥(n, e)进行加密,得到密文c,公式为c = (m^e)mod n。
6. 对于要解密的密文c,使用私钥(n, d)进行解密,得到明文m,公式为m = (c^d)mod n。
以下是使用C语言实现RSA加密和解密的代码:```c#include <stdio.h>#include <stdlib.h>#include <math.h>//求最大公约数int gcd(int a, int b)if (b == 0)return a;return gcd(b, a % b);//求模反元素int mod_inverse(int e, int phi) int d;for (d = 1; d <= phi; d++)if ((e * d) % phi == 1)return d;}return -1; // 模反元素不存在//加密函数int encrypt(int m, int e, int n) int c = fmod(pow(m, e), n); return c;//解密函数int decrypt(int c, int d, int n)int m = fmod(pow(c, d), n);return m;//主函数int maiint p, q, n, phi, e, d, m, c;printf("请输入两个质数p和q: ");scanf("%d%d", &p, &q);n=p*q;phi = (p - 1) * (q - 1);printf("请输入一个整数e(1 < e < %d且与%d互质): ", phi, phi);scanf("%d", &e);// 检查e和phi是否互质if (gcd(e, phi) != 1)printf("输入的e不符合要求,请重新输入!\n");return 0;}d = mod_inverse(e, phi);if (d == -1)printf("模反元素不存在,解密失败!\n");return 0;}printf("请输入要加密的明文m: ");scanf("%d", &m);c = encrypt(m, e, n);printf("加密后的密文c为: %d\n", c);m = decrypt(c, d, n);printf("解密后的明文m为: %d\n", m);return 0;```以上代码中,首先通过输入两个质数p和q,计算得到公共模数n和欧拉函数φ(n),然后要求输入一个符合条件的整数e,通过计算求得模反元素d。
RSA加解密算法C语言的实现

RSA加解密算法C语言的实现RSA(Rivest-Shamir-Adleman)是一种非对称加密算法,常用于保护网络通信的安全性。
它的主要思想是通过生成一对公钥和私钥,使用公钥进行加密,使用私钥进行解密,从而保证安全性。
RSA算法的实现一般包括生成密钥对、加密和解密三个部分。
1.生成密钥对RSA算法的第一步是生成一对公钥和私钥。
生成密钥对的过程如下:1)选择两个较大的质数p和q;2)计算N=p*q,确定模数N;3)计算欧拉函数φ(N)=(p-1)*(q-1);4)选择一个整数e,满足1<e<φ(N)且e与φ(N)互质;5)计算e关于模φ(N)的乘法逆元d,满足d * e ≡ 1 (modφ(N))。
生成密钥对的代码实现如下:```c#include <stdio.h>typedef unsigned long long int ulli;ulli gcd(ulli a, ulli b)if (b == 0)return a;}return gcd(b, a % b);ulli inverse(ulli e, ulli phi)ulli d = 0;ulli x1 = 0, x2 = 1, y1 = 1, y2 = 0; ulli temp_phi = phi;while (e > 0)ulli quotient = phi / e;ulli remainder = phi - quotient * e; phi = e;e = remainder;ulli x = x2 - quotient * x1;ulli y = y2 - quotient * y1;x2=x1;x1=x;y2=y1;y1=y;}if (phi != 1)return -1; // 没有乘法逆元}if (y2 < 0)d = temp_phi + y2;} elsed=y2;}return d;int mainulli p, q, N, phi, e, d;printf("Enter two prime numbers: ");scanf("%llu %llu", &p, &q);N=p*q;phi = (p - 1) * (q - 1);printf("Enter a number e such that 1 < e < phi(N) and gcd(e, phi(N)) = 1: ");scanf("%llu", &e);d = inverse(e, phi);printf("Public Key (N, e) = (%llu, %llu)\n", N, e);printf("Private Key (N, d) = (%llu, %llu)\n", N, d);return 0;```2.加密RSA算法的第二步是使用公钥进行加密。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<stdio.h>#include<string.h>#include <stdlib.h>#include <time.h>#include <math.h>#include <malloc.h>#define MAX 100#define LEN sizeof(struct slink)void sub(int a[MAX],int b[MAX] ,int c[MAX] );struct slink{int bignum[MAX];/*bignum[98]用来标记正负号,1正,0负bignum[99]来标记实际长度*/struct slink *next;};/*/--------------------------------------自己建立的大数运算库-------------------------------------*/void print( int a[MAX] ){int i;for(i=0;i<a[99];i++)printf("%d",a[a[99]-i-1]);printf("\n\n");return;}int cmp(int a1[MAX],int a2[MAX]){ int l1, l2;int i;l1=a1[99];l2=a2[99];if (l1>l2)return 1;if (l1<l2)return -1;for(i=(l1-1);i>=0;i--){if (a1[i]>a2[i])return 1 ;if (a1[i]<a2[i])return -1;}return 0;}void mov(int a[MAX],int *b){int j;for(j=0;j<MAX;j++)b[j]=a[j];return ;}void mul(int a1[MAX],int a2[MAX],int *c) {int i,j;int y;int x;int z;int w;int l1, l2;l1=a1[MAX-1];l2=a2[MAX-1];if (a1[MAX-2]=='-'&& a2[MAX-2]=='-') c[MAX-2]=0;else if (a1[MAX-2]=='-')c[MAX-2]='-';else if (a2[MAX-2]=='-')c[MAX-2]='-';for(i=0;i<l1;i++){for(j=0;j<l2;j++){x=a1[i]*a2[j];y=x/10;z=x%10;w=i+j;c[w]=c[w]+z;c[w+1]=c[w+1]+y+c[w]/10;c[w]=c[w]%10;}}w=l1+l2;if(c[w-1]==0)w=w-1;c[MAX-1]=w;return;}void add(int a1[MAX],int a2[MAX],int *c) {int i,l1,l2;int len,temp[MAX];int k=0;l1=a1[MAX-1];l2=a2[MAX-1];if((a1[MAX-2]=='-')&&(a2[MAX-2]=='-')) {c[MAX-2]='-';}else if (a1[MAX-2]=='-'){mov(a1,temp);temp[MAX-2]=0;sub(a2,temp,c);return;}else if (a2[MAX-2]=='-'){mov(a2,temp);temp[98]=0;sub(a1,temp,c);return;}if(l1<l2)len=l1;else len=l2;for(i=0;i<len;i++){c[i]=(a1[i]+a2[i]+k)%10;k=(a1[i]+a2[i]+k)/10;}if(l1>len){for(i=len;i<l1;i++){c[i]=(a1[i]+k)%10;k=(a1[i]+k)/10;}if(k!=0){c[l1]=k;len=l1+1;}else len=l1;}else{for(i=len;i<l2;i++){c[i]=(a2[i]+k)%10;k=(a2[i]+k)/10;}if(k!=0){c[l2]=k;len=l2+1;}else len=l2;}c[99]=len;return;}void sub(int a1[MAX],int a2[MAX],int *c) {int i,l1,l2;int len,t1[MAX],t2[MAX];int k=0;l1=a1[MAX-1];l2=a2[MAX-1];if ((a1[MAX-2]=='-') && (a2[MAX-2]=='-')) {mov(a1,t1);mov(a2,t2);t1[MAX-2]=0;t2[MAX-2]=0;sub(t2,t1,c);return;}else if( a2[MAX-2]=='-'){mov(a2,t2);t2[MAX-2]=0;add(a1,t2,c);return;}else if (a1[MAX-2]=='-'){mov(a2,t2);t2[MAX-2]='-';add(a1,t2,c);return;}if(cmp(a1,a2)==1){len=l2;for(i=0;i<len;i++){if ((a1[i]-k-a2[i])<0){c[i]=(a1[i]-a2[i]-k+10)%10; k=1;}else{c[i]=(a1[i]-a2[i]-k)%10; k=0;}}for(i=len;i<l1;i++){if ((a1[i]-k)<0){c[i]=(a1[i]-k+10)%10;k=1;}else{k=0;}}if(c[l1-1]==0)/*使得数组C中的前面所以0字符不显示了,如1000-20=0980--->显示为980了*/{len=l1-1;i=2;while (c[l1-i]==0)/*111456-111450=00006,消除0后变成了6;*/{len=l1-i;i++;}}else{len=l1;}}elseif(cmp(a1,a2)==(-1)){c[MAX-2]='-';len=l1;for(i=0;i<len;i++){if ((a2[i]-k-a1[i])<0){c[i]=(a2[i]-a1[i]-k+10)%10;k=1;}else{c[i]=(a2[i]-a1[i]-k)%10;k=0;}}for(i=len;i<l2;i++){if ((a2[i]-k)<0){k=1;}else{c[i]=(a2[i]-k)%10;k=0;}}if(c[l2-1]==0){len=l2-1;i=2;while (c[l1-i]==0){len=l1-i;i++;}}else len=l2;}else if(cmp(a1,a2)==0){len=1;c[len-1]=0;}c[MAX-1]=len;return;}void mod(int a[MAX],int b[MAX],int *c)/*/c=a mod b//注意:经检验知道此处A和C 的数组都改变了。
*/{ int d[MAX];mov (a,d);while (cmp(d,b)!=(-1))/*/c=a-b-b-b-b-b.......until(c<b)*/{sub(d,b,c);mov(c,d);/*/c复制给a*/}return ;void divt(int t[MAX],int b[MAX],int *c ,int *w)/*//试商法//调用以后w为a mod b, C为a div b;*/{int a1,b1,i,j,m;/*w用于暂时保存数据*/int d[MAX],e[MAX],f[MAX],g[MAX],a[MAX];mov(t,a);for(i=0;i<MAX;i++)e[i]=0;for(i=0;i<MAX;i++)d[i]=0;for(i=0;i<MAX;i++) g[i]=0;a1=a[MAX-1];b1=b[MAX-1];if (cmp(a,b)==(-1)){c[0]=0;c[MAX-1]=1;mov(t,w);return;}else if (cmp(a,b)==0){c[0]=1;c[MAX-1]=1;w[0]=0;w[MAX-1]=1;return;}m=(a1-b1);for(i=m;i>=0;i--)/*341245/3=341245-300000*1--->41245-30000*1--->11245-3000*3--->2245-300*7--->145-30*4=25--->25-3*8=1*/{for(j=0;j<MAX;j++)d[j]=0;d[i]=1;d[MAX-1]=i+1;mov(b,g);mul(g,d,e);while (cmp(a,e)!=(-1)){c[i]++;sub(a,e,f);mov(f,a);/*f复制给g*/}for(j=i;j<MAX;j++)/*高位清零*/e[j]=0;}mov(a,w);if (c[m]==0) c[MAX-1]=m;else c[MAX-1]=m+1;return;}void mulmod(int a[MAX] ,int b[MAX] ,int n[MAX],int *m)/*解决了 m=a*b mod n;*/ {int c[MAX],d[MAX];int i;for(i=0;i<MAX;i++)d[i]=c[i]=0;mul(a,b,c);divt(c,n, d,m);for(i=0;i<m[MAX-1];i++)printf("%d",m[m[MAX-1]-i-1]);printf("\nm length is : %d \n",m[MAX-1]);}/*接下来的重点任务是要着手解决 m=a^p mod n的函数问题。