rsa加密解密c语言算法_C和C ++中的RSA算法(加密和解密)

rsa加密解密c语言算法

Here you will learn about RSA algorithm in C and C++.

在这里,您将了解C和C ++中的RSA算法。

RSA Algorithm is used to encrypt and decrypt data in modern computer systems and other electronic devices. RSA algorithm is an asymmetric cryptographic algorithm as it creates 2 different keys for the purpose of encryption and decryption. It is public key cryptography as one of the keys involved is made public. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman who first publicly described it in 1978.

RSA算法用于在现代计算机系统和其他电子设备中加密和解密数据。 RSA算法是一种非对称密码算法,因为它创建2个不同的密钥用于加密和解密。 这是公钥密码术,因为其中涉及的密钥之一是公开的。 RSA代表Ron Rivest,Adi Shamir和Leonard Adleman,他们于1978年首次公开描述它。

RSA makes use of prime numbers (arbitrary large numbers) to function. The public key is made available publicly (means to everyone) and only the person having the private key with them can decrypt the original message.

RSA利用质数(任意大数)起作用。 公钥是公开可用的(对所有人均适用),只有拥有私钥的人才能解密原始消息。

RSA Algorithm Block Diagram

Image Source

图片来源

RSA算法的工作 (Working of RSA Algorithm)

RSA involves use of public and private key for its operation. The keys are generated using the following steps:-

RSA涉及使用公钥和私钥进行操作。 密钥使用以下步骤生成:

  1. Two prime numbers are selected as p and q

    选择两个质数作为pq

  2. n = pq which is the modulus of both the keys.

    n = pq ,这是两个键的模数。

  3. Calculate totient = (p-1)(q-1)

    计算总含量=(p-1)(q-1)

  4. Choose e such that e > 1 and coprime to totient which means gcd (e, totient) must be equal to 1, e is the public key

    选择e ,使e> 1并乘以totoient ,这意味着gcd(e,totient)必须等于1e是公钥

  5. Choose d such that it satisfies the equation de = 1 + k (totient), d is the private key not known to everyone.

    选择d使其满足等式de = 1 + k(totient)d是每个人都不知道的私钥。

  6. Cipher text is calculated using the equation c = m^e mod n where m is the message.

    使用等式c = m ^ e mod n计算密文,其中m是消息。

  7. With the help of c and d we decrypt message using equation m = c^d mod n where d is the private key.

    借助cd,我们使用等式m = c ^ d mod n解密消息,其中d是私钥。

Note: If we take the two prime numbers very large it enhances security but requires implementation of Exponentiation by squaring algorithm and square and multiply algorithm for effective encryption and decryption. For simplicity the program is designed with relatively small prime numbers.

注意:如果我们将两个素数取得非常大,则会提高安全性,但需要通过平方算法和平方乘算法来实现幂运算,以实现有效的加密和解密。 为简单起见,程序设计为具有相对较小的质数。

Below is the implementation of this algorithm in C and C++.

下面是该算法在C和C ++中的实现。

C语言RSA算法程序 (Program for RSA Algorithm in C)

//Program for RSA asymmetric cryptographic algorithm
//for demonstration values are relatively small compared to practical application
 
#include<stdio.h>
#include<math.h>
 
//to find gcd
int gcd(int a, int h)
{
    int temp;
    while(1)
    {
        temp = a%h;
        if(temp==0)
        return h;
        a = h;
        h = temp;
    }
}
 
int main()
{
    //2 random prime numbers
    double p = 3;
    double q = 7;
    double n=p*q;
    double count;
    double totient = (p-1)*(q-1);
 
    //public key
    //e stands for encrypt
    double e=2;
 
    //for checking co-prime which satisfies e>1
    while(e<totient){
    count = gcd(e,totient);
    if(count==1)
        break;
    else
        e++;
    }
 
    //private key
    //d stands for decrypt
    double d;
 
    //k can be any arbitrary value
    double k = 2;
 
    //choosing d such that it satisfies d*e = 1 + k * totient
    d = (1 + (k*totient))/e;
    double msg = 12;
    double c = pow(msg,e);
    double m = pow(c,d);
    c=fmod(c,n);
    m=fmod(m,n);
 
    printf("Message data = %lf",msg);
    printf("\np = %lf",p);
    printf("\nq = %lf",q);
    printf("\nn = pq = %lf",n);
    printf("\ntotient = %lf",totient);
    printf("\ne = %lf",e);
    printf("\nd = %lf",d);
    printf("\nEncrypted data = %lf",c);
    printf("\nOriginal Message Sent = %lf",m);
 
    return 0;
}

