EN | 中文
This package is not the official version of GitHub Repository stulzq/RSAUtil but a custom fork. Please go to NuGet XC.RSAUtil for the official one.
A .NET Core RSA tool that provides the ability of data encryption, decryption, signing and verifying signature. It supports using and converting RSA keys of formats, namely: .xml
, .pem
, .der
.
Install-Package EggEgg.XC.RSAUtil
- The
RSAUtilBase.LoadRSAKey(byte[])
method can quickly load any supported format of private and public keys, automatically detecting the format, key size, and other parameters, so you don't need to worry about the underlying details of RSA operations. - Added comprehensive support for PKCS#1 RSA public keys.
• Supports the
.der
binary key format. - Together with the
RsaKeyFeature
class and theRSAUtilBase.TreatRSAKeyType(byte[])
method, compared to the original implementation's hard-coded approach, you can more flexibly handle RSA key operations. - Added unit tests to ensure the correctness of the package's behavior.
Use the "RsaKeyGenerator" class. The result returned is a
BinaryKeyResult
containingKeySize
,Format
,Padding
,PrivateKey
andPublicKey
.
Like this:
var keys = RsaKeyGenerator.GetKey(RsaKeyFormat.Pem, RsaKeyPadding.Pkcs1, 2048);
// or use RsaKeyFeature class
// var keys = RsaKeyGenerator.GetKey(new RsaKeyFeature { Format = RsaKeyFormat.Der, Padding = RsaKeyPadding.Pkcs1 }, 2048);
byte[] privateKey = keys.PrivateKey;
byte[] publicKey = key.PublicKey;
// You can directly use it also:
var rsa = keys.GetRSAInstance();
// Or save key and load next time:
rsa = RSAUtilBase.LoadRSAKey(privateKey);
You can freely convert key formats using RsaKeyConvert.Format()
. For example:
RsaKeyConvert.Format(key,
new RsaKeyFeature { IsPrivate = true; Format = RsaKeyFormat.Xml; Padding = RsaKeyPadding.Xml },
new RsaKeyFeature { IsPrivate = true; Format = RsaKeyFormat.Der; Padding = RsaKeyPadding.Pkcs1 });
RsaKeyConvert.Format(key,
new RsaKeyFeature { IsPrivate = false; Format = RsaKeyFormat.Xml; Padding = RsaKeyPadding.Xml },
new RsaKeyFeature { IsPrivate = false; Format = RsaKeyFormat.Pem; Padding = RsaKeyPadding.Pkcs1 });
RsaKeyConvert.Format(key,
new RsaKeyFeature { IsPrivate = true; Format = RsaKeyFormat.Pem; Padding = RsaKeyPadding.Pkcs1 },
new RsaKeyFeatire { IsPrivate = false; Format = RsaKeyFormat.Pem; Padding = RsaKeyPadding.Pkcs8 });
Other hardcoded conversion methods can also be used under the RsaKeyConvert
class.
Load any key via RSAUtilBase.LoadRSAKey(byte[])
, then:
- Encryption:
RSAUtilBase.RsaEncrypt()
- Decryption:
RSAUtilBase.RsaDecrypt()
- Sign:
RSAUtilBase.SignData()
- Verification:
RSAUtilBase.VerifyData()
bc-csharp - onovotny