RSA 非对称加密与解密
2008-12-01 15:23
344 查看
private static string Encrypt(string msg, string publickey)
{
ASCIIEncoding enc = new ASCIIEncoding();
byte[] bytes = enc.GetBytes(msg);
int blockSize = 0;
RSACryptoServiceProvider crypt = new RSACryptoServiceProvider();
crypt.FromXmlString(publickey);
if (crypt.KeySize == 1024)
blockSize = 16;
else
blockSize = 8;
MemoryStream ms = new MemoryStream();
byte[] rawblock, encryblock;
for (int i = 0; i < bytes.Length; i += blockSize)
{
if ((bytes.Length - i) > blockSize)
rawblock = new byte[blockSize];
else
rawblock = new byte[bytes.Length - i];
Buffer.BlockCopy(bytes, i, rawblock, 0, rawblock.Length);
encryblock = crypt.Encrypt(rawblock, false);
ms.Write(encryblock, 0, encryblock.Length);
}
ms.Position = 0;
byte[] decode = new byte[ms.Length];
ms.Read(decode, 0, (int)ms.Length);
string decodeinfo = Convert.ToBase64String(decode);
ms.Close();
return decodeinfo;
}
private static string Decrypt(string msg, string privatekey)
{
RSACryptoServiceProvider crypt = new RSACryptoServiceProvider();
crypt.FromXmlString(privatekey);
ASCIIEncoding enc = new ASCIIEncoding();
byte[] bytes = Convert.FromBase64String(msg);
MemoryStream ms = new MemoryStream();
int keySize = crypt.KeySize / 8;
byte[] rawblock, decryptblock;
for (int i = 0; i < bytes.Length; i += keySize)
{
if ((bytes.Length - i) > keySize)
{
rawblock = new byte[keySize];
}
else
{ rawblock = new byte[bytes.Length - i]; }
Buffer.BlockCopy(bytes, i, rawblock, 0, rawblock.Length);
decryptblock = crypt.Decrypt(rawblock, false);
ms.Write(decryptblock, 0, decryptblock.Length);
}
ms.Position = 0;
byte[] decode = new byte[ms.Length];
ms.Read(decode, 0, (int)ms.Length);
string text = enc.GetString(decode);
ms.Close();
return text;
}
{
ASCIIEncoding enc = new ASCIIEncoding();
byte[] bytes = enc.GetBytes(msg);
int blockSize = 0;
RSACryptoServiceProvider crypt = new RSACryptoServiceProvider();
crypt.FromXmlString(publickey);
if (crypt.KeySize == 1024)
blockSize = 16;
else
blockSize = 8;
MemoryStream ms = new MemoryStream();
byte[] rawblock, encryblock;
for (int i = 0; i < bytes.Length; i += blockSize)
{
if ((bytes.Length - i) > blockSize)
rawblock = new byte[blockSize];
else
rawblock = new byte[bytes.Length - i];
Buffer.BlockCopy(bytes, i, rawblock, 0, rawblock.Length);
encryblock = crypt.Encrypt(rawblock, false);
ms.Write(encryblock, 0, encryblock.Length);
}
ms.Position = 0;
byte[] decode = new byte[ms.Length];
ms.Read(decode, 0, (int)ms.Length);
string decodeinfo = Convert.ToBase64String(decode);
ms.Close();
return decodeinfo;
}
private static string Decrypt(string msg, string privatekey)
{
RSACryptoServiceProvider crypt = new RSACryptoServiceProvider();
crypt.FromXmlString(privatekey);
ASCIIEncoding enc = new ASCIIEncoding();
byte[] bytes = Convert.FromBase64String(msg);
MemoryStream ms = new MemoryStream();
int keySize = crypt.KeySize / 8;
byte[] rawblock, decryptblock;
for (int i = 0; i < bytes.Length; i += keySize)
{
if ((bytes.Length - i) > keySize)
{
rawblock = new byte[keySize];
}
else
{ rawblock = new byte[bytes.Length - i]; }
Buffer.BlockCopy(bytes, i, rawblock, 0, rawblock.Length);
decryptblock = crypt.Decrypt(rawblock, false);
ms.Write(decryptblock, 0, decryptblock.Length);
}
ms.Position = 0;
byte[] decode = new byte[ms.Length];
ms.Read(decode, 0, (int)ms.Length);
string text = enc.GetString(decode);
ms.Close();
return text;
}
相关文章推荐
- 加密传到后台的数据,使用RSA非对称加密,前端加密,后台java解密
- ios学习--RSA非对称加密(解决解密后会乱码的问题)
- Python下RSA加密/解密, 签名/验证
- java RSA加密解密实现
- php rsa加密解密实例
- Cryptopp iOS 使用 RSA加密解密和签名验证签名
- RSA的公钥和私钥到底哪个才是用来加密和哪个用来解密
- iOS RSA加解密以及签名验签
- RSA - Java编程: 用Cipher进行RSA加解密
- RSA 加解密 签名 示例
- RSA加密解密
- RSA加密和3DES加解密
- python下RSA加密解密以及跨平台问题
- PHP RSA密文过长加密解密 越过1024的解决代码
- C#使用RSA证书文件加密和解密示例
- 关于非对称加密RSA
- 使用X.509数字证书加密解密实务(二)-- 使用RSA证书加密敏感数据
- C#实现RSA加密和解密详解
- Rsa加密解密