资讯

精准传达 • 有效沟通

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

vb.netrsa的简单介绍

如何用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);

}

.net软件加密,加密锁哪种比较好,混淆代码可以实现吗?

加密锁:威步(WIBU)的CodeMeter,AxProtector(for.net)两款软件加密锁性能非常不错 混淆的问题,与传统的代码混淆工具(Obfuscator)不同,AxProtector可以完全阻止对.NET 程序集(由 C#, VB.NET, Delphi.NET, ASP.Net… 等语言编写)的反编译。通俗的讲,AxProtector在破解者和您的 .NET 代码之间构建了强大的防破解保护屏障,生成一个基于 Windows 的而不是基于 MSIL 的兼容格式文件。原始的 .NET 代码完整的被加密后封装在本地代码内,无论何时都不会释放到硬盘,对于破解者是不可见的。 与单纯的.net加密软件不同,AxProtector与CodeMeter硬件加密狗配套餐使用,采用了更为严密的密钥管理,及最先进的AES、RSA、ECC等加密算法存储或传输密钥,保证通讯安全。 .Net代码编译后生成的 .class 中包含有源代码中的所有信息(不包括注释),尤其是在其中保存有调试信息的时候。所以一个按照正常方式编译的.class 文件可以非常轻易地被反编译。一般软件开发商会采用一种叫做混淆器的工具。混淆器的作用是对编译好的代码进行混淆,使得其无法被反编译或者反编译后的代码混乱难懂。由于混淆器只是混淆了方法名称或流程,而不能防止源代码被反编译,因此混淆器的作用只是增加了反编译的难度,最终的结果也是治标不治本。对于一些掌握工具的人来说几乎还是透明的。AxProtector是一款真正意义的加密源代码、防止反编译的.net软件加密软件。 AxProtector加密了.net原代码,任何时候原代码都不可能被还原到硬盘当中。采用AxProtector加密后的.net代码只有在程序调用或执行某一段函数的时候,才能通过AxProtectorClass在内存中解密后返回到程序中执行,运行之后迅速立即加密。这种随机加密、按需解密原代码的功能,能很好的防止.Net程序的反编译,同时能够很好地防止API加密点被摘除。有效地保证了源代码的执行效率和安全性。


文章名称:vb.netrsa的简单介绍
转载源于:http://cdkjz.cn/article/dsgsspg.html
多年建站经验

多一份参考,总有益处

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

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

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220