|
| 1 | +# SafeCrypt AES Encryption and Decryption |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The SafeCrypt library provides a simple and secure implementation of the Advanced Encryption Standard (AES) algorithm for |
| 6 | +encryption and decryption in C#. |
| 7 | +This document guides you through the basic steps to use AES encryption and decryption methods. |
| 8 | + |
| 9 | +## Table of Contents |
| 10 | + |
| 11 | +- [Usage](#usage) |
| 12 | + - [EncryptToHexStringAsync method](#encryptToHexStringAsync-method) |
| 13 | + - [Encrypting Data](#encrypting-data) |
| 14 | + - [Decrypting Data](#decrypting-data) |
| 15 | + - [EncryptToBase64StringAsync method](#encryptToBase64StringAsync-method) |
| 16 | + - [Example](#example) |
| 17 | + - [EncryptionData Object](#encryptiondata-object) |
| 18 | + - [Cipher Modes](#cipher-modes) |
| 19 | +- [Contributing](#contributing) |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### EncryptToHexStringAsync method |
| 24 | + |
| 25 | +#### Encrypting Data |
| 26 | + |
| 27 | +To encrypt data using the AES algorithm in this library, you first need to decide which method you want to use. |
| 28 | +The `EncryptToHexStringAsync` method returns a hexadecimal string, while `EncryptToBase64StringAsync` returns a Base64 string. |
| 29 | +Follow the steps below to use `EncryptToHexStringAsync`: |
| 30 | + |
| 31 | +1. **Generate an IV Key:** Use the `KeyGenerators.GenerateHexadecimalIVKey()` method to create an Initialization Vector (IV) key. The IV is crucial for secure encryption. |
| 32 | + |
| 33 | +2. **Generate a Secret Key:** Call `KeyGenerators.GenerateAesSecretKey(128)` to generate a secret key with the desired bit size. Supported bit sizes are 128, 192, or 256. Any other value will result in an exception. |
| 34 | + |
| 35 | +3. **Create an EncryptionParameters Model:** Construct an `EncryptionParameters` model, providing the data to be encrypted, IV, and secret key. |
| 36 | + |
| 37 | +4. **Call EncryptToHexStringAsync:** Invoke the `Aes.EncryptToHexStringAsync` method with the `EncryptionParameters` model to encrypt the data. Check for errors in the `Errors` property of the returned `EncryptionData` object. |
| 38 | + |
| 39 | +#### Decrypting Data |
| 40 | + |
| 41 | +To decrypt data encrypted with AES, follow these steps: |
| 42 | + |
| 43 | +1. **Create a DecryptionParameters Model:** Build a `DecryptionParameters` model, providing the encrypted data, IV, and secret key used during encryption. |
| 44 | + |
| 45 | +2. **Call DecryptFromHexStringAsync:** Use the `Aes.DecryptFromHexStringAsync` method with the `DecryptionParameters` model to decrypt the data. Ensure that the provided IV and secret key match those used during encryption. |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +### EncryptToBase64StringAsync method |
| 50 | +To use the EncryptToBase64StringAsync method for encryption, follow these steps: |
| 51 | + |
| 52 | +Generate an Initialization Vector (IV) using the KeyGenerators.GenerateBase64IVKey() method. |
| 53 | +Generate a secret key by calling the KeyGenerators.GenerateAesSecretKey(256) method. The parameter for this method accepts the following values: 128, 192, 256. Any value aside from these throws an exception with the message "Invalid key size. Supported sizes are 128, 192, or 256 bits." |
| 54 | +To encrypt the data, provide the EncryptionParameters model to the EncryptToBase64StringAsync method, along with an optional cipher mode. The cipher mode defaults to CBC if not specified. |
| 55 | + |
| 56 | + |
| 57 | +## Example |
| 58 | + |
| 59 | +```csharp |
| 60 | +using SafeCrypt.AES; |
| 61 | +using SafeCrypt.Helpers; |
| 62 | +using SafeCrypt.Models; |
| 63 | + |
| 64 | +// Using the EncryptToHexStringAsync and DecryptFromHexStringAsync methods |
| 65 | +
|
| 66 | +var aesIv = KeyGenerators.GenerateHexadecimalIVKey(); |
| 67 | +var secret = KeyGenerators.GenerateAesSecretKey(256); |
| 68 | + |
| 69 | +var dataToEncrypt = "Hello World"; |
| 70 | + |
| 71 | +var data = new EncryptionParameters |
| 72 | +{ |
| 73 | + Data = dataToEncrypt, |
| 74 | + IV = aesIv, |
| 75 | + SecretKey = secret |
| 76 | +}; |
| 77 | + |
| 78 | +Console.WriteLine($"Hex Encryption Started"); |
| 79 | + |
| 80 | +Console.WriteLine(); |
| 81 | +Console.WriteLine(); |
| 82 | + |
| 83 | +var encryptionResult = await Aes.EncryptToHexStringAsync(data); |
| 84 | + |
| 85 | +if (encryptionResult.Errors.Count > 0) |
| 86 | +{ |
| 87 | + // List errors here |
| 88 | +} |
| 89 | + |
| 90 | +Console.WriteLine($"Hex Encrypted data: {encryptionResult.EncryptedData}"); |
| 91 | +Console.WriteLine($"IV key: {encryptionResult.Iv}"); |
| 92 | +Console.WriteLine($"Secret key: {encryptionResult.SecretKey}"); |
| 93 | + |
| 94 | +Console.WriteLine(); |
| 95 | +Console.WriteLine(); |
| 96 | + |
| 97 | +Console.WriteLine($"Hex Decryption Started"); |
| 98 | + |
| 99 | +// Perform decryption using the same IV and secret |
| 100 | +var decryptionResult = await Aes.DecryptFromHexStringAsync(new DecryptionParameters |
| 101 | +{ |
| 102 | + Data = encryptionResult.EncryptedData, |
| 103 | + IV = aesIv, |
| 104 | + SecretKey = secret |
| 105 | +}); |
| 106 | + |
| 107 | +Console.WriteLine($"Hex Decrypted data: {decryptionResult.DecryptedData}"); |
| 108 | +Console.WriteLine($"IV key: {decryptionResult.Iv}"); |
| 109 | +Console.WriteLine($"Secret key: {decryptionResult.SecretKey}"); |
| 110 | + |
| 111 | + |
| 112 | +// Using the EncryptToBase64StringAsync and DecryptFromBase64StringAsync methods |
| 113 | +
|
| 114 | +var base64AesIv = KeyGenerators.GenerateBase64IVKey(); |
| 115 | + |
| 116 | +var base64dataToEncrypt = new EncryptionParameters |
| 117 | +{ |
| 118 | + Data = dataToEncrypt, |
| 119 | + IV = base64AesIv, |
| 120 | + SecretKey = secret |
| 121 | +}; |
| 122 | + |
| 123 | +Console.WriteLine(); |
| 124 | +Console.WriteLine(); |
| 125 | + |
| 126 | + |
| 127 | +Console.WriteLine($"Base64 Encryption Started"); |
| 128 | + |
| 129 | +Console.WriteLine(); |
| 130 | +Console.WriteLine(); |
| 131 | + |
| 132 | +var encryptedResult = await Aes.EncryptToBase64StringAsync(base64dataToEncrypt); |
| 133 | +Console.WriteLine($"Base64 Encrypted data: {encryptedResult.EncryptedData}"); |
| 134 | +Console.WriteLine($"IV key: {encryptedResult.Iv}"); |
| 135 | +Console.WriteLine($"Secret key: {encryptedResult.SecretKey}"); |
| 136 | + |
| 137 | + |
| 138 | +Console.WriteLine(); |
| 139 | +Console.WriteLine(); |
| 140 | + |
| 141 | +Console.WriteLine($"Base64 Decryption Started"); |
| 142 | + |
| 143 | +var decryptionResponse = await Aes.DecryptFromBase64StringAsync(new DecryptionParameters |
| 144 | +{ |
| 145 | + Data = encryptedResult.EncryptedData, |
| 146 | + IV = base64AesIv, |
| 147 | + SecretKey = secret |
| 148 | +}); |
| 149 | + |
| 150 | +Console.WriteLine($"Base64 Decrypted data: {decryptionResponse.DecryptedData}"); |
| 151 | +Console.WriteLine($"IV key: {decryptionResponse.Iv}"); |
| 152 | +Console.WriteLine($"Secret key: {decryptionResponse.SecretKey}"); |
| 153 | +``` |
| 154 | + |
| 155 | +## EncryptionData Object |
| 156 | + |
| 157 | +The Encryption methods returns an `EncryptionData` object with the following properties: |
| 158 | + |
| 159 | +- `EncryptedData`: Holds the encrypted data as a hexadecimal string. |
| 160 | +- `Iv`: The Initialization Vector used for encryption. |
| 161 | +- `SecretKey`: The secret key used for encryption. |
| 162 | +- `HasError`: If an error occurs during encryption, this property is set to true. |
| 163 | +- `Errors`: A list of all errors that occurred during encryption. |
| 164 | + |
| 165 | +## Cipher Modes |
| 166 | + |
| 167 | +By default, the methods uses Cipher Block Chaining (CBC) mode for both encryption and decryption. |
| 168 | +If you change the mode during encryption, provide the same mode during decryption. |
| 169 | + |
| 170 | +## Contributing |
| 171 | + |
| 172 | +Contributions to the SafeCrypt library are welcome! Follow the contribution guidelines and feel free to open issues or submit pull requests. |
0 commit comments