The HybridEncryption
class provides a set of methods for performing combined RSA and AES encryption and decryption in C#. This class enables the generation of RSA key pairs, encryption of text using a combination of RSA and AES, and subsequent decryption of the encrypted text.
public static (string publicKey, string privateKey) GenerateRSAKeys()
Generates a pair of private and public RSA keys and returns them as XML strings.
public static (string publicKey, string privateKey) GenerateRSAKeys()
None
publicKey
: The public RSA key in XML format.privateKey
: The private RSA key in XML format.
var keys = HybridEncryption.GenerateRSAKeys();
var publicKey = keys.publicKey;
var privateKey = keys.privateKey;
public static (string text, string aesKey, string aesIV) Encrypt(string text, string publicKey)
Encrypts a given text using a combination of RSA and AES. It generates AES key and IV, encrypts them with the provided RSA public key, and then encrypts the text using AES.
public static (string text, string aesKey, string aesIV) Encrypt(string text, string publicKey)
text
: The text to be encrypted.publicKey
: The target RSA public key in XML format.
text
: The encrypted text in Base64 format.aesKey
: The encrypted AES key in Base64 format.aesIV
: The encrypted AES initialization vector (IV) in Base```
var encryptionResult = HybridEncryption.Encrypt("Hello, World!", publicKey);
var encryptedText = encryptionResult.text;
var encryptedAESKey = encryptionResult.aesKey;
var encryptedAESIV = encryptionResult.aesIV;
public static string Decrypt(string encryptedText, string aesKey, string aesIV, string privateKey)
Decrypts the provided encrypted text. It decrypts the AES key and IV with the private RSA key and then uses them to decrypt the text.
public static string Decrypt(string encryptedText, string aesKey, string aesIV, string privateKey)
encryptedText
: The encrypted text in Base64 format.aesKey
: The encrypted AES key in Base64 format.aesIV
: The encrypted AES initialization vector (IV) in Base64 format.privateKey
: The private RSA key in XML format.
- The decrypted text.
var decryptedText = HybridEncryption.Decrypt(encryptedText, encryptedAESKey, encryptedAESIV, privateKey);
private static byte[] RsaEncrypt(string data, string publicKey)
Encrypts data using RSA encryption with the provided RSA public key.
private static byte[] RsaEncrypt(string data, string publicKey)
data
: The data to be encrypted in Base64 format.publicKey
: The RSA public key in XML format.
- The encrypted data.
private static byte[] RsaDecrypt(string data, string privateKey)
Decrypts data using RSA decryption with the provided RSA private key.
private static byte[] RsaDecrypt(string data, string privateKey)
data
: The data to be decrypted in Base64 format.privateKey
: The RSA private key in XML format.
- The decrypted data.
private static byte[] AesEncrypt(string data, byte[] key, byte[] IV)
Encrypts data using AES encryption with the provided AES key and initialization vector (IV).
private static byte[] AesEncrypt(string data, byte[] key, byte[] IV)
data
: The data to be encrypted.key
: The AES key.IV
: The AES initialization vector (IV).
- The encrypted data.
private static string AesDecrypt(byte[] cipherText, byte[] key, byte[] IV)
Decrypts data using AES decryption with the provided AES key and initialization vector (IV).
private static string AesDecrypt(byte[] cipherText, byte[] key, byte[] IV)
cipherText
: The encrypted data.key
: The AES key.IV
: The AES initialization vector (IV).
- The decrypted data.
private static (byte[] key, byte[] IV) GenerateAESKey()
Generates a pair of AES key and initialization vector (IV).
private static (byte[] key, byte[] IV) GenerateAESKey()
None
key
: The generated AES key.IV
: The generated AES initialization vector (IV).