|
| 1 | +# RSA Encryption and Decryption |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This library provides a straightforward implementation of RSA encryption and decryption in C# using the .NET `RSACryptoServiceProvider`. |
| 6 | +It includes methods for generating RSA key pairs, encrypting data with a public key, and decrypting data with a private key. |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Usage](#usage) |
| 11 | + - [Generate RSA Keys](#generate-rsa-keys) |
| 12 | + - [Encrypt and Decrypt using RSA](#encrypt-and-decrypt-using-rsa) |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Generate RSA Keys |
| 17 | + |
| 18 | +```csharp |
| 19 | +using SafeCrypt.Helpers; |
| 20 | +using SafeCrypt.RsaEncryption; |
| 21 | + |
| 22 | +var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048); |
| 23 | + |
| 24 | +string rsaPublicKey = rsaKeyPair.Item1; |
| 25 | +string rsaPrivateKey = rsaKeyPair.Item2; |
| 26 | + |
| 27 | +Console.WriteLine($"Public Key: {rsaPublicKey}"); |
| 28 | +Console.WriteLine($"Private Key: {rsaPrivateKey}"); |
| 29 | +``` |
| 30 | + |
| 31 | +### Encrypt and Decrypt using RSA |
| 32 | + |
| 33 | +```csharp |
| 34 | + using SafeCrypt.RsaEncryption; |
| 35 | + |
| 36 | + // Encrypt |
| 37 | + string originalData = "Hello, RSA Encryption!"; |
| 38 | + |
| 39 | + var encryptionModel = new RsaEncryptionParameters |
| 40 | + { |
| 41 | + DataToEncrypt = originalData, |
| 42 | + PublicKey = rsaPublicKey, |
| 43 | + }; |
| 44 | + |
| 45 | + var encryptedData = await Rsa.EncryptAsync(encryptionModel); |
| 46 | + |
| 47 | + Console.WriteLine($"Original Data: {originalData}"); |
| 48 | + Console.WriteLine("Encrypted Data: " + BitConverter.ToString(encryptedData.EncryptedData)); |
| 49 | + |
| 50 | + // Convert encrypted byte array to Base64 string |
| 51 | + string encryptedDataConvertedString = Convert.ToBase64String(encryptedData.EncryptedData); |
| 52 | + |
| 53 | + // Convert string back to byte array for decryption |
| 54 | + byte[] convertedBytes = Convert.FromBase64String(encryptedDataConvertedString); |
| 55 | + |
| 56 | + bool arraysAreEqual = StructuralComparisons.StructuralEqualityComparer.Equals(encryptedData.EncryptedData, convertedBytes); |
| 57 | + Console.WriteLine("Original and converted byte arrays are equal: " + arraysAreEqual); // should return true |
| 58 | +
|
| 59 | + |
| 60 | + |
| 61 | + // Decrypt |
| 62 | + var decryptionModel = new RsaDecryptionParameters |
| 63 | + { |
| 64 | + DataToDecrypt = convertedBytes, // encryptedData.EncryptedData |
| 65 | + PrivateKey = rsaPrivateKey, |
| 66 | + }; |
| 67 | + |
| 68 | + var decryptedData = await Rsa.DecryptAsync(decryptionModel); |
| 69 | + |
| 70 | + // if Error occurs during encryption |
| 71 | + if (decryptedData.Errors.Count > 0) |
| 72 | + { |
| 73 | + Console.WriteLine("Decryption Errors:"); |
| 74 | + foreach (var error in decryptedData.Errors) |
| 75 | + { |
| 76 | + Console.WriteLine(error); |
| 77 | + } |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + Console.WriteLine($"Decrypted Data: {decryptedData.DecryptedData}"); |
| 82 | + } |
| 83 | + |
| 84 | +// Note: The return type from Rsa.EncryptAsync is `EncryptionResult`, and Rsa.DecryptAsync is `DecryptionResult`. |
| 85 | +// Both models include a list of errors encountered during encryption/decryption. |
| 86 | +
|
| 87 | +``` |
| 88 | +## Contributing |
| 89 | + |
| 90 | +Contributions are welcome! Feel free to open issues, submit pull requests, or provide feedback. |
0 commit comments