C # RSA encryption and decryption example no length limit

RSA is an asymmetric encryption algorithm. Since the characteristics of an algorithm, encryption and decryption processes with different keys, that is, public and private, and is widely used in the safety management of digital certificates. In a particular application, the public key encryption and private key for decryption, or public key and a private key used for digital signature for signature verification. Because asymmetric encryption algorithm parity complex, time-consuming, it is generally in a network environment RAS is applied to signature authentication, or small data transmission, such as AES symmetric key transport.

In the .Net framework, the auxiliary class only provides the default for the same size of data encryption and decryption key length. C # RSA article provides an example of an optimization algorithm, the data size may not be limited. This is for example the exchange of learning.

RSA 加密
public string RsaEncrypt(string rawInput, string publicKey)
{
if (string.IsNullOrEmpty(rawInput))
{
return string.Empty;
}

if(string.IsNullOrWhiteSpace(publicKey))
{
throw new ArgumentException("Invalid Public Key");
}

(var rsaProvider the RSACryptoServiceProvider new new = ()) the using
{
var inputBytes = Encoding.UTF8.GetBytes (rawInput); // meaningful string into a byte stream
rsaProvider.FromXmlString (publicKey); // Loading public
int bufferSize = (rsaProvider.KeySize / 8) - 11 ; // maximum monolith length
var Buffer = new new byte [bufferSize];
the using (the MemoryStream = new new inputStream the MemoryStream (inputBytes),
the outputStream the MemoryStream = new new ())
{
the while (to true)
{ encryption segment //
int = readsize InputStream.read (Buffer, 0, bufferSize);
IF (readsize <= 0)
{
BREAK;
}

var temp = new byte[readSize];
Array.Copy(buffer, 0, temp, 0, readSize);
var encryptedBytes = rsaProvider.Encrypt(temp, false);
outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);
}
return Convert.ToBase64String(outputStream.ToArray());//转化为字节流方便传输
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
RSA 解密
public string RsaDecrypt(string encryptedInput, string privateKey)
{
if (string.IsNullOrEmpty(encryptedInput))
{
return string.Empty;
}

if (string.IsNullOrWhiteSpace(privateKey))
{
throw new ArgumentException("Invalid Private Key");
}

using (var rsaProvider = new RSACryptoServiceProvider())
{
var inputBytes = Convert.FromBase64String(encryptedInput);
rsaProvider.FromXmlString(privateKey);
int bufferSize = rsaProvider.KeySize / 8;
var buffer = new byte[bufferSize];
using (MemoryStream inputStream = new MemoryStream(inputBytes),
outputStream = new MemoryStream())
{
while (true)
{
int readSize = inputStream.Read(buffer, 0, bufferSize);
if (readSize <= 0)
{
break;
}

var temp = new byte[readSize];
Array.Copy(buffer, 0, temp, 0, readSize);
var rawBytes = rsaProvider.Decrypt(temp, false);
outputStream.Write(rawBytes, 0, rawBytes.Length);
}
return Encoding.UTF8.GetString(outputStream.ToArray());
}
}
}

Guess you like

Origin www.cnblogs.com/Alex80/p/11541476.html