Skip to content

Instantly share code, notes, and snippets.

@alan-null
Forked from gashupl/Program.cs
Created October 17, 2022 20:34
Show Gist options
  • Select an option

  • Save alan-null/97645a865e65c9d955f21e000f36bf4b to your computer and use it in GitHub Desktop.

Select an option

Save alan-null/97645a865e65c9d955f21e000f36bf4b to your computer and use it in GitHub Desktop.

Revisions

  1. @gashupl gashupl created this gist Sep 30, 2016.
    111 changes: 111 additions & 0 deletions Program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    using System;
    using System.Security.Cryptography;
    using System.Text;

    namespace RsaEncryptionSample
    {
    class Program
    {
    static void Main(string[] args)
    {

    var cryptoServiceProvider = new RSACryptoServiceProvider(2048); //2048 - Długość klucza
    var privateKey = cryptoServiceProvider.ExportParameters(true); //Generowanie klucza prywatnego
    var publicKey = cryptoServiceProvider.ExportParameters(false); //Generowanie klucza publiczny

    string publicKeyString = GetKeyString(publicKey);
    string privateKeyString = GetKeyString(privateKey);

    Console.WriteLine("KLUCZ PUBLICZNY: ");
    Console.WriteLine(publicKeyString);
    Console.WriteLine("-------------------------------------------");

    Console.WriteLine("KLUCZ PRYWATNY: ");
    Console.WriteLine(privateKeyString);
    Console.WriteLine("-------------------------------------------");

    string textToEncrypt = GenerateTestString();
    Console.WriteLine("TEKST DO ZASZYFROWANIA: ");
    Console.WriteLine(textToEncrypt);
    Console.WriteLine("-------------------------------------------");

    string encryptedText = Encrypt(textToEncrypt, publicKeyString); //Szyfrowanie za pomocą klucza publicznego
    Console.WriteLine("ZASZYFROWANY TEXT: ");
    Console.WriteLine(encryptedText);
    Console.WriteLine("-------------------------------------------");

    string decryptedText = Decrypt(encryptedText, privateKeyString); //Odszyfrowywanie za pomocą klucza prywatnego

    Console.WriteLine("ODSZYFROWANY TEXT: ");
    Console.WriteLine(decryptedText);

    }

    public static string GetKeyString(RSAParameters publicKey)
    {

    var stringWriter = new System.IO.StringWriter();
    var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
    xmlSerializer.Serialize(stringWriter, publicKey);
    return stringWriter.ToString();
    }

    public static string Encrypt(string textToEncrypt, string publicKeyString)
    {
    var bytesToEncrypt = Encoding.UTF8.GetBytes(textToEncrypt);

    using (var rsa = new RSACryptoServiceProvider(2048))
    {
    try
    {
    rsa.FromXmlString(publicKeyString.ToString());
    var encryptedData = rsa.Encrypt(bytesToEncrypt, true);
    var base64Encrypted = Convert.ToBase64String(encryptedData);
    return base64Encrypted;
    }
    finally
    {
    rsa.PersistKeyInCsp = false;
    }
    }
    }

    public static string Decrypt(string textToDecrypt, string privateKeyString)
    {
    var bytesToDescrypt = Encoding.UTF8.GetBytes(textToDecrypt);

    using (var rsa = new RSACryptoServiceProvider(2048))
    {
    try
    {

    // server decrypting data with private key
    rsa.FromXmlString(privateKeyString);

    var resultBytes = Convert.FromBase64String(textToDecrypt);
    var decryptedBytes = rsa.Decrypt(resultBytes, true);
    var decryptedData = Encoding.UTF8.GetString(decryptedBytes);
    return decryptedData.ToString();
    }
    finally
    {
    rsa.PersistKeyInCsp = false;
    }
    }
    }

    private static string GenerateTestString()
    {
    Guid opportinityId = Guid.NewGuid();
    Guid systemUserId = Guid.NewGuid();
    string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("opportunityid={0}", opportinityId.ToString());
    sb.AppendFormat("&systemuserid={0}", systemUserId.ToString());
    sb.AppendFormat("&currenttime={0}", currentTime);

    return sb.ToString();
    }
    }
    }