Kashub's Code Barn - "<3"

podświetlone jako text (dodał(a) fifis @ 2022-11-15 10:24:15)

Twoja wyszukiwarka
T-Mobile
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 klucz_prywatny;
        public string encryptedText;
        public string szyfr;
        public string klucz_publiczny;
        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);
            tbPublic_Key.Text = publicKeyString;
            klucz_publiczny = 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
            privateKeyString = GetKeyString(privateKey);
            lblPrivate_Key.Text = privateKeyString;
            klucz_prywatny = 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;
            encryptedText = Encrypt(tekst, publicKeyString); //Szyfrowanie za pomocą klucza publicznego
            lbl_Encryptiontxt.Text = encryptedText;
            szyfr = 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(szyfr, klucz_prywatny);
            lbl_Decryptiontxt.Text = decryptedText;
        }
 
        public static string Decrypt(string szyfr, string klucz_prywatny)
        {
            var bytesToDescrypt = Encoding.UTF8.GetBytes(szyfr);
 
            using (var rsa = new RSACryptoServiceProvider(2048))
            {
                try
                {
 
                    // server decrypting data with private key                    
                    rsa.FromXmlString(klucz_prywatny);
 
                    var resultBytes = Convert.FromBase64String(szyfr);
                    var decryptedBytes = rsa.Decrypt(resultBytes, true);
                    var decryptedData = Encoding.UTF8.GetString(decryptedBytes);
                    return decryptedData.ToString();
                }
                finally
                {
                    rsa.PersistKeyInCsp = false;
                }
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
    }
}
 
| Sklep z oponami | | Opony zimowe | | karma dla psa - sklep | | Skróć link | | Załóż za darmo bloga | | Wklejacz kodów | | Skracacz linków | | Opisy na Facebooka | | Pionowe opisy |