當前位置:首頁 » 密碼管理 » mfcrsa加密

mfcrsa加密

發布時間: 2022-09-11 09:52:56

1. 如何用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,
使第三者作因數分解時發生困難
<定理>
若 p, q 是相異質數, rm == 1 mod (p-1)(q-1),
a 是任意一個正整數, b == a^m mod pq, c == b^r mod pq,
則 c == a mod pq

證明的過程, 會用到費馬小定理, 敘述如下:
m 是任一質數, n 是任一整數, 則 n^m == n mod m
(換另一句話說, 如果 n 和 m 互質, 則 n^(m-1) == 1 mod m)
運用一些基本的群論的知識, 就可以很容易地證出費馬小定理的

<證明>
因為 rm == 1 mod (p-1)(q-1), 所以 rm = k(p-1)(q-1) + 1, 其中 k 是整數
因為在 molo 中是 preserve 乘法的
(x == y mod z and u == v mod z => xu == yv mod z),
所以, c == b^r == (a^m)^r == a^(rm) == a^(k(p-1)(q-1)+1) mod pq

1. 如果 a 不是 p 的倍數, 也不是 q 的倍數時,
則 a^(p-1) == 1 mod p (費馬小定理) => a^(k(p-1)(q-1)) == 1 mod p
a^(q-1) == 1 mod q (費馬小定理) => a^(k(p-1)(q-1)) == 1 mod q
所以 p, q 均能整除 a^(k(p-1)(q-1)) - 1 => pq | a^(k(p-1)(q-1)) - 1
即 a^(k(p-1)(q-1)) == 1 mod pq
=> c == a^(k(p-1)(q-1)+1) == a mod pq

2. 如果 a 是 p 的倍數, 但不是 q 的倍數時,
則 a^(q-1) == 1 mod q (費馬小定理)
=> a^(k(p-1)(q-1)) == 1 mod q
=> c == a^(k(p-1)(q-1)+1) == a mod q
=> q | c - a
因 p | a
=> c == a^(k(p-1)(q-1)+1) == 0 mod p
=> p | c - a
所以, pq | c - a => c == a mod pq

3. 如果 a 是 q 的倍數, 但不是 p 的倍數時, 證明同上

4. 如果 a 同時是 p 和 q 的倍數時,
則 pq | a
=> c == a^(k(p-1)(q-1)+1) == 0 mod pq
=> pq | c - a
=> c == a mod pq
Q.E.D.

這個定理說明 a 經過編碼為 b 再經過解碼為 c 時, a == c mod n (n = pq)
但我們在做編碼解碼時, 限制 0 <= a < n, 0 <= c < n,
所以這就是說 a 等於 c, 所以這個過程確實能做到編碼解碼的功能

二、RSA 的安全性

RSA的安全性依賴於大數分解,但是否等同於大數分解一直未能得到理論上的證明,因為沒有證明破解
RSA就一定需要作大數分解。假設存在一種無須分解大數的演算法,那它肯定可以修改成為大數分解演算法。目前, RSA
的一些變種演算法已被證明等價於大數分解。不管怎樣,分解n是最顯然的攻擊方法。現在,人們已能分解多個十進制位的大素數。因此,模數n
必須選大一些,因具體適用情況而定。

三、RSA的速度

由於進行的都是大數計算,使得RSA最快的情況也比DES慢上倍,無論是軟體還是硬體實現。速度一直是RSA的缺陷。一般來說只用於少量數據加密。

四、RSA的選擇密文攻擊

RSA在選擇密文攻擊面前很脆弱。一般攻擊者是將某一信息作一下偽裝( Blind),讓擁有私鑰的實體簽署。然後,經過計算就可得到它所想要的信息。實際上,攻擊利用的都是同一個弱點,即存在這樣一個事實:乘冪保留了輸入的乘法結構:

( XM )^d = X^d *M^d mod n

前面已經提到,這個固有的問題來自於公鑰密碼系統的最有用的特徵--每個人都能使用公鑰。但從演算法上無法解決這一問題,主要措施有兩條:一條是採用好的公
鑰協議,保證工作過程中實體不對其他實體任意產生的信息解密,不對自己一無所知的信息簽名;另一條是決不對陌生人送來的隨機文檔簽名,簽名時首先使用
One-Way HashFunction 對文檔作HASH處理,或同時使用不同的簽名演算法。在中提到了幾種不同類型的攻擊方法。

五、RSA的公共模數攻擊

若系統中共有一個模數,只是不同的人擁有不同的e和d,系統將是危險的。最普遍的情況是同一信息用不同的公鑰加密,這些公鑰共模而且互質,那末該信息無需私鑰就可得到恢復。設P為信息明文,兩個加密密鑰為e1和e2,公共模數是n,則:

C1 = P^e1 mod n

C2 = P^e2 mod n

密碼分析者知道n、e1、e2、C1和C2,就能得到P。

因為e1和e2互質,故用Euclidean演算法能找到r和s,滿足:

r * e1 + s * e2 = 1

假設r為負數,需再用Euclidean演算法計算C1^(-1),則

( C1^(-1) )^(-r) * C2^s = P mod n

另外,還有其它幾種利用公共模數攻擊的方法。總之,如果知道給定模數的一對e和d,一是有利於攻擊者分解模數,一是有利於攻擊者計算出其它成對的e』和d』,而無需分解模數。解決辦法只有一個,那就是不要共享模數n。

RSA的小指數攻擊。 有一種提高 RSA速度的建議是使公鑰e取較小的值,這樣會使加密變得易於實現,速度有
所提高。但這樣作是不安全的,對付辦法就是e和d都取較大的值。

RSA演算法是
第一個能同時用於加密和數字簽名的演算法,也易於理解和操作。RSA是被研究得最廣泛的公鑰演算法,從提出到現在已近二十年,經歷了各種攻擊的考驗,逐漸為人
們接受,普遍認為是目前最優秀的公鑰方案之一。RSA的安全性依賴於大數的因子分解,但並沒有從理論上證明破譯RSA的難度與大數分解難度等價。即RSA
的重大缺陷是無法從理論上把握它的保密性能
如何,而且密碼學界多數人士傾向於因子分解不是NPC問題。
RSA的缺點主要有:A)產生密鑰很麻煩,受到素數產生技術的限制,因而難以做到一次一密。B)分組長度太大,為保證安全性,n 至少也要 600
bits
以上,使運算代價很高,尤其是速度較慢,較對稱密碼演算法慢幾個數量級;且隨著大數分解技術的發展,這個長度還在增加,不利於數據格式的標准化。目
前,SET( Secure Electronic Transaction )協議中要求CA採用比特長的密鑰,其他實體使用比特的密鑰。

C語言實現

#include <stdio.h>
int candp(int a,int b,int c)
{ int r=1;
b=b+1;
while(b!=1)
{
r=r*a;
r=r%c;
b--;
}
printf("%d\n",r);
return r;
}
void main()
{
int p,q,e,d,m,n,t,c,r;
char s;
printf("please input the p,q: ");
scanf("%d%d",&p,&q);
n=p*q;
printf("the n is %3d\n",n);
t=(p-1)*(q-1);
printf("the t is %3d\n",t);
printf("please input the e: ");
scanf("%d",&e);
if(e<1||e>t)
{
printf("e is error,please input again: ");
scanf("%d",&e);
}
d=1;
while(((e*d)%t)!=1) d++;
printf("then caculate out that the d is %d\n",d);
printf("the cipher please input 1\n");
printf("the plain please input 2\n");
scanf("%d",&r);
switch(r)
{
case 1: printf("input the m: "); /*輸入要加密的明文數字*/
scanf("%d",&m);
c=candp(m,e,n);
printf("the cipher is %d\n",c);break;
case 2: printf("input the c: "); /*輸入要解密的密文數字*/
scanf("%d",&c);
m=candp(c,d,n);
printf("the cipher is %d\n",m);break;
}
getch();
}

2. 用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)程序。這個可以不,可以加密字元串,要的話把分給我,發你郵箱里

3. 用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;
}

4. 電腦RSA是加密的那裡怎麼找到

這不用專門找啊,因為RSA的演算法本身就是公開的。是誰都可以用。

在很多地方你都能看到這個應用,例如支付寶和網銀的數字證書就是一個RSA加密的運用。

一般RSA在計算機應用都是以證書簽名的形式存在的。

5. RSA加解密原理以及三種填充模式

如果需要理解RSA的加密原理,需要理解以下理論:

​ 等同於求一元二次方程 23 * d + 192 * y = 1

​ 可以求得其中一解為(d=167,y=-20)

​ 至此就完成了所有的計算

​ 對於上述例子的到公鑰(221,23)和私鑰(221,167)

在上述的計算過程中一共用到了

上面用到的數中只有公鑰部分是公開的,即(221,23)。那麼我們是否可以通過公鑰來推到出私鑰部分,即已知n和e,推到出d?

(1)ed 1(mod (n)),只有知道 (n)才能解出d

(2) (n)= (p) (q)= (p-1) (q-1),只有知道p和q才能得到 (n)

(3)n=p q,就需要對n進行因式分解

那麼如果可以對n因式分解就可以求出d,也就意味著私匙被破解

那麼RSA加密的可靠性就在於對n因式分解的難度,而現在對一個整數n做因式分解並沒有巧妙的演算法,只有通過暴力破解計算。在實際應用中的n取值通常在1024位以上,而公開已知的可因式分解的最大數為768位。所以現階段來說RSA加密是可靠的。

