KB-RSA
1.0.3
The error encountered when creating rsa key of type xml in versions before 1.0.4 has been fixed in version 1.0.4. Due to bugs found in older versions, these versions are no longer usable. You can use the package in your project by updating it to the latest version 1.0.4
See the version list below for details.
dotnet add package KB-RSA --version 1.0.3
NuGet\Install-Package KB-RSA -Version 1.0.3
<PackageReference Include="KB-RSA" Version="1.0.3" />
paket add KB-RSA --version 1.0.3
#r "nuget: KB-RSA, 1.0.3"
// Install KB-RSA as a Cake Addin #addin nuget:?package=KB-RSA&version=1.0.3 // Install KB-RSA as a Cake Tool #tool nuget:?package=KB-RSA&version=1.0.3
KB-RSA
A classlibrary project released as nuget that makes it easy to use for key generation, encryption, and decryption with RSA.
All classes in the project are in static structure.
KeySizes
public static class KeySizes {...}
- A list of accepted key sizes for RSA
public enum KeySize
{
_384 = 384,
_392 = 392,
.
.
_2048 = 2048,
.
.
_16384 = 16384
}
RSAKeyModel
public class RSAKeyModel {...}
- Two property to store public and private key
public RSAParameters publicKey { get; set; }
public RSAParameters privateKey { get; set; }
GenerateKey
public static class GenerateKey {...}
key generation processes
- Returns the public and private key it generates according to the key size sent into it as an RSAKeyModel object.
public static RSAKeyModel RSAGenerateKey(KeySize keySize)
{
int ks = (int)keySize;
RSAKeyModel model = new RSAKeyModel();
using (RSACryptoServiceProvider csp = new RSACryptoServiceProvider(ks))
{
model.publicKey = csp.ExportParameters(false);
model.privateKey = csp.ExportParameters(true);
}
return model;
}
- It creates a folder on the desktop with the date and time of the day and saves the public and private key it generates according to the key size sent to it.
public static bool RSAGenerateXMLKey(KeySize keySize)
{
int ks = (int)keySize;
using (RSACryptoServiceProvider csp = new RSACryptoServiceProvider(ks))
{
string filePath = DirectoryControl();
string publicKeyPath = $@"{filePath}\public.key";
string privateKeyPath = $@"{filePath}\private.key";
string publicKey = csp.ToXmlString(false);
TextWriter publicKeyWriter = new StreamWriter(publicKeyPath);
publicKeyWriter.WriteLine(publicKey);
publicKeyWriter.Dispose();
publicKeyWriter.Close();
string privateKey = csp.ToXmlString(true);
TextWriter privateKeyWriter = new StreamWriter(privateKeyPath);
privateKeyWriter.WriteLine(publicKey);
privateKeyWriter.Dispose();
privateKeyWriter.Close();
}
return true;
}
DireDirectoryControl()
method
private static string DirectoryControl()
{
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
filePath += $@"\RSA_XML_Key_{DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss")}";
bool folderCheck = File.Exists(filePath);
if (folderCheck == false)
Directory.CreateDirectory(filePath);
string publicKeyPath = $@"{filePath}\public.key";
bool pubicKeyCheck = File.Exists(publicKeyPath);
if (pubicKeyCheck == false)
{
using (FileStream fs = File.Create(publicKeyPath))
{
fs.Dispose();
fs.Close();
}
}
string privateKeyPath = $@"{filePath}\private.key";
bool privateKeyCheck = File.Exists(privateKeyPath);
if (privateKeyCheck == false)
{
using (FileStream fs = File.Create(privateKeyPath))
{
fs.Dispose();
fs.Close();
}
}
return filePath;
}
Encrypt
public static class Encrypt {...}
- Encrypts the data with the key of type RSAParameters sent
public static string EncryptWithKey(RSAParameters publicKey, string data)
{
byte[] dataBytes = GetBytes(data);
byte[] encryptedData;
using (RSACryptoServiceProvider csp = new RSACryptoServiceProvider())
{
csp.ImportParameters(publicKey);
encryptedData = csp.Encrypt(dataBytes, false);
}
return GetBase64String(encryptedData);
}
- Encrypts data with XML type key sent as string
public static string EncryptWithXMLKey(string publicKey, string data)
{
byte[] dataBytes = GetBytes(data);
byte[] encryptedData;
using (RSACryptoServiceProvider csp = new RSACryptoServiceProvider())
{
csp.FromXmlString(publicKey);
encryptedData = csp.Encrypt(dataBytes, false);
}
return GetBase64String(encryptedData);
}
GetBytes()
method
private static byte[] GetBytes(string data)
{
return byteConverter.GetBytes(data);
}
GetBase64String()
method
private static string GetBase64String(byte[] encryptedData)
{
return Convert.ToBase64String(encryptedData);
}
byteConverter
private static UnicodeEncoding byteConverter = new UnicodeEncoding();
Decrypt
public static class Decrypt {...}
- Decrypts the data with the key of type RSAParameters sent
public static string DecryptWithKey(RSAParameters privateKey, string encryptedData)
{
byte[] dataBytes = GetBytes(encryptedData);
byte[] decryptedData;
using (RSACryptoServiceProvider csp = new RSACryptoServiceProvider())
{
csp.ImportParameters(privateKey);
decryptedData = csp.Decrypt(dataBytes, false);
}
return GetString(decryptedData);
}
- Decrypts data with XML type key sent as string
public static string DecryptWithXMLKey(string privateKey, string encryptedData)
{
byte[] dataBytes = GetBytes(encryptedData);
byte[] decryptedData;
using (RSACryptoServiceProvider csp = new RSACryptoServiceProvider())
{
csp.FromXmlString(privateKey);
decryptedData = csp.Decrypt(dataBytes, false);
}
return GetString(decryptedData);
}
GetString()
method
private static string GetString(byte[] decryptedData)
{
return byteConverter.GetString(decryptedData);
}
GetBytes()
method
private static byte[] GetBytes(string encryptedData)
{
return Convert.FromBase64String(encryptedData);
}
byteConverter
private static UnicodeEncoding byteConverter = new UnicodeEncoding();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Fixed error when generating xml key.