Skip to content
Snippets Groups Projects
Commit 75d9dc2e authored by Ruben wihler's avatar Ruben wihler
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
// Wihler Ruben
// 05.12.2023
using System.Security.Cryptography;
//Resources:
//Doc :
// Symmetric encryption (AES): https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=net-5.0
// Asymmetric encryption (RSA): https://learn.microsoft.com/en-us/dotnet/standard/security/encrypting-data#asymmetric-encryption
//Other:
//AES | initialisation vector (IV) : https://www.techtarget.com/whatis/definition/initialization-vector-IV
public static class EncryptionManager
{
/// <summary>
/// Generate a private/public RSA key and return it in XML string
/// </summary>
/// <returns></returns>
public static (string publicKey, string privateKey) GenerateRSAKeys()
{
var rsa = new RSACryptoServiceProvider()
{
KeySize = 1024
};
var private_key = rsa.ToXmlString(true);
var public_key = rsa.ToXmlString(false);
return (public_key, private_key);
}
/// <summary>
/// Encrypt a text with a combinaison of RSA and AES.
/// We will generate AES key and IV for encrypt the text.
/// The AES key and IV will be encrypted with the entered RSA public key.
/// </summary>
/// <param name="text">The text to encrypt</param>
/// <param name="publicKey">the target RSA public key in XML format</param>
/// <returns></returns>
public static (string text, string aesKey, string aesIV) Encrypt(string text, string publicKey)
{
(var key, var iv) = GenerateAESKey();
var keyb64 = Convert.ToBase64String(key);
var ivb64 = Convert.ToBase64String(iv);
var encrypted_msg = AesEncrypt(text, key, iv);
var encrypted_key = RsaEncrypt(keyb64, publicKey);
var encrypted_iv = RsaEncrypt(ivb64, publicKey);
return (Convert.ToBase64String(encrypted_msg),
Convert.ToBase64String(encrypted_key),
Convert.ToBase64String(encrypted_iv));
}
/// <summary>
/// Decrypt the target encrypted text.
/// It will decrypt the AES key and IV with the private RSA key.
/// With the founded AES key and IV we will decrypt the text.
/// </summary>
/// <param name="encryptedText"></param>
/// <param name="aesKey"></param>
/// <param name="aesIV"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string Decrypt(string encryptedText, string aesKey, string aesIV, string privateKey)
{
var textbyte = Convert.FromBase64String(encryptedText);
var decrypted_key = RsaDecrypt(aesKey, privateKey);
var decrypted_IV = RsaDecrypt(aesIV, privateKey);
var decrypted_text = AesDecrypt(textbyte, decrypted_key, decrypted_IV);
return decrypted_text;
}
private static byte[] RsaEncrypt(string data, string publicKey)
{
var data_b64 = Convert.FromBase64String(data);
using var rsa = new RSACryptoServiceProvider(1024);
try
{
rsa.FromXmlString(publicKey.ToString());
return rsa.Encrypt(data_b64, true);
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
private static byte[] RsaDecrypt(string data, string privateKey)
{
var data_b64 = Convert.FromBase64String(data);
using var rsa = new RSACryptoServiceProvider(1024);
try
{
rsa.FromXmlString(privateKey);
return rsa.Decrypt(data_b64, true);
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
private static byte[] AesEncrypt(string data, byte[] key, byte[] IV)
{
using var aes = Aes.Create();
aes.Key = key;
aes.IV = IV;
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
byte[] encrypted_data;
using (MemoryStream ms = new MemoryStream())
{
using CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(data);
}
encrypted_data = ms.ToArray();
}
return encrypted_data;
}
private static string AesDecrypt(byte[] cipherText, byte[] key, byte[] IV)
{
string data = string.Empty;
using (var aes = Aes.Create())
{
aes.Key = key;
aes.IV = IV;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
// Create the streams used for decryption.
using var ms = new MemoryStream(cipherText);
using var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
using var sr = new StreamReader(cs);
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
data = sr.ReadToEnd();
}
return data;
}
private static (byte[] key, byte[] IV) GenerateAESKey()
{
using var aes = Aes.Create();
aes.KeySize = 256;
aes.GenerateKey();
aes.GenerateIV();
return (aes.Key, aes.IV);
}
}
\ No newline at end of file
readme.md 0 → 100644
# EncryptionManager Class Documentation
## Overview
The `EncryptionManager` 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.
## Table of Contents
1. [RSA Key Generation](#rsa-key-generation)
2. [Text Encryption](#text-encryption)
3. [Text Decryption](#text-decryption)
4. [Private Methods](#private-methods)
## RSA Key Generation <a name="rsa-key-generation"></a>
```csharp
public static (string publicKey, string privateKey) GenerateRSAKeys()
```
### `GenerateRSAKeys` Method
#### Description
Generates a pair of private and public RSA keys and returns them as XML strings.
#### Signature
```csharp
public static (string publicKey, string privateKey) GenerateRSAKeys()
```
#### Parameters
None
#### Returns
- `publicKey`: The public RSA key in XML format.
- `privateKey`: The private RSA key in XML format.
#### Example
```csharp
var keys = EncryptionManager.GenerateRSAKeys();
var publicKey = keys.publicKey;
var privateKey = keys.privateKey;
```
## Text Encryption <a name="text-encryption"></a>
```csharp
public static (string text, string aesKey, string aesIV) Encrypt(string text, string publicKey)
```
### `Encrypt` Method
#### Description
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.
#### Signature
```csharp
public static (string text, string aesKey, string aesIV) Encrypt(string text, string publicKey)
```
#### Parameters
- `text`: The text to be encrypted.
- `publicKey`: The target RSA public key in XML format.
#### Returns
- `text`: The encrypted text in Base64 format.
- `aesKey`: The encrypted AES key in Base64 format.
- `aesIV`: The encrypted AES initialization vector (IV) in Base```
#### Example
```csharp
var encryptionResult = EncryptionManager.Encrypt("Hello, World!", publicKey);
var encryptedText = encryptionResult.text;
var encryptedAESKey = encryptionResult.aesKey;
var encryptedAESIV = encryptionResult.aesIV;
```
## Text Decryption <a name="text-decryption"></a>
```csharp
public static string Decrypt(string encryptedText, string aesKey, string aesIV, string privateKey)
```
### `Decrypt` Method
#### Description
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.
#### Signature
```csharp
public static string Decrypt(string encryptedText, string aesKey, string aesIV, string privateKey)
```
#### Parameters
- `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.
#### Returns
- The decrypted text.
#### Example
```csharp
var decryptedText = EncryptionManager.Decrypt(encryptedText, encryptedAESKey, encryptedAESIV, privateKey);
```
## Private Methods <a name="private-methods"></a>
```csharp
private static byte[] RsaEncrypt(string data, string publicKey)
```
### `RsaEncrypt` Method
#### Description
Encrypts data using RSA encryption with the provided RSA public key.
#### Signature
```csharp
private static byte[] RsaEncrypt(string data, string publicKey)
```
#### Parameters
- `data`: The data to be encrypted in Base64 format.
- `publicKey`: The RSA public key in XML format.
#### Returns
- The encrypted data.
```csharp
private static byte[] RsaDecrypt(string data, string privateKey)
```
### `RsaDecrypt` Method
#### Description
Decrypts data using RSA decryption with the provided RSA private key.
#### Signature
```csharp
private static byte[] RsaDecrypt(string data, string privateKey)
```
#### Parameters
- `data`: The data to be decrypted in Base64 format.
- `privateKey`: The RSA private key in XML format.
#### Returns
- The decrypted data.
```csharp
private static byte[] AesEncrypt(string data, byte[] key, byte[] IV)
```
### `AesEncrypt` Method
#### Description
Encrypts data using AES encryption with the provided AES key and initialization vector (IV).
#### Signature
```csharp
private static byte[] AesEncrypt(string data, byte[] key, byte[] IV)
```
#### Parameters
- `data`: The data to be encrypted.
- `key`: The AES key.
- `IV`: The AES initialization vector (IV).
#### Returns
- The encrypted data.
```csharp
private static string AesDecrypt(byte[] cipherText, byte[] key, byte[] IV)
```
### `AesDecrypt` Method
#### Description
Decrypts data using AES decryption with the provided AES key and initialization vector (IV).
#### Signature
```csharp
private static string AesDecrypt(byte[] cipherText, byte[] key, byte[] IV)
```
#### Parameters
- `cipherText`: The encrypted data.
- `key`: The AES key.
- `IV`: The AES initialization vector (IV).
#### Returns
- The decrypted data.
```csharp
private static (byte[] key, byte[] IV) GenerateAESKey()
```
### `GenerateAESKey` Method
#### Description
Generates a pair of AES key and initialization vector (IV).
#### Signature
```csharp
private static (byte[] key, byte[] IV) GenerateAESKey()
```
#### Parameters
None
#### Returns
- `key`: The generated AES key.
- `IV`: The generated AES initialization vector (IV).
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment