//透過內建的產生公鑰和私鑰
var rsa = System.Security.Cryptography.RSA.Create();
var publicXMLKey = rsa.ToXmlString(false);
var privateXMLKey = rsa.ToXmlString(true);
var pubKeyPem = XC.RSAUtil.RsaKeyConvert.PublicKeyXmlToPem(publicXMLKey);
var privateKeyPkcs1 = XC.RSAUtil.RsaKeyConvert.PrivateKeyXmlToPkcs1(privateXMLKey);
Console.WriteLine("Private PKCS1");
Console.WriteLine(privateKeyPkcs1);
Console.WriteLine("");
var privateKeyPkcs8 = XC.RSAUtil.RsaKeyConvert.PrivateKeyXmlToPkcs8(privateXMLKey);
Console.WriteLine("Private PKCS8");
Console.WriteLine(privateKeyPkcs8);
Console.WriteLine("");
var str = "測試加密使用的內文當麻許功蓋";
//加密
//PEM
var encData = "";
using (var rsaUtil = new XC.RSAUtil.RsaPkcs1Util(Encoding.UTF8, pubKeyPem))
{
encData = rsaUtil.Encrypt(str, RSAEncryptionPadding.Pkcs1);
Console.WriteLine("加密後文字");
Console.WriteLine(encData);
Console.WriteLine("");
}
//解密 Pkcs1
using (var rsaUtil = new XC.RSAUtil.RsaPkcs1Util(Encoding.UTF8, pubKeyPem, privateKeyPkcs1))
{
var deCrypto = rsaUtil.Decrypt(encData, RSAEncryptionPadding.Pkcs1);
Console.WriteLine("解密後 PKCS1");
Console.WriteLine(deCrypto);
Console.WriteLine("");
}
//解密 Pkcs8
using (var rsaUtil = new XC.RSAUtil.RsaPkcs8Util(Encoding.UTF8, pubKeyPem, privateKeyPkcs8))
{
var deCrypto = rsaUtil.Decrypt(encData, RSAEncryptionPadding.Pkcs1);
Console.WriteLine("解密後 PKCS8");
Console.WriteLine(deCrypto);
Console.WriteLine("");
}
筆記一下,之後也會有相關的測試。
--
Before you read this, the article was in a superposition of human and AI. Upon observation, the wave function collapsed into a sleep-deprived developer.