現在我們就可以進行加密和解密了

我們使用上面生成的公鑰(221,23)來加密。如果我們需要加密的信息是m( m必須為整數並且m要小於n ),m取56,可以用以下公式求出加密串c:

c (mod n)

10 (mod 221)

可以求出加密後的結果c為10

密鑰為(221,167),加密結果c=10,可以使用以下公式求出被加密的信息

m (mod n) 即加密結果的d次方除以n的余數為m

56 (mod 221)

RSA加密屬於塊加密演算法,總是在一個固定長度的塊上進行操作。如果被加密的字元串過長,則需要對字元串進行切割,如果字元串過短則需要進行填充。

以下主介紹一下RSA_PKCS1_PADDING填充模式以及RSA_NO_PADDING模式

此填充模式是最常用的填充模式,在此填充模式下輸入的長度受加密鑰的長度限制,輸入的最大長度為加密鑰的位數k-11。如果公鑰的長度為1024位即128位元組,那麼輸入的長度最多為128-11=117位元組。如果長度小於117就需要填充。如果輸入T的長度為55位元組,填充後的塊為EM,則EM格式如下:

EM= 0x00 || BT || PS || 0x00 || T

在此填充模式下,輸入的長度最多和RSA公鑰長度一樣長,如果小於公鑰長度則會在前面填充0x00。如果公鑰長度是128位元組,輸入T的長度為55位元組,填充後的塊為EM,則EM格式如下:

EM=P || T

參考:
http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html
http://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html
https://my.oschina.net/3pgp/blog/749195

6. rsa演算法加密演算法的實現問題

RSA加密是把數據當作數值運算,而且會進行大數運算,加密演算法很慢,建議加密小的數據可採用。你把任何的數據流當位元組流來讀取,那每個位元組就是就是一個數了,分組取決你使用的模長,比如rsa1024,那麼每次分片可加密數據的大小是,1024/8-11=117個,為什麼減11參見RSA理論。解密每片是1024/8=128個。

7. 用RSA演算法設計加密軟體 C語言或者C++

UpdateData(TRUE);

m_miwencode=_T("");

CKEY_PRODUCErsa;

intcodelenght,codenum;

codelenght=m_yuanwencode.GetLength();

codenum=codelenght/3;

CStringstrmod;

strmod.Format(_T("%d"),Model);

ModeNum=strmod.GetLength();

intCryptograph;

for(inti=0;i<codenum;i++)

{

CStringstr;

str=m_yuanwencode.Mid(3*i,3);

intj=(str[0]-'0')*100+(str[1]-'0')*10+(str[2]-'0');

inttemp=1;

for(intk=0;k<PublicKey;k++)

{

temp*=j;

if(temp>=Model)

temp%=Model;

if(!temp)

Cryptograph=temp;

}

Cryptograph=temp%Model;

str.Format(_T("%d"),Cryptograph);

intstrnum=str.GetLength();

if(strnum!=ModeNum)

{

ints=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)程序。這個可以不,可以加密字元串,要的話把分給我,發你郵箱里

8. 怎麼用vc++6.0創建一個RSA加密演算法的圖形界面工程 步驟盡量完整哈 謝謝哈

圖形界面用MFC,加密可以用CSP.

9. 用c#實現Rsa加密和解密

using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace Microsoft.Samples.Security.PublicKey
{
class App
{
// Main entry point
static void Main(string[] args)
{
// Instantiate 3 People for example. See the Person class below
Person alice = new Person("Alice");
Person bob = new Person("Bob");
Person steve = new Person("Steve");

// Messages that will exchanged. See CipherMessage class below
CipherMessage aliceMessage;
CipherMessage bobMessage;
CipherMessage steveMessage;

// Example of encrypting/decrypting your own message
Console.WriteLine("Encrypting/Decrypting Your Own Message");
Console.WriteLine("-----------------------------------------");

// Alice encrypts a message using her own public key
aliceMessage = alice.EncryptMessage("Alice wrote this message");
// then using her private key can decrypt the message
alice.DecryptMessage(aliceMessage);
// Example of Exchanging Keys and Messages
Console.WriteLine();
Console.WriteLine("Exchanging Keys and Messages");
Console.WriteLine("-----------------------------------------");

// Alice Sends a of her public key to Bob and Steve
bob.GetPublicKey(alice);
steve.GetPublicKey(alice);

// Bob and Steve both encrypt messages to send to Alice
bobMessage = bob.EncryptMessage("Hi Alice! - Bob.");
steveMessage = steve.EncryptMessage("How are you? - Steve");

// Alice can decrypt and read both messages
alice.DecryptMessage(bobMessage);
alice.DecryptMessage(steveMessage);

Console.WriteLine();
Console.WriteLine("Private Key required to read the messages");
Console.WriteLine("-----------------------------------------");

// Steve cannot read the message that Bob encrypted
steve.DecryptMessage(bobMessage);
// Not even Bob can use the Message he encrypted for Alice.
// The RSA private key is required to decrypt the RS2 key used
// in the decryption.
bob.DecryptMessage(bobMessage);

} // method Main
} // class App

class CipherMessage
{
public byte[] cipherBytes; // RC2 encrypted message text
public byte[] rc2Key; // RSA encrypted rc2 key
public byte[] rc2IV; // RC2 initialization vector
}

class Person
{
private RSACryptoServiceProvider rsa;
private RC2CryptoServiceProvider rc2;
private string name;

// Maximum key size for the RC2 algorithm
const int keySize = 128;

// Person constructor
public Person(string p_Name)
{
rsa = new RSACryptoServiceProvider();
rc2 = new RC2CryptoServiceProvider();
rc2.KeySize = keySize;
name = p_Name;
}

// Used to send the rsa public key parameters
public RSAParameters SendPublicKey()
{
RSAParameters result = new RSAParameters();
try
{
result = rsa.ExportParameters(false);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
return result;
}

// Used to import the rsa public key parameters
public void GetPublicKey(Person receiver)
{
try
{
rsa.ImportParameters(receiver.SendPublicKey());
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
}

public CipherMessage EncryptMessage(string text)
{
// Convert string to a byte array
CipherMessage message = new CipherMessage();
byte[] plainBytes = Encoding.Unicode.GetBytes(text.ToCharArray());

// A new key and iv are generated for every message
rc2.GenerateKey();
rc2.GenerateIV();

// The rc2 initialization doesnt need to be encrypted, but will
// be used in conjunction with the key to decrypt the message.
message.rc2IV = rc2.IV;
try
{
// Encrypt the RC2 key using RSA encryption
message.rc2Key = rsa.Encrypt(rc2.Key, false);
}
catch (CryptographicException e)
{
// The High Encryption Pack is required to run this sample
// because we are using a 128-bit key. See the readme for
// additional information.
Console.WriteLine("Encryption Failed. Ensure that the" +
" High Encryption Pack is installed.");
Console.WriteLine("Error Message: " + e.Message);
Environment.Exit(0);
}
// Encrypt the Text Message using RC2 (Symmetric algorithm)
ICryptoTransform sse = rc2.CreateEncryptor();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
try
{
cs.Write(plainBytes, 0, plainBytes.Length);
cs.FlushFinalBlock();
message.cipherBytes = ms.ToArray();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
ms.Close();
cs.Close();
}
return message;
} // method EncryptMessage

public void DecryptMessage(CipherMessage message)
{
// Get the RC2 Key and Initialization Vector
rc2.IV = message.rc2IV;
try
{
// Try decrypting the rc2 key
rc2.Key = rsa.Decrypt(message.rc2Key, false);
}
catch (CryptographicException e)
{
Console.WriteLine("Decryption Failed: " + e.Message);
return;
}

ICryptoTransform ssd = rc2.CreateDecryptor();
// Put the encrypted message in a memorystream
MemoryStream ms = new MemoryStream(message.cipherBytes);
// the CryptoStream will read cipher text from the MemoryStream
CryptoStream cs = new CryptoStream(ms, ssd, CryptoStreamMode.Read);
byte[] initialText = new Byte[message.cipherBytes.Length];

try
{
// Decrypt the message and store in byte array
cs.Read(initialText, 0, initialText.Length);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
ms.Close();
cs.Close();
}

// Display the message received
Console.WriteLine(name + " received the following message:");
Console.WriteLine(" " + Encoding.Unicode.GetString(initialText));
} // method DecryptMessage
} // class Person
} // namespace PublicKey

熱點內容
android開發sd卡 發布:2025-07-04 15:50:28 瀏覽:948
離歌腳本 發布:2025-07-04 15:50:13 瀏覽:414
距估計演算法 發布:2025-07-04 15:48:50 瀏覽:813
安卓手機的號碼在哪裡看 發布:2025-07-04 15:36:53 瀏覽:27
蒲公英路由器伺服器端ip 發布:2025-07-04 15:20:30 瀏覽:678
python學習中 發布:2025-07-04 15:20:26 瀏覽:257
linux查看cuda版本 發布:2025-07-04 15:15:49 瀏覽:44
反編譯瀏覽器 發布:2025-07-04 15:15:45 瀏覽:453
java直播網站源碼 發布:2025-07-04 14:46:35 瀏覽:170
安卓應用市場消費記錄怎麼刪除 發布:2025-07-04 14:39:47 瀏覽:31