资源简介
非对称加密算法,RSA算法纯C语言代码实现,带测试demo

代码片段和文件信息
#include
#include
#include
#include
#include
#include
char buffer[1024];
const int MAX_DIGITS = 50;
int ij = 0;
struct public_key_class{
long long modulus;
long long exponent;
};
struct private_key_class{
long long modulus;
long long exponent;
};
// This should totally be in the math library.
long long gcd(long long a long long b)
{
long long c;
while ( a != 0 ) {
c = a; a = b%a; b = c;
}
return b;
}
long long ExtEuclid(long long a long long b)
{
long long x = 0 y = 1 u = 1 v = 0 gcd = b m n q r;
while (a!=0) {
q = gcd/a; r = gcd % a;
m = x-u*q; n = y-v*q;
gcd = a; a = r; x = u; y = v; u = m; v = n;
}
return y;
}
long long rsa_modExp(long long b long long e long long m)
{
if (b < 0 || e < 0 || m <= 0){
exit(1);
}
b = b % m;
if(e == 0) return 1;
if(e == 1) return b;
if( e % 2 == 0){
return ( rsa_modExp(b * b % m e/2 m) % m );
}
if( e % 2 == 1){
return ( b * rsa_modExp(b (e-1) m) % m );
}
}
// Calling this function will generate a public and private key and store them in the pointers
// it is given.
void rsa_gen_keys(struct public_key_class *pub struct private_key_class *priv char *PRIME_SOURCE_FILE)
{
FILE *primes_list;
if(!(primes_list = fopen(PRIME_SOURCE_FILE “r“))){
fprintf(stderr “Problem reading %s\n“ PRIME_SOURCE_FILE);
exit(1);
}
// count number of primes in the list
long long prime_count = 0;
do{
int bytes_read = fread(buffer1sizeof(buffer)-1 primes_list);
buffer[bytes_read] = ‘\0‘;
for (i=0 ; buffer[i]; i++){
if (buffer[i] == ‘\n‘){
prime_count++;
}
}
}
while(feof(primes_list) == 0);
// choose random primes from the list store them as pq
long long p = 0;
long long q = 0;
long long e = powl(2 8) + 1;
long long d = 0;
char prime_buffer[MAX_DIGITS];
long long max = 0;
long long phi_max = 0;
srand(time(NULL));
do{
// a and b are the positions of p and q in the list
int a = (double)rand() * (prime_count+1) / (RAND_MAX+1.0);
int b = (double)rand() * (prime_count+1) / (RAND_MAX+1.0);
// here we find the prime at position a store it as p
rewind(primes_list);
for(i=0; i < a + 1; i++){
// for(j=0; j < MAX_DIGITS; j++){
// prime_buffer[j] = 0;
// }
fgets(prime_buffersizeof(prime_buffer)-1 primes_list);
}
p = atol(prime_buffer);
// here we find the prime at position b store it as q
rewind(primes_list);
for(i=0; i < b + 1; i++){
for(j=0; j < MAX_DIGITS; j++){
prime_buffer[j] = 0;
}
fgets(prime_buffersizeof(prime_buffer)-1 primes_list);
}
q = atol(prime_buffer);
max = p*q;
phi_max = (p-1)*(q-1);
}
while(!(p && q) || (p == q) || (gcd(phi_max e) != 1));
// Next we need to choose ab so that a*max+b*e = gcd(maxe). We actually only need b
// here and in keeping with the
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5020 2018-06-29 12:55 RSA-Library-C\rsa.c
文件 1649 2018-06-29 12:55 RSA-Library-C\rsa.h
文件 1228 2018-06-29 12:55 RSA-Library-C\test.c
目录 0 2018-11-13 10:08 RSA-Library-C
----------- --------- ---------- ----- ----
7897 4
- 上一篇:VoIP高质量音视频传输技术研究
- 下一篇:AES算法采用verilog硬件实现
相关资源
- C++中头文件与源文件的作用详解
- C语言代码高亮html输出工具
- 猜数字游戏 c语言代码
- C语言课程设计
- 数字电位器C语言程序
- CCS FFT c语言算法
- 使用C语言编写的病房管理系统
- 通信过程中的RS编译码程序(c语言)
- 计算机二级C语言上机填空,改错,编
- 用回溯法解决八皇后问题C语言实现
- 简易教务管理系统c语言开发文档
- 操作系统课设 读写者问题 c语言实现
- 小波变换算法 c语言版
- C流程图生成器,用C语言代码 生成C语
- 3des加密算法C语言实现
- 简单的C语言点对点聊天程序
- 单片机c语言源程序(51定时器 八个按
- 个人日常财务管理系统(C语言)
- c语言电子商务系统
- 小甲鱼C语言课件 源代码
- 将图片转换为C语言数组的程序
- C语言实现的一个内存泄漏检测程序
- DES加密算法C语言实现
- LINUX下命令行界面的C语言细胞游戏
- 用单片机控制蜂鸣器播放旋律程序(
- 学校超市选址问题(数据结构C语言版
- 电子时钟 有C语言程序,PROTEUS仿真图
- 尚观培训linux许巍老师关于c语言的课
- 算符优先语法分析器(C语言编写)
- 基于C语言的密码锁程序
评论
共有 条评论