资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb.netrsa加密 net rsa加密

关于vb做的rsa加解密程序

:将方程x^4-px^3+q=0移项,得

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网页空间、营销软件、网站建设、衢州网站维护、网站推广。

x^4+q=px^3

可见,x^4≥0,则x^4+q0,所以px^30,即x0,本题也就是要求出使方程x^4-px^3+q=0有正整数解的素数p、q;

且素数p必定是奇素数,否则是偶素数的话,那么p=2,则方程成为:x^4+q=2x^3,即q=2x^3-x^4=x^3×(2-x)0,得出2-x0,即x2,则只能是x=1,代入方程:1^4+q=2×1^3,即1+q=2,解得q=1,不是素数,故p必定是奇素数。

分两种情形讨论:

情形一:当x为偶数时,设为x=2n,则有

(2n)^4+q=p×(2n)^3

16n^4+q=p×8n^3

上式右端是偶数,则左端的q必须为偶数,否则:左端奇偶相加得奇,不符。

而q作为素数,唯一的偶素数就是2,即q=2,则上式成为

16n^4+2=p×8n^3

两边同时除以2,得:8n^4+1=p×4n^3,显然,左端奇偶相加得奇,但右端为偶,矛盾。所以方程无偶整数解;

情形二:当x为奇数时,设为x=2n-1,则有

(2n-1)^4+q=p×(2n-1)^3

观察上式,右端为奇,则左端也必须为奇,而(2n-1)^4是奇,所以得出q必须为偶,故素数q=2,上式成为:

(2n-1)^4+2=p×(2n-1)^3,整理成:

p(2n-1)^3-(2n-1)^4=(2n-1)^3×[p-(2n-1)]=1×2

由于(2n-1)^3为奇,所以必有:(2n-1)^3=1,解得:n=1;

则:[p-(2n-1)]=2,解得:p=3;

综上,对于素数p、q,方程x^4-px^3+q=0有整数解,则p、q分别为3和2。

晕,再补一个!

1:编程实现 自动生成两个素数p,q

2:编程实现 计算n=p*q

f(n)=(p-1)(q-1)

3: 随机数e满足:0ef(n) and f(n)与e互为素数

4: 编程实现 计算d:d=e mod f(n)=1

如要加密m

5:编程实现计算c:

加密公式:c=m^e mod n

6:编程实现解密m:

解密公式:m=c^d mod n

Private Sub Command10_Click()

End

End Sub

Private Sub Command9_Click()

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

Text4.Text = ""

Text5.Text = ""

Text6.Text = ""

Text7.Text = ""

Text8.Text = ""

Text9.Text = ""

End Sub

Private Sub Command2_Click()

p = Val(Text1.Text)

q = Val(Text2.Text)

Text3.Text = Str$(p * q)

End Sub

Private Sub Command3_Click()

p = Val(Text1.Text)

q = Val(Text2.Text)

Text4.Text = Str$(p - 1) * (q - 1)

End Sub

如何用VB实现RSA加密算法,网上找到了一份代码,没有注释看不懂,请大神解释!!!

RSA算法非常简单,概述如下:

找两素数p和q

取n=p*q

取t=(p-1)*(q-1)

取任何一个数e,要求满足et并且e与t互素(就是最大公因数为1)

取d*e%t==1

这样最终得到三个数: n d e

设消息为数M (M n)

设c=(M**d)%n就得到了加密后的消息c

设m=(c**e)%n则 m == M,从而完成对c的解密。

注:**表示次方,上面两式中的d和e可以互换。

在对称加密中:

n d两个数构成公钥,可以告诉别人;

n e两个数构成私钥,e自己保留,不让任何人知道。

给别人发送的信息使用e加密,只要别人能用d解开就证明信息是由你发送的,构成了签名机制。

别人给你发送信息时使用d加密,这样只有拥有e的你能够对其解密。

rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解

从而在已知n d的情况下无法获得e;同样在已知n e的情况下无法

求得d。

二实践

接下来我们来一个实践,看看实际的操作:

找两个素数:

p=47

q=59

这样

n=p*q=2773

t=(p-1)*(q-1)=2668

取e=63,满足et并且e和t互素

用perl简单穷举可以获得满主 e*d%t ==1的数d:

C:\Tempperl -e "foreach $i (1..9999){ print($i),last if $i*63%2668==1 }"

847

即d=847

最终我们获得关键的

n=2773

d=847

e=63

取消息M=244我们看看

加密:

c=M**d%n = 244**847%2773

用perl的大数计算来算一下:

C:\Tempperl -Mbigint -e "print 244**847%2773"

465

即用d对M加密后获得加密信息c=465

解密:

我们可以用e来对加密后的c进行解密,还原M:

m=c**e%n=465**63%2773 :

C:\Tempperl -Mbigint -e "print 465**63%2773"

244

即用e对c解密后获得m=244 , 该值和原始信息M相等。

三字符串加密

把上面的过程集成一下我们就能实现一个对字符串加密解密的示例了。

每次取字符串中的一个字符的ascii值作为M进行计算,其输出为加密后16进制

的数的字符串形式,按3字节表示,如01F

代码如下:

#!/usr/bin/perl -w

#RSA 计算过程学习程序编写的测试程序

#watercloud 2003-8-12

#

use strict;

use Math::BigInt;

my %RSA_CORE = (n=2773,e=63,d=847); #p=47,q=59

my $N=new Math::BigInt($RSA_CORE{n});

