⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 this is about cryptography. takes text as input and decrypt it and then aagin in plane text
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace yaseen
{
    public partial class Form1 : Form
    {


       
       public string cipherText;
        
       public  string plainText;

       public string passPhrase = "Pas5pr@se";        // can be any string
       public string saltValue = "s@1tValue";        // can be any string
       public string hashAlgorithm = "SHA1";             // can be "MD5"
       public int passwordIterations = 2;                  // can be any number
       public string initVector;  
        //"@1B2c3D4e5F6g7H8"; // must be 16 bytes
        public int keySize = 256;                // can be 192 or 128g kg

        public Form1()
        {
            InitializeComponent();
            this.textBox2.Hide();
        }

        private void En_button_Click(object sender, EventArgs e)
        {
            this.textBox2.Show();
            initVector = textBox2.Text;
            this.textBox2.Hide();

            //if (textkey.Text == "") { MessageBox.Show("You must enter a key to encrypt"); return; }
            //initVector = this.textkey.Text;
            
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);


            // Convert our plaintext into a byte array.
            // Let us assume that plaintext contains UTF8-encoded characters.
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plaintext());

            // First, we must create a password, from which the key will be derived.
            // This password will be generated from the specified passphrase and 
            // salt value. The password will be created using the specified hash 
            // algorithm. Password creation can be done in several iterations.
            PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                            passPhrase,
                                                            saltValueBytes,
                                                            hashAlgorithm,
                                                            passwordIterations);

            // Use the password to generate pseudo-random bytes for the encryption
            // key. Specify the size of the key in bytes (instead of bits).
            byte[] keyBytes = password.GetBytes(keySize / 8);

            // Create uninitialized Rijndael encryption object.
            RijndaelManaged symmetricKey = new RijndaelManaged();

            // It is reasonable to set encryption mode to Cipher Block Chaining
            // (CBC). Use default options for other symmetric key parameters.
            symmetricKey.Mode = CipherMode.CBC;

            // Generate encryptor from the existing key bytes and initialization 
            // vector. Key size will be defined based on the number of the key 
            // bytes.
            //ICryptoTransform 
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
                                                             keyBytes,
                                                             initVectorBytes);

            // Define memory stream which will be used to hold encrypted data.
            MemoryStream memoryStream = new MemoryStream();

            // Define cryptographic stream (always use Write mode for encryption).
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                         encryptor,
                                                         CryptoStreamMode.Write);
            // Start encrypting.
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

            // Finish encrypting.
            cryptoStream.FlushFinalBlock();

            // Convert our encrypted data from a memory stream into a byte array.
            byte[] cipherTextBytes = memoryStream.ToArray();

            // Close both streams.
            memoryStream.Close();
            cryptoStream.Close();

            // Convert encrypted data into a base64-encoded string.
            string cipherText = Convert.ToBase64String(cipherTextBytes);

            FileStream filewrite = new FileStream("D:\\yaseen.txt", FileMode.Create, FileAccess.Write);
            StreamWriter wr = new StreamWriter(filewrite);
            wr.Write(cipherText);
            wr.Close();
            filewrite.Close();
            // Return encrypted string.
            //return cipherText;
        }

        private void De_button_Click(object sender, EventArgs e)
        {
            //byte[] cipherTextBytes;

            // Convert strings defining encryption key characteristics into byte
            // arrays. Let us assume that strings only contain ASCII codes.
            // If strings include Unicode characters, use Unicode, UTF7, or UTF8
            // encoding.
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

            // Convert our ciphertext into a byte array.

            //if (cipherText != null)

            //{
            FileStream Crpfile = new FileStream("D:\\yaseen.txt", FileMode.Open, FileAccess.Read);
            StreamReader rd = new StreamReader(Crpfile);
            string cryptedtext=rd.ReadToEnd();
            rd.Close();
            Crpfile.Close();
            byte[] cipherTextBytes = Convert.FromBase64String(cryptedtext);
            //}
            
            // First, we must create a password, from which the key will be 
            // derived. This password will be generated from the specified 
            // passphrase and salt value. The password will be created using
            // the specified hash algorithm. Password creation can be done in
            // several iterations.
            PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                            passPhrase,
                                                            saltValueBytes,
                                                            hashAlgorithm,
                                                            passwordIterations);

            // Use the password to generate pseudo-random bytes for the encryption
            // key. Specify the size of the key in bytes (instead of bits).
            byte[] keyBytes = password.GetBytes(keySize / 8);

            // Create uninitialized Rijndael encryption object.
            RijndaelManaged symmetricKey = new RijndaelManaged();

            // It is reasonable to set encryption mode to Cipher Block Chaining
            // (CBC). Use default options for other symmetric key parameters.
            symmetricKey.Mode = CipherMode.CBC;

            // Generate decryptor from the existing key bytes and initialization 
            // vector. Key size will be defined based on the number of the key 
            // bytes.
            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
                                                             keyBytes,
                                                             initVectorBytes);

            // Define memory stream which will be used to hold encrypted data.
            MemoryStream memoryStream = new MemoryStream(cipherTextBytes);

            // Define cryptographic stream (always use Read mode for encryption).
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                          decryptor,
                                                          CryptoStreamMode.Read);

            // Since at this point we don't know what the size of decrypted data
            // will be, allocate the buffer long enough to hold ciphertext;
            // plaintext is never longer than ciphertext.
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];

            // Start decrypting.
            int decryptedByteCount = cryptoStream.Read(plainTextBytes,
                                                       0,
                                                       plainTextBytes.Length);

            // Close both streams.
            memoryStream.Close();
            cryptoStream.Close();

            // Convert decrypted data into a string. 
            // Let us assume that the original plaintext string was UTF8-encoded.
            string plainText = Encoding.UTF8.GetString(plainTextBytes,
                                                       0,
                                                       decryptedByteCount);


            FileStream filede = new FileStream("D:\\saleem.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter w = new StreamWriter(filede);
            w.Write(plainText);
            w.Close();
            filede.Close();
        }
        public string plaintext()
        {
      
        FileStream file = new FileStream("C:\\yaseen.txt", FileMode.Open, FileAccess.Read);
        StreamReader reader = new StreamReader(file);
        plainText=reader.ReadToEnd();
        reader.Close();
        file.Close();
        return plainText;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileStream filetoread = new FileStream("C:\\yaseen.txt", FileMode.Open, FileAccess.Read);
            StreamReader r = new StreamReader(filetoread);
            textBox1.Text = r.ReadToEnd();
            r.Close();
            filetoread.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FileStream crfile = new FileStream("D:\\yaseen.txt", FileMode.Open, FileAccess.Read);
            StreamReader r = new StreamReader(crfile);
            textBox1.Text = r.ReadToEnd();
            r.Close();
           crfile.Close();
        }

        private void Save_button_Click(object sender, EventArgs e)
        {
            FileStream filetowrite = new FileStream("C:/yaseen.txt",FileMode.OpenOrCreate,FileAccess.Write);
            StreamWriter writer = new StreamWriter(filetowrite);
            writer.Write(textBox1.Text);
            writer.Close();
            filetowrite.Close();
            textBox1.Text = null;
        }

        private void Ok_button_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
        }

       

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -