|
| 1 | +using SafeCrypt.Helpers; |
| 2 | +using SafeCrypt.RsaEncryption; |
| 3 | +using System.Collections; |
| 4 | + |
| 5 | +namespace safecrypt_testapp.Usage; |
| 6 | + |
| 7 | +internal class RsaUsage |
| 8 | +{ |
| 9 | + internal async void Usage() |
| 10 | + { |
| 11 | + // Example: Generate RSA keys |
| 12 | + var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048); |
| 13 | + |
| 14 | + string rsaPublicKey = rsaKeyPair.Item1; |
| 15 | + string rsaPrivateKey = rsaKeyPair.Item2; |
| 16 | + |
| 17 | + Console.WriteLine($"pubic key {rsaPublicKey}"); |
| 18 | + Console.WriteLine($"private key {rsaPrivateKey}"); |
| 19 | + |
| 20 | + // Example: Encrypt and Decrypt using RSA |
| 21 | + string originalData = "Hello, RSA Encryption!"; |
| 22 | + |
| 23 | + var enModel = new RsaEncryptionParameters |
| 24 | + { |
| 25 | + DataToEncrypt = originalData, |
| 26 | + PublicKey = rsaPublicKey, |
| 27 | + }; |
| 28 | + |
| 29 | + var encryptedData = await Rsa.EncryptAsync(enModel); |
| 30 | + |
| 31 | + Console.WriteLine($"Original Data: {originalData}"); |
| 32 | + |
| 33 | + Console.WriteLine("Original byte array: " + BitConverter.ToString(encryptedData.EncryptedData)); |
| 34 | + string EncryptedDataconvertedString = Convert.ToBase64String(encryptedData.EncryptedData); |
| 35 | + |
| 36 | + byte[] convertedBytes = Convert.FromBase64String(EncryptedDataconvertedString); |
| 37 | + |
| 38 | + Console.WriteLine("Converted back to byte array: " + BitConverter.ToString(convertedBytes)); |
| 39 | + bool arraysAreEqual = StructuralComparisons.StructuralEqualityComparer.Equals(encryptedData.EncryptedData, convertedBytes); |
| 40 | + Console.WriteLine("Original and converted byte arrays are equal: " + arraysAreEqual); |
| 41 | + |
| 42 | + // Decrypting |
| 43 | + var decryptionModel = new RsaDecryptionParameters |
| 44 | + { |
| 45 | + DataToDecrypt = convertedBytes, |
| 46 | + PrivateKey = rsaPrivateKey |
| 47 | + }; |
| 48 | + |
| 49 | + var decryptedData = await Rsa.DecryptAsync(decryptionModel); |
| 50 | + Console.WriteLine($"{decryptedData.DecryptedData}"); |
| 51 | + |
| 52 | + Console.ReadLine(); |
| 53 | + } |
| 54 | +} |
0 commit comments