Kashub's Code Barn - "tooo1"

podświetlone jako csharp (dodał(a) poteznystarosta @ 2022-11-14 17:04:03)

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;
            encryptedText = Encrypt(tekst, publicKeyString); //Szyfrowanie za pomocą klucza publicznego
            tb_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 encryptedText = tb_Encryptiontxt.Text;
            string decryptedText = Decrypt(encryptedText, privateKeyString);
            tb_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;
                }
            }
        }
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
 
        }
    }
}
 
| Opony zimowe | | Sklep z artykułami dla zwierząt | | Blog o książkach | | Blogi za darmo | | Przenieś bloga z onetu | | Skracacz linków | | Opisy na Facebooka | | Pionowe opisy |