class Program
{
static void Main(string[] args)
{
var key = KeyPair.CreateNew(512);
Console.WriteLine(key.Public);
Console.WriteLine(key.Private);
var raw = Encoding.ASCII.GetBytes("Hello Wu");
Console.WriteLine(raw);
var r = KeyPair.Encrypt(raw, key);
Console.WriteLine(r);
var signature = KeyPair.Sign(key, r);
Console.WriteLine(signature);
var success = KeyPair.VerifyData(key, r, signature);
Console.WriteLine(success);
var u = Encoding.ASCII.GetString(KeyPair.Decrypt(r, key));
Console.WriteLine(u);
Console.ReadKey();
}
}
KeyPair.cs代码:
publicclass KeyPair
{
publicstring Public { get; set; }
publicstring Private { get; set; }
publicKeyPair(string pu, string pr)
{
Public = pu;
Private = pr;
}
publicstaticbyte[] Encrypt(byte[] data, KeyPair kp)
{
byte[] raw;
using (var rsa = new RSACryptoServiceProvider())
{
//导入表示 RSA 密钥信息的 blob
rsa.ImportCspBlob(Decompress(Base32.FromBase32String(kp.Public)));
//使用 RSA 算法加密数据//true 若要直接执行 RSA 使用 OAEP 填充 (仅可在运行 Windows XP 的计算机上或更高版本) 的加密; 否则为 false 使用 PKCS #1 v1.5 填充。
raw = rsa.Encrypt(data, true);
}
return raw;
}
publicstaticbyte[] Decrypt(byte[] data, KeyPair kp)
{
byte[] raw;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(Decompress(Base32.FromBase32String(kp.Private)));
raw = rsa.Decrypt(data, true);
}
return raw;
}
publicstaticbyte[] Sign(KeyPair kp, byte[] data)
{
byte[] signature;
using(var rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(Decompress(Base32.FromBase32String(kp.Private)));
//使用指定的哈希算法计算指定字节数组的哈希值,并对生成的哈希值进行签名。
signature = rsa.SignData(data, new SHA1CryptoServiceProvider());
}
return signature;
}
publicstaticboolVerifyData(KeyPair kp, byte[] data, byte[] signature)
{
bool b;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(Decompress(Base32.FromBase32String(kp.Public)));
//通过使用提供的公钥确定签名中的哈希值并将其与所提供数据的哈希值进行比较验证数字签名是否有效。
b = rsa.VerifyData(data, new SHA1CryptoServiceProvider(), signature);
}
return b;
}
publicstatic KeyPair CreateNew(int length)
{
KeyPair ret;
//密钥的大小using (var rsa = new RSACryptoServiceProvider(length))
{
try
{
//参数true 生成私钥,false生成公钥var pub = rsa.ExportCspBlob(false);
var priv = rsa.ExportCspBlob(true);
ret = new KeyPair(Base32.ToBase32String(Compress(pub)), Base32.ToBase32String(Compress(priv)));
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
return ret;
}
publicbyte[] ToArray()
{
return Decompress(Base32.FromBase32String(Private));
}
publicstatic KeyPair Import(byte[] raw, int length)
{
KeyPair ret;
using (var rsa = new RSACryptoServiceProvider(length))
{
rsa.ImportCspBlob(raw);
try
{
var pub = rsa.ExportCspBlob(false);
var priv = rsa.ExportCspBlob(true);
ret = new KeyPair(Base32.ToBase32String(Compress(pub)), Base32.ToBase32String(Compress(priv)));
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
return ret;
}
privatestaticbyte[] Compress(byte[] raw)
{
using (MemoryStream memory = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true))
{
gzip.Write(raw, 0, raw.Length);
}
return memory.ToArray();
}
}
privatestaticbyte[] Decompress(byte[] gzip)
{
using (GZipStream stream = new GZipStream(new MemoryStream(gzip),
CompressionMode.Decompress))
{
constint size = 4096;
byte[] buffer = newbyte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
}