C ++中的RSA算法程序 (Program for RSA Algorithm in C++)

//Program for RSA asymmetric cryptographic algorithm
//for demonstration values are relatively small compared to practical application
 
#include<iostream>
#include<math.h>
 
using namespace std;
 
//to find gcd
int gcd(int a, int h)
{
    int temp;
    while(1)
    {
        temp = a%h;
        if(temp==0)
        return h;
        a = h;
        h = temp;
    }
}
 
int main()
{
    //2 random prime numbers
    double p = 3;
    double q = 7;
    double n=p*q;
    double count;
    double totient = (p-1)*(q-1);
 
    //public key
    //e stands for encrypt
    double e=2;
 
    //for checking co-prime which satisfies e>1
    while(e<totient){
    count = gcd(e,totient);
    if(count==1)
        break;
    else
        e++;
    }
 
    //private key
    //d stands for decrypt
    double d;
 
    //k can be any arbitrary value
    double k = 2;
 
    //choosing d such that it satisfies d*e = 1 + k * totient
    d = (1 + (k*totient))/e;
    double msg = 12;
    double c = pow(msg,e);
    double m = pow(c,d);
    c=fmod(c,n);
    m=fmod(m,n);
 
    cout<<"Message data = "<<msg;
    cout<<"\n"<<"p = "<<p;
    cout<<"\n"<<"q = "<<q;
    cout<<"\n"<<"n = pq = "<<n;
    cout<<"\n"<<"totient = "<<totient;
    cout<<"\n"<<"e = "<<e;
    cout<<"\n"<<"d = "<<d;
    cout<<"\n"<<"Encrypted data = "<<c;
    cout<<"\n"<<"Original Message sent = "<<m;
 
    return 0;
}

Output

输出量

RSA Algorithm in C and C++ (Encryption and Decryption)

This article is submitted by Rahul Maheshwari. You can connect with him on facebook.

本文由Rahul Maheshwari提交 您可以在 Facebook上与他建立联系。

Comment below if you have any queries related to above program for rsa algorithm in C and C++.

如果您对C和C ++中的rsa算法的上述程序有任何疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/03/rsa-algorithm.html

rsa加密解密c语言算法

