導航:首頁 > 文檔加密 > 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加密相關的資料

熱點內容
伺服器一直崩應該用什麼指令 瀏覽:916
cm202貼片機編程 瀏覽:723
php構造函數帶參數 瀏覽:174
解壓電波歌曲大全 瀏覽:336
為啥文件夾移到桌面成word了 瀏覽:858
命令符的安全模式是哪個鍵 瀏覽:758
編程中學 瀏覽:956
單片機求助 瀏覽:993
ug加工側面排銑毛坯怎麼編程 瀏覽:271
程序員有關的介紹 瀏覽:736
支付寶使用的什麼伺服器 瀏覽:210
安卓看本地書用什麼軟體好 瀏覽:921
經傳軟體滾動凈利潤指標源碼 瀏覽:522
螢石雲視頻已加密怎麼解除 瀏覽:574
一命令四要求五建議 瀏覽:30
qq文件夾遷移不了 瀏覽:19
液體粘滯系數測定不確定度演算法 瀏覽:332
輕棧源碼 瀏覽:426
把圖片壓縮到500k 瀏覽:35
命令你自己 瀏覽:369