导航:首页 > 文档加密 > crsa加密

crsa加密

发布时间:2022-01-18 04:32:40

❶ C++实现RSA加密解密算法

#include <iostream>
using namespace std;

template <class HugeInt>
HugeInt Power( const HugeInt & x, const HugeInt & n, // 求x^n mod p
const HugeInt & p )
{
if( n == 0 )
return 1;

HugeInt tmp = Power( ( x * x ) % p, n / 2, p );

if( n % 2 != 0 )
tmp = ( tmp * x ) % p;

return tmp;
}

template <class HugeInt>
void fullGcd( const HugeInt & a, const HugeInt & b, //
HugeInt & x, HugeInt & y )
{
HugeInt x1, y1;

if( b == 0 )
{
x = 1;
y = 0;
}
else
{
fullGcd( b, a % b, x1, y1 );
x = y1;
y = x1 - ( a / b ) * y1;
}
}

template <class HugeInt>
HugeInt inverse( const HugeInt & p, const HugeInt & q, // 求d
const HugeInt & e )
{
int fyn = ( 1 - p ) * ( 1 - q );
HugeInt x, y;

fullGcd( fyn, e, x, y );
return x > 0 ? x : x + e;
}

int main( )
{
cout << "Please input the plaintext: " << endl;
int m;
cin >> m;
cout << "Please input p,q and e: " << endl;
int p, q, e;
cin >> p >> q >> e;
int n = p * q;
int d = inverse( p, q, e );
int C = Power( m, e, n );
cout << "The ciphertext is: " << C << endl;
cout << "\n\nPlease input the ciphertext: " << endl;
cin >> C;
cout << "\n\nPlease input p, q and d: " << endl;
cin >> p >> q >> d;
n = p * q;
m = Power( C, d, n );
cout <<"The plaintext is: " << m << endl << endl;

system( "pause" );
return 0;
}

这就是RSA加密解密算法

❷ C++ rsa加密解密算法

你的程序直接运行结束了,所以你什么都看不见。
你可以在你的MAIN函数最后一行加一句:
getchar();
或者
system("pause");

另外如果你输出的内容是非可见字符,那你也看不见,你下个断点,看看变量的值就看见了。

❸ 急急急!跪求RSA算法加密 C语言 最简单的就可以 要求可以直接运行出界面,请附带界面的

http://hi..com/rankabc/item/47340498e5542af61b49df1c

❹ 谁给用C或C++实现一个加密算法DES或者RSA的都行

发给你了一个对文本文件简单DES加密的源码,不是自己写的,从开源中国下载的(^_^为开源中国做下广告,应该可以抵消我利用其中的源码换积分的罪过吧^_^)

❺ windows c语言加密rsa公钥加密有哪些

RSA算法它是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。算法的名字以发明者的名字命名:Ron Rivest, Adi Shamir 和Leonard
Adleman。但RSA的安全性一直未能得到理论上的证明。它经历了各种攻击,至今未被完全攻破。

一、RSA算法 :

首先, 找出三个数, p, q, r,
其中 p, q 是两个相异的质数, r 是与 (p-1)(q-1) 互质的数
p, q, r 这三个数便是 private key

接着, 找出 m, 使得 rm == 1 mod (p-1)(q-1)
这个 m 一定存在, 因为 r 与 (p-1)(q-1) 互质, 用辗转相除法就可以得到了
再来, 计算 n = pq
m, n 这两个数便是 public key

编码过程是, 若资料为 a, 将其看成是一个大整数, 假设 a < n
如果 a >= n 的话, 就将 a 表成 s 进位 (s <= n, 通常取 s = 2^t),
则每一位数均小于 n, 然后分段编码
接下来, 计算 b == a^m mod n, (0 <= b < n),
b 就是编码后的资料

解码的过程是, 计算 c == b^r mod pq (0 <= c < pq),
于是乎, 解码完毕 等会会证明 c 和 a 其实是相等的 :)

如果第三者进行窃听时, 他会得到几个数: m, n(=pq), b
他如果要解码的话, 必须想办法得到 r
所以, 他必须先对 n 作质因数分解
要防止他分解, 最有效的方法是找两个非常的大质数 p, q,
使第三者作因数分解时发生困难

❻ 用C或者C++编写RSA加密算法,要求既能加密数字,又能加密字母!!