culing2941
关注 关注
  • 7
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RSA加解密算法 C语言实现
01-17
C语言编程实现经典非对称加密算法——RSA加密算法
RSA加密算法C语言实现
11-11
RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其一个加密,则需要用另一个才能解密RSA算法涉及三个参数,n、e1、e2。 其,n是两个大质数p、q的积,n的二进制表示时所占用的位数,就是所谓的密钥长度。 e1和e2是一对相关的值,e1可以任意取,但要求e1与(p-1)*(q-1)互质;再选择e2,要求(e2×e1)≡1(mod(p-1)×(q-1))。 (n,e1),(n,e2)就是密钥对。其(n,e1)为公钥,(n,e2)为私钥。 RSA加解密算法完全相同,设A为明文,B为密文,则:A≡B^e2( mod n);B≡A^e1 (mod n);(公钥加密体制,一般用公钥加密,私钥解密) e1和e2可以互换使用,即: A≡B^e1 (mod n);B≡A^e2( mod n);
C语言实现RSA的简单加解密
weixin_44116874的博客
10-30 1万+
C语言实现 RSA的简单加密解密 RSA我就不普及了,网上都有,现在用我的实验报告实现一下: 需要的数据有: 两个大素数p,q; n=pq; t=(p-1)(q-1); 公钥e满足(e,t)=1; 私钥d满足de==1(mod n); 加密公式c=m^e(mod n),c是密文,m是明文; 加密的时候注意明文m<n; 下面再展示一下代码块: //判断两个数是不是互素。 void gcd(int p,int q){ int temp1,temp2; //q=temp2*p+temp1 ; if
RSA加密算法(C语言实现)
热门推荐
haroroda的博客
06-02 8万+
RSA算法流程说明----适合密码学初学者看
C语言实现简单加密算法 凯撒密码 RSA算法 简介及实现_c语言凯撒加密函数(1)
最新发布
m0_60666921的博客
04-05 1002
在如今的万维网环境,如果A要向B发送数据,需要先加密这个数据,因为在一些不安全的网络环境上网,是很容易被拦截到发送的数据的,如果使用对称加密,密钥传递在网络被拦截了也一样可以解密,也不可能用线下告知消息接收方密钥的这种方式来进行通信。在这种情况下第一代非对称加密算法RSA出现了,RSA是1977年由在麻省理工学院工作的Ron Rivest、Adi Shamir和Leonard Adleman一起提出的,RSA就是他们三人姓氏开头字母拼在一起组成的。
RSA加密算法C语言实现
01-10
RSA加密算法C语言实现
RSA加密算法个人理解
oldwolf1987的专栏
03-31 3975
RSA加密算法   引用http://www.cfca.com.cn/zhishi/wz-012.htmRSA加密算法是最常用的非对称加密算法,CFCA在证书服务离不了它。但是有不少新来的同事对它不太了解,恰好看到一本书作者用实例对它进行了简化而生动的描述,使得高深的数学理论能够被容易地理解。我们经过整理和改写特别推荐给大家阅读,希望能够对时间紧张但是又想了解它的同事有所帮助。 
RSA加密算法
09-03 92
RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。 原理图: C# 代码实现: using System; using System.Collections.Generic; using System.Text; using Sys...
C语言RSA加密解密算法-_RSA加解密算法的演示,C语言实现
06-09
RSA
rsa.rar_RSA  C语言_RSA算法C++_RSA解密 C语言_rsa
09-14
RSA加密解密算法C语言实现,代码思路清晰简洁明了。
rsa.rar_RSA解密 C语言_rsa加密解密_site:www.pudn.com
09-24
RSA加密解密算法,C语言编程实现。通过算法实现,可以对输入的字符数字进行加密解密
RSA加密解密算法C语言源代码
11-04
RSA公开密钥密码体制。所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制。   在公开密钥密码体制加密密钥(即公开密钥)PK是公开信息,而解密密钥(即秘密密钥)SK是需要保密的。加密算法E和解密算法D也都是公开的。虽然秘密密钥SK是由公开密钥PK决定的,但却不能根据PK计算出SK。正是基于这种理论,1978年出现了著名的RSA算法,它通常是先生成一对RSA 密钥,其之一是保密密钥,由用户保存;另一个为公开密钥,可对外公开,甚至可在网络服务器注册。为提高保密强度,RSA密钥至少为500位长,一般推荐使用1024位。这就使加密的计算量很大。为减少计算量,在传送信息时,常采用传统加密方法与公开密钥加密方法相结合的方式,即信息采用改进的DES或IDEA对话密钥加密,然后使用RSA密钥加密对话密钥和信息摘要。对方收到信息后,用不同的密钥解密并可核对信息摘要。
RSA加密算法c语言
06-03
个人编的RSA加密程序,要想了解具体算法说明或者其他加密算法请关注我的博客----适合任何对算法理解不透彻的密码初学者和爱好者。
rsa加密解密算法C语言代码
12-26
rsa加密解密算法C语言代码 #include #include #include #include #include #include #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; }; /*/--------------------------------------自己建立的大数运算库-------------------------------------*/
RSA加密 C语言实现
04-07
配套博客:https://blog.csdn.net/qq_41739364/article/details/86775886
RSA加解密c语言实现).zip
12-08
RSA加解密c语言实现).zip
rsa.rar_RSA linux_rsa_加密解密 RSA C_加密解密 linux_加解密
09-20
RSA加解密算法代码C语言编写,可对字符串和文件加密
C语言实现简单的RSA加解密算法
qq_59460636的博客
11-04 1万+
使用c语言实现了简单的RSA加解密算法。把输入的数据当做了字符串,所以没有问题对于汉字,数字,字符都可以进行正确的加解密。 要求:输入两个素数,然后生成一个随机数,然后保存这些信息,选择加解密对其进行操作
C语言RSA加密解密算法: 深度探索与实现
qq_38334677的博客
08-15 3384
同时,我们也可以更方便地处理一些错误情况,例如,如果密钥文件不存在,或者密钥格式不正确,我们可以在loadPublicKey和loadPrivateKey方法抛出异常,然后在调用这些方法的地方捕获这些异常,并进行相应的处理。RSA_public_encrypt函数用于公钥加密,它接受四个参数:要加密的数据的长度,要加密的数据,加密后的数据,公钥,以及填充方式。需要注意的是,这个示例加密解密过程是非常简化的,实际的RSA加密解密过程可能需要处理更大的质数,并且需要更复杂的计算。
RSA加密解密算法C语言
11-07
RSA加密解密算法是一种非对称加密算法,它是由Ron Rivest、Adi Shamir和Leonard Adleman在1977年提出的。RSA算法的安全性基于大数分解的困难性,即将一个大数分解成两个质数的乘积。RSA算法加密过程是将明文先转换成数字,然后使用公钥进行加密,得到密文;解密过程是使用私钥对密文进行解密,得到明文。RSA算法在数字签名、密钥协商等领域有着广泛的应用。 以下是一个简单的C语言实现RSA加密解密算法的范例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/rsa.h> #include <openssl/pem.h> #define PUBLIC_KEY_FILE "public_key.pem" #define PRIVATE_KEY_FILE "private_key.pem" int main() { RSA *rsa = NULL; FILE *fp = NULL; char *plain_text = "Hello, RSA!"; char *cipher_text = NULL; char *decrypted_text = NULL; int plain_text_len = strlen(plain_text); int cipher_text_len = 0; int decrypted_text_len = 0; // 生成RSA密钥对 rsa = RSA_generate_key(1024, RSA_F4, NULL, NULL); if (rsa == NULL) { printf("Failed to generate RSA key pair!\n"); return -1; } // 将公钥写入文件 fp = fopen(PUBLIC_KEY_FILE, "w"); if (fp == NULL) { printf("Failed to open public key file!\n"); return -1; } PEM_write_RSAPublicKey(fp, rsa); fclose(fp); // 将私钥写入文件 fp = fopen(PRIVATE_KEY_FILE, "w"); if (fp == NULL) { printf("Failed to open private key file!\n"); return -1; } PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 0, NULL, NULL); fclose(fp); // 加密明文 cipher_text_len = RSA_size(rsa); cipher_text = (char *)malloc(cipher_text_len); memset(cipher_text, 0, cipher_text_len); RSA_public_encrypt(plain_text_len, (unsigned char *)plain_text, (unsigned char *)cipher_text, rsa, RSA_PKCS1_PADDING); // 解密密文 decrypted_text_len = RSA_size(rsa); decrypted_text = (char *)malloc(decrypted_text_len); memset(decrypted_text, 0, decrypted_text_len); RSA_private_decrypt(cipher_text_len, (unsigned char *)cipher_text, (unsigned char *)decrypted_text, rsa, RSA_PKCS1_PADDING); // 输出结果 printf("Plain text: %s\n", plain_text); printf("Cipher text: "); for (int i = 0; i < cipher_text_len; i++) { printf("%02x", (unsigned char)cipher_text[i]); } printf("\n"); printf("Decrypted text: %s\n", decrypted_text); // 释放资源 RSA_free(rsa); free(cipher_text); free(decrypted_text); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
写文章

