Kashub's Code Barn - "tooo"

podświetlone jako plsql (dodał(a) rzepson99 @ 2022-11-14 16:45:01)

Twoja wyszukiwarka
Podświetl ten kod w:
Ostatnio dodane:
Losowe wpisy:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
 
namespace WindowsFormsApp1
{
    PUBLIC partial class Form1 : Form
    {
        PUBLIC string publicKeyString;
        PUBLIC string privateKeyString;
        PUBLIC string encryptedText;
        PUBLIC string decryptedText;
        PUBLIC Form1()
        {
            InitializeComponent();
 
        }
 
            PRIVATE void btnPublic_key_Click(object sender, EventArgs e)
        {
            var cryptoServiceProvider = NEW RSACryptoServiceProvider(2048); //2048 - Długość klucza
            var publicKey = cryptoServiceProvider.ExportParameters(FALSE); //Generowanie klucza publiczny
            publicKeyString = GetKeyString(publicKey);
            lblPublic_Key.Text = publicKeyString;
 
        }
 
 
        PRIVATE void btnPrivate_key_Click(object sender, EventArgs e)
        {
            var cryptoServiceProvider = NEW RSACryptoServiceProvider(2048); //2048 - Długość klucza
            var privateKey = cryptoServiceProvider.ExportParameters(TRUE); //Generowanie klucza prywatnego
            string privateKeyString = GetKeyString(privateKey);
            lblPrivate_Key.Text = privateKeyString;
        }
 
        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();
        }
 
        PRIVATE void btnEncryptiontext_Click(object sender, EventArgs e)
        {
            string tekst = textBox_text.Text;
            string encryptedText = Encrypt(tekst, publicKeyString); //Szyfrowanie za pomocą klucza publicznego
            lbl_Encryptiontxt.Text = encryptedText;
 
            IF (publicKeyString == NULL)
            {
                MessageBox.Show("Najpierw wygeneruj kklucz", "Error");
 
            }
        }
 
        PUBLIC static string Encrypt(string tekst, string publicKeyString)
        {
 
 
 
            var bytesToEncrypt = Encoding.UTF8.GetBytes(tekst);
 
            using (var rsa = NEW RSACryptoServiceProvider(2048))
            {
                try
                {
                    IF (publicKeyString == NULL)
                    {
                        MessageBox.Show("Najpierw wygeneruj kklucz", "Error");
 
                    }
                    ELSE
                    {
                        rsa.FromXmlString(publicKeyString.ToString());
                    }
                        var encryptedData = rsa.Encrypt(bytesToEncrypt, TRUE);
                        var base64Encrypted = CONVERT.ToBase64String(encryptedData);
                        RETURN base64Encrypted;
 
                }
                finally
                {
                    rsa.PersistKeyInCsp = FALSE;
                }
            }
        }
 
        PRIVATE void btn_Decrypttxt_Click(object sender, EventArgs e)
        {
            string decryptedText = Decrypt(encryptedText, privateKeyString);
            lbl_Decryptiontxt.Text = decryptedText;
        }
 
        PUBLIC static string Decrypt(string encryptedText, string privateKeyString)
        {
            var bytesToDescrypt = Encoding.UTF8.GetBytes(encryptedText);
 
            using (var rsa = NEW RSACryptoServiceProvider(2048))
            {
                try
                {
 
                    // server decrypting data WITH PRIVATE key                    
                    rsa.FromXmlString(privateKeyString);
 
                    var resultBytes = CONVERT.FromBase64String(encryptedText);
                    var decryptedBytes = rsa.Decrypt(resultBytes, TRUE);
                    var decryptedData = Encoding.UTF8.GetString(decryptedBytes);
                    RETURN decryptedData.ToString();
                }
                finally
                {
                    rsa.PersistKeyInCsp = FALSE;
                }
            }
        }
    }
}
 
| Opony motocyklowe | | Opony zimowe | | Sklep z artykułami RTV/AGD | | Programista PHP | | Skracacz linków | | Blog o książkach | | Darmowe Blogi | | Kody programów | | Smutne Opisy | | Pionowe opisy |