vb 的可以吗!!!我现在没有时间把她改写为c# 的
解决方案 »
- 求JS 多张图片轮换的简单完整例子!
- 问个DataTable.select()方法的问题
- 显示网络图片
- ListView高手来...在线等
- 怎样用触发器自动添加一条记录?
- 求助:.net编译执行的程序不出错,但是运行编译生成的exe文件程序操作过程中出错,请各位高手帮忙
- 请问谁有ActiveReport For .Net 正式版本提供下载?感激不尽!
- 这个结构怎么写?
- 不可访问“System.Windows.Forms.Control.text”,因为它受保护级别限制
- 请问用WebClient时,先通过了验证(Post),可是访问别的网页是,服务器端不能读取到cookie还是提示登陆
- 50分求助关于数据库更新的一个小问题(在线等,立即接贴)
- 继续混还是考研?
System.Security.Cryptography.RSA 类
下面的示例使用公钥信息加密对称公钥和 IV。初始化表示第三方公钥的两个字节数组。RSAParameters 对象被初始化为这些值。接着,使用 RSACryptoServiceProvider.ImportParameters 方法将 RSAParameters(及其所表示的公钥)导入到 RSACryptoServiceProvider 中。最后,加密 RijndaelManaged 类所创建的私钥和 IV。本示例要求系统安装有 128 位加密。using System;
using System.Security.Cryptography;class Class1
{
static void Main()
{
//initialze the byte arrays to the public key information.
byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,
207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,
108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,
240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,
168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,
38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,
106,99,179,68,175,211,164,116,64,148,226,254,172,147}; byte[] Exponent = {1,0,1};
//Values to store encrypted symmetric keys.
byte[] EncryptedSymmetricKey;
byte[] EncryptedSymmetricIV; //Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //Create a new instance of RSAParameters.
RSAParameters RSAKeyInfo = new RSAParameters(); //Set RSAKeyInfo to the public key values.
RSAKeyInfo.Modulus = PublicKey;
RSAKeyInfo.Exponent = Exponent; //Import key parameters into RSA.
RSA.ImportParameters(RSAKeyInfo); //Create a new instance of the RijndaelManaged class.
RijndaelManaged RM = new RijndaelManaged(); //Encrypt the symmetric key and IV.
EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false);
EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false);
}
}
生成公钥/私钥对的用户通常执行不对称解密。可以使用用于创建公钥/私钥对的 RSACryptoServiceProvider 来解密数据或者用密钥信息初始化一个新的 RSACryptoServiceProvider。只有使用与用于加密数据的公钥相对应的私钥,解密才能成功。
下面的示例阐释如何对表示一个对称密钥和 IV 的两个字节数组进行解密。//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();//Decrypt the symetric key and IV.
SymetricKey = RSA.Decrypt( EncryptedSymetricKey, false);
SymetricIV = RSA.Decrypt( EncryptedSymetricIV , false);
大该是 58 个字符//####################################################################
//RSA 方式加密
//说明KEY必须是XML的行式,返回的是字符串
//######################################################################
//RSA的加密函数
public string RSAEncrypt(string xmlPublicKey,string EncryptString )
{
byte[] PlainTextBArray;
byte[] CypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray = (new UnicodeEncoding()).GetBytes(EncryptString);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
//Result=(new UnicodeEncoding()).GetString(CypherTextBArray);
Result=Convert.ToBase64String(CypherTextBArray);
return Result;
}
//RSA的解密函数
public string RSADecrypt(string xmlPrivateKey, string DecryptString )
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
PlainTextBArray =Convert.FromBase64String(DecryptString);
DypherTextBArray=rsa.Decrypt(PlainTextBArray, false);
Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
//RSA 的密钥产升
//产生私钥 和公钥
public void RSAKey(out string xmlKeys,out string xmlPublicKey)
{
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
xmlKeys=rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
}
public void RSAKey(out string xmlKeys,out string xmlPublicKey)
{
RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
xmlKeys=rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
}