热门文章

  • Python字符串比较 46457
  • Python将字符串转换为日期时间 34465
  • java二进制转化为十进制_用Java将二进制转换为十进制的程序 21887
  • indexerror_解决IndexError:Python中的列表索引超出范围 21565
  • 解决错误:左值必须作为赋值的左操作数 21063

您愿意向朋友推荐“博客详情页”吗?

  • 强烈不推荐
  • 不推荐
  • 一般般
  • 推荐
  • 强烈推荐
提交

最新文章

  • 最佳编程笔记本_2020年如何选择最佳笔记本电脑进行编程?
  • 解决Python中的“ TypeError:字符串索引必须为整数”
  • ue4 加快移动_加快移动网站的9种方法
2020年1066篇

目录

目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值

哆哆女性网建材公司免费名字起名大全家电商店起个什么名字周易里好的名字关于农副产品公司起名曹氏女生起名用若起名字婴儿网上起名建设网站广告剑鱼行动下载店铺免费起名取名打分手机网站优化报价默起名寓意好吗给女生起好的寓意的名字银行网站建设方案要怎么网络做推广营销猴王出世读后感50字黑色恐怖故事餐饮品牌设计调研女孩春天生下来起名字暗格里的秘密剧情介绍5g推广营销文案适合取名起名大全的诗词有那些怎么制作广告网站周公解梦豆包毒液观后感600字网站建设的建议周易免费测车牌内的建筑公司起名最准算命周易算命生辰八字准吗淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化