my $E=new Math::BigInt($RSA_CORE{e});

my $D=new Math::BigInt($RSA_CORE{d});

print "N=$N D=$D E=$E\n";

sub RSA_ENCRYPT

{

my $r_mess = shift @_;

my ($c,$i,$M,$C,$cmess);

for($i=0;$i length($$r_mess);$i++)

{

$c=ord(substr($$r_mess,$i,1));

$M=Math::BigInt-new($c);

$C=$M-copy(); $C-bmodpow($D,$N);

$c=sprintf "%03X",$C;

$cmess.=$c;

}

return \$cmess;

}

sub RSA_DECRYPT

{

my $r_mess = shift @_;

my ($c,$i,$M,$C,$dmess);

for($i=0;$i length($$r_mess);$i+=3)

{

$c=substr($$r_mess,$i,3);

$c=hex($c);

$M=Math::BigInt-new($c);

$C=$M-copy(); $C-bmodpow($E,$N);

$c=chr($C);

$dmess.=$c;

}

return \$dmess;

}

my $mess="RSA 娃哈哈哈~~~";

$mess=$ARGV[0] if @ARGV = 1;

print "原始串:",$mess,"\n";

my $r_cmess = RSA_ENCRYPT(\$mess);

print "加密串:",$$r_cmess,"\n";

my $r_dmess = RSA_DECRYPT($r_cmess);

print "解密串:",$$r_dmess,"\n";

#EOF

测试一下:

C:\Tempperl rsa-test.pl

N=2773 D=847 E=63

原始串:RSA 娃哈哈哈~~~

加密串:5CB6CD6BC58A7709470AA74A0AA74A0AA74A6C70A46C70A46C70A4

解密串:RSA 娃哈哈哈~~~

C:\Tempperl rsa-test.pl 安全焦点(xfocus)

N=2773 D=847 E=63

原始串:安全焦点(xfocus)

加密串:3393EC12F0A466E0AA9510D025D7BA0712DC3379F47D51C325D67B

解密串:安全焦点(xfocus)

vb.net中实现rsa加密解密 急!急!

我觉得你的并不是RSA加密解密算法。

在.net的有一个System.Security.Cryptography的命名空间,里面有一RSACryptoServiceProvider的类用来对byte进行RSA加密解密。

具体例子如下:

using System;

using System.Security.Cryptography;

using System.Text;

class RSACSPSample

{

static void Main()

{

try

{

//Create a UnicodeEncoder to convert between byte array and string.

UnicodeEncoding ByteConverter = new UnicodeEncoding();

//Create byte arrays to hold original, encrypted, and decrypted data.

byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");

byte[] encryptedData;

byte[] decryptedData;

//Create a new instance of RSACryptoServiceProvider to generate

//public and private key data.

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//Pass the data to ENCRYPT, the public key information

//(using RSACryptoServiceProvider.ExportParameters(false),

//and a boolean flag specifying no OAEP padding.

encryptedData = RSAEncrypt(dataToEncrypt,RSA.ExportParameters(false), false);

//Pass the data to DECRYPT, the private key information

//(using RSACryptoServiceProvider.ExportParameters(true),

//and a boolean flag specifying no OAEP padding.

decryptedData = RSADecrypt(encryptedData,RSA.ExportParameters(true), false);

//Display the decrypted plaintext to the console.

Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));

}

catch(ArgumentNullException)

{

//Catch this exception in case the encryption did

//not succeed.

Console.WriteLine("Encryption failed.");

}

}

static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)

{

try

{

//Create a new instance of RSACryptoServiceProvider.

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//Import the RSA Key information. This only needs

//toinclude the public key information.

RSA.ImportParameters(RSAKeyInfo);

//Encrypt the passed byte array and specify OAEP padding.

//OAEP padding is only available on Microsoft Windows XP or

//later.

return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);

}

//Catch and display a CryptographicException

//to the console.

catch(CryptographicException e)

{

Console.WriteLine(e.Message);

return null;

}

}

static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)

{

try

{

//Create a new instance of RSACryptoServiceProvider.

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//Import the RSA Key information. This needs

//to include the private key information.

RSA.ImportParameters(RSAKeyInfo);

//Decrypt the passed byte array and specify OAEP padding.

//OAEP padding is only available on Microsoft Windows XP or

//later.

return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);

}

//Catch and display a CryptographicException

//to the console.

catch(CryptographicException e)

{

Console.WriteLine(e.ToString());

return null;

}

}

}

[Visual Basic]

Try

'Create a new RSACryptoServiceProvider object.

Dim RSA As New RSACryptoServiceProvider()

'Export the key information to an RSAParameters object.

'Pass false to export the public key information or pass

'true to export public and private key information.

Dim RSAParams As RSAParameters = RSA.ExportParameters(False)

Catch e As CryptographicException

'Catch this exception in case the encryption did

'not succeed.

Console.WriteLine(e.Message)

End Try

[C#]

try

{

//Create a new RSACryptoServiceProvider object.

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//Export the key information to an RSAParameters object.

//Pass false to export the public key information or pass

//true to export public and private key information.

RSAParameters RSAParams = RSA.ExportParameters(false);

}

catch(CryptographicException e)

{

//Catch this exception in case the encryption did

//not succeed.

Console.WriteLine(e.Message);

}


当前名称:vb.netrsa加密 net rsa加密
当前链接:http://cdkjz.cn/article/hihihg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220