UpdateData(TRUE);
m_miwencode=_T("");
CKEY_PRODUCE rsa;
int codelenght,codenum;
codelenght=m_yuanwencode.GetLength();
codenum=codelenght/3;
CString strmod;
strmod.Format(_T("%d"),Model);
ModeNum=strmod.GetLength();
int Cryptograph;
for (int i=0;i<codenum;i++)
{
CString str;
str=m_yuanwencode.Mid(3*i,3);
int j=(str[0]-'0')*100+(str[1]-'0')*10+(str[2]-'0');
int temp= 1;
for(int k=0;k<PublicKey;k++)
{
temp *= j;
if( temp >= Model )
temp %= Model;
if( !temp )
Cryptograph = temp;
}
Cryptograph = temp % Model;
str.Format(_T("%d"),Cryptograph);
int strnum=str.GetLength();
if (strnum!=ModeNum)
{
int s=ModeNum-strnum;
if (s==1)
{
str=_T("0")+str;
}
if (s==2)
{
str=_T("00")+str;
}
if (s==3)
{
str=_T("000")+str;
}
if (s==4)
{
str=_T("0000")+str;
}
}
m_miwencode+=str;
}
UpdateData(FALSE);
m_miwencode=_T("");

vs2005编写的C++(mfc)程序。这个可以不,可以加密字符串,要的话把分给我,发你邮箱里

❼ 求助c rsa 分段加密

RSA算法它是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。算法的名字以发明者的名字命名:RonRivest,AdiShamir和LeonardAdleman。但RSA的安全性一直未能得到理论上的证明。它经历了各种攻击,至今未被完全攻破。一、RSA算法:首先,找出三个数,p,q,r,其中p,q是两个相异的质数,r是与(p-1)(q-1)互质的数p,q,r这三个数便是privatekey接着,找出m,使得rm==1mod(p-1)(q-1)这个m一定存在,因为r与(p-1)(q-1)互质,用辗转相除法就可以得到了再来,计算n=pqm,n这两个数便是publickey编码过程是,若资料为a,将其看成是一个大整数,假设a=n的话,就将a表成s进位(s因为rm==1mod(p-1)(q-1),所以rm=k(p-1)(q-1)+1,其中k是整数因为在molo中是preserve乘法的(x==ymodzan==vmodz=>xu==yvmodz),所以,c==b^r==(a^m)^r==a^(rm)==a^(k(p-1)(q-1)+1)modpq1.如果a不是p的倍数,也不是q的倍数时,则a^(p-1)==1modp(费马小定理)=>a^(k(p-1)(q-1))==1modpa^(q-1)==1modq(费马小定理)=>a^(k(p-1)(q-1))==1modq所以p,q均能整除a^(k(p-1)(q-1))-1=>pq|a^(k(p-1)(q-1))-1即a^(k(p-1)(q-1))==1modpq=>c==a^(k(p-1)(q-1)+1)==amodpq2.如果a是p的倍数,但不是q的倍数时,则a^(q-1)==1modq(费马小定理)=>a^(k(p-1)(q-1))==1modq=>c==a^(k(p-1)(q-1)+1)==amodq=>q|c-a因p|a=>c==a^(k(p-1)(q-1)+1)==0modp=>p|c-a所以,pq|c-a=>c==amodpq3.如果a是q的倍数,但不是p的倍数时,证明同上4.如果a同时是p和q的倍数时,则pq|a=>c==a^(k(p-1)(q-1)+1)==0modpq=>pq|c-a=>c==amodpqQ.E.D.这个定理说明a经过编码为b再经过解码为c时,a==cmodn(n=pq)但我们在做编码解码时,限制0intcandp(inta,intb,intc){intr=1;b=b+1;while(b!=1){r=r*a;r=r%c;b--;}printf("%d\n",r);returnr;}voidmain(){intp,q,e,d,m,n,t,c,r;chars;printf("pleaseinputthep,q:");scanf("%d%d",&p,&q);n=p*q;printf("thenis%3d\n",n);t=(p-1)*(q-1);printf("thetis%3d\n",t);printf("pleaseinputthee:");scanf("%d",&e);if(et){printf("eiserror,pleaseinputagain:");scanf("%d",&e);}d=1;while(((e*d)%t)!=1)d++;printf("thencaculateoutthatthedis%d\n",d);printf("thecipherpleaseinput1\n");printf("theplainpleaseinput2\n");scanf("%d",&r);switch(r){case1:printf("inputthem:");/*输入要加密的明文数字*/scanf("%d",&m);c=candp(m,e,n);printf("thecipheris%d\n",c);break;case2:printf("inputthec:");/*输入要解密的密文数字*/scanf("%d",&c);m=candp(c,d,n);printf("thecipheris%d\n",m);break;}getch();}

❽ RSA加密算法怎样用C语言实现 急急急!!!

/*数据只能是大写字母组成的字符串。
加密的时候,输入Y,然后输入要加密的文本(大写字母)
解密的时候,输入N,然后输入一个整数n表示密文的个数,然后n个整数表示加密时候得到的密文。
*/
/*RSA algorithm */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MM 7081
#define KK 1789
#define PHIM 6912
#define PP 85
typedef char strtype[10000];
int len;
long nume[10000];
int change[126];
char antichange[37];

void initialize()
{ int i;
char c;
for (i = 11, c = 'A'; c <= 'Z'; c ++, i ++)
{ change[c] = i;
antichange[i] = c;
}
}
void changetonum(strtype str)
{ int l = strlen(str), i;
len = 0;
memset(nume, 0, sizeof(nume));
for (i = 0; i < l; i ++)
{ nume[len] = nume[len] * 100 + change[str[i]];
if (i % 2 == 1) len ++;
}
if (i % 2 != 0) len ++;
}
long binamod(long numb, long k)
{ if (k == 0) return 1;
long curr = binamod (numb, k / 2);
if (k % 2 == 0)
return curr * curr % MM;
else return (curr * curr) % MM * numb % MM;
}
long encode(long numb)
{ return binamod(numb, KK);
}
long decode(long numb)
{ return binamod(numb, PP);
}
main()
{ strtype str;
int i, a1, a2;
long curr;
initialize();
puts("Input 'Y' if encoding, otherwise input 'N':");
gets(str);
if (str[0] == 'Y')
{ gets(str);
changetonum(str);
printf("encoded: ");
for (i = 0; i < len; i ++)
{ if (i) putchar('-');
printf(" %ld ", encode(nume[i]));
}
putchar('\n');
}
else
{ scanf("%d", &len);
for (i = 0; i < len; i ++)
{ scanf("%ld", &curr);
curr = decode(curr);
a1 = curr / 100;
a2 = curr % 100;
printf("decoded: ");
if (a1 != 0) putchar(antichange[a1]);
if (a2 != 0) putchar(antichange[a2]);
}
putchar('\n');
}
putchar('\n');
system("PAUSE");
return 0;
}

/*
测试:
输入:
Y
FERMAT
输出:
encoded: 5192 - 2604 - 4222
输入
N
3 5192 2604 4222
输出
decoded: FERMAT
*/

❾ 求 rsa简单加密算法 C代码,急!

#include<iostream>
#include<cmath>
using namespace std;

void main()
{
int p,q;
cout<<"请输入两个较大的素数:"<<endl;
cin>>p>>q;
cout<<"p="<<p<<",q="<<q<<endl;
int n,o;
n=p*q;
o=(p-1)*(q-1);
cout<<"n="<<n<<",o="<<o<<endl;
cout<<"请从【0,"<<o-1<<"】中选择一个与"<<o<<"互素的数e:"<<endl;
int e,i;
float d;
cin>>e;
for(i=1;;i++)
{
d=(float)(o*i+1)/e;
if(d-(int)d==0)
break;
}
cout<<"e="<<e<<",d="<<d<<endl;
cout<<"公开密钥Pk={e,n}={"<<e<<","<<n<<"}"<<endl;
cout<<"秘密密钥Sk={d,n}={"<<d<<","<<n<<"}"<<endl;
cout<<endl;

cout<<"请输入要加密的正整数(以-1结束):"<<endl;
int m1[500],m3[500],m4[500];
double m2[500];
int j;
for(j=0;j<500;j++)
{
cin>>m1[j];
if(m1[j]==-1)
break;
m2[j]=pow(m1[j],e);
m4[j]=m2[j]/n;
m3[j]=m2[j]-m4[j]*n;
}
cout<<"密文为:"<<endl;
int k;
for(k=0;k<j;k++)
cout<<m3[k]<<" ";
cout<<endl;
}

❿ 如何用C语言来使用openssl rsa进行公钥加密,已有公钥和明文

1. 本程序使用2048位密钥对,每次加密时,原始数据的最大长度为245字节,加密后的密文长度为256字节.(采用打PADDING 的加密方式)

2. 如果所加密数据长度大于245字节,请分多次加密,后将密文按顺序存储;解密时,每次读取256字节,进行解密,将解密后的数据依次按顺序存储,即可还原原始数据.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#define OPENSSLKEY "test.key"
#define PUBLICKEY "test_pub.key"
#define BUFFSIZE 1024
char *my_encrypt(char *str, char *path_key); //加密
char *my_decrypt(char *str, char *path_key); //解密
int main(void)
{
char *source = "i like dancing !!!";
char *ptf_en, *ptf_de;
printf("source is :%s\n", source);
//1.加密
ptf_en = my_encrypt(source, PUBLICKEY);
if (ptf_en == NULL){
return 0;
}else{
printf("ptf_en is :%s\n", ptf_en);
}
//2.解密
ptf_de = my_decrypt(ptf_en, OPENSSLKEY);
if (ptf_de == NULL){
return 0;
}else{
printf("ptf_de is :%s\n", ptf_de);
}
if(ptf_en) free(ptf_en);
if(ptf_de) free(ptf_de);
return 0;
}
//加密
char *my_encrypt(char *str, char *path_key)
{
char *p_en = NULL;
RSA *p_rsa = NULL;
FILE *file = NULL;
int lenth = 0; //flen为源文件长度, rsa_len为秘钥长度
//1.打开秘钥文件
if((file = fopen(path_key, "rb")) == NULL)
{
perror("fopen() error 111111111 ");
goto End;
}
//2.从公钥中获取 加密的秘钥
if((p_rsa = PEM_read_RSA_PUBKEY(file, NULL,NULL,NULL )) == NULL)
{
ERR_print_errors_fp(stdout);
goto End;
}
lenth = strlen(str);
p_en = (char *)malloc(256);
if(!p_en)
{
perror("malloc() error 2222222222");
goto End;
}
memset(p_en, 0, 256);
//5.对内容进行加密
if(RSA_public_encrypt(lenth, (unsigned char*)str, (unsigned char*)p_en, p_rsa, RSA_PKCS1_PADDING) < 0)
{
perror("RSA_public_encrypt() error 2222222222");
goto End;
}
End:
//6.释放秘钥空间, 关闭文件
if(p_rsa) RSA_free(p_rsa);
if(file) fclose(file);
return p_en;
}
//解密
char *my_decrypt(char *str, char *path_key)
{
char *p_de = NULL;
RSA *p_rsa = NULL;
FILE *file = NULL;
//1.打开秘钥文件
file = fopen(path_key, "rb");
if(!file)
{
perror("fopen() error 22222222222");
goto End;
}
//2.从私钥中获取 解密的秘钥
if((p_rsa = PEM_read_RSAPrivateKey(file, NULL,NULL,NULL )) == NULL)
{
ERR_print_errors_fp(stdout);
goto End;
}
p_de = (char *)malloc(245);
if(!p_de)
{
perror("malloc() error ");
goto End;
}
memset(p_de, 0, 245);
//5.对内容进行加密
if(RSA_private_decrypt(256, (unsigned char*)str, (unsigned char*)p_de, p_rsa, RSA_PKCS1_PADDING) < 0)
{
perror("RSA_public_encrypt() error ");
goto End;
}
End:
//6.释放秘钥空间, 关闭文件
if(p_rsa) RSA_free(p_rsa);
if(file) fclose(file);
return p_de;
}

阅读全文

与crsa加密相关的资料

热点内容
求知课堂python2020 浏览:260
kafka删除topic命令 浏览:759
phpsql单引号 浏览:86
英雄联盟压缩壁纸 浏览:452
办公app需要什么服务器 浏览:628
安卓服务器怎么获得 浏览:808
空调压缩机冷媒的作用 浏览:781
淘宝app是以什么为利的 浏览:657
java提取图片文字 浏览:924
我的世界手机版指令复制命令 浏览:35
java判断字符串为数字 浏览:926
androidrpc框架 浏览:490
云服务器essd和ssd 浏览:524
家用网关的加密方式 浏览:3
怎么从ppt导出pdf文件 浏览:973
换汽车空调压缩机轴承 浏览:845
平板怎么登录安卓端 浏览:197
图像拼接计算法 浏览:257
怎么打开饥荒服务器的本地文件夹 浏览:293
usb扫描枪编程 浏览:675