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

📄 form1.cs

📁 C#编写的带控件图象加密解密功能 主要是encoding和decoding二函数
💻 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.IO;
using System.Collections;

namespace DeviceApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       

       

        private void decode_Click(object sender, EventArgs e)
        {
            string address_pic = "\\My Documents\\My Pictures\\c.tif";
            string codes_pic = "dffdfggfgfgfd";
            encoding(codes_pic, address_pic);
        }

        private void encode_Click(object sender, EventArgs e)
        {
            string address_pic = "\\My Documents\\My Pictures\\c.tif";
            string codes_pic = "dffdfggfgfgfd";
            encoding(codes_pic, address_pic);
            

        }

        private void open_a_pic_Click(object sender, EventArgs e)
        {
             string address = "\\My Documents\\My Pictures\\1.jpg";
            //textBox1.Text = picName;
         //   encoding("good", "\\My Documents\\My Pictures\\2.jpg");
            FileStream picFile = new FileStream(address, FileMode.Open);
            pictureBox1.Image = new Bitmap(picFile);
            textBox1.Text = picFile.Length.ToString();
           
            
        }
        private void  encoding(string codes,string address)
        {
            // ref parameters are the codes and address where target picture is restored
            // by calling this function,the old picture will be replaced by the encoded one.

            FileStream picFile = new FileStream(address , FileMode.Open);
            //pictureBox1.Image = new Bitmap(picFile);
            textBox1.Text = picFile.Length.ToString();
            long size_of_pic = picFile.Length;
            // begin encoding:
            FileStream myfile=new FileStream("\\My Documents\\My Pictures\\temp_coded.jjpg",FileMode.CreateNew ) ;
            int size_of_codes = codes.Length;
            StringReader codereader = new StringReader(codes);
            char  [] cod=new char  [codes .Length ];
            byte  [] array  = new byte [codes .Length ];
            byte[]  temp = new byte[codes.Length];
            codereader.Read(cod, 0, codes.Length);
            int temp1 = 0;
            

            int i=0;
            int   l;
            byte j=0;
            
            for (i = 0; i <size_of_pic/codes.Length   ; i++)
            {

              //  picFile.Read(array, i, size_of_codes);

                l= picFile.Read (array ,0,codes .Length);
                for (j = 0; j < l; j++)
                {
                    //Convert.ToChar(array[j]);
                    temp1 = array[j] ^ cod[j];
                    temp[j] = Convert.ToByte(temp1);
                   
                }

                myfile.Write(temp , 0, codes.Length);

                
            }
            int residues;
            residues = (int)(size_of_pic % codes.Length);
          //  textBox2.Text = "";
           // textBox2.Text = residues.ToString();
            char[] cod1 = new char[residues];
            byte[] array1 = new byte[residues];
            byte[] temp2 = new byte[residues];

            if (residues != 0)
            {
                StringReader codereader2 = new StringReader(codes);
                codereader2.Read(cod1, 0, residues);
                l = picFile.Read(array1, 0, residues);
                for (j = 0; j < residues ; j++)
                {
                    //Convert.ToChar(array[j]);
                    temp1 = array1[j] ^ cod1[j];
                    temp2[j] = Convert.ToByte(temp1);

                }

                myfile.Write(temp2, 0, residues);
            }
            
            
            picFile.Close();
            File.Delete(address);

            myfile.Close();
            File.Move("\\My Documents\\My Pictures\\temp_coded.jjpg", address);

                  

        }
        private void decoding(string codes, string address )
        {
            // ref parameters are the codes and address where target picture is restored
            // by calling this function,the encoded picture will be replaced by the decoded one.
            // ref parameters are the codes and address where target picture is restored
            // by calling this function,the old picture will be replaced by the encoded one.

            FileStream picFile = new FileStream(address, FileMode.Open);
            //pictureBox1.Image = new Bitmap(picFile);
            //  textBox1.Text = picFile.Length.ToString();
            long size_of_pic = picFile.Length;
            // begin encoding:
            FileStream myfile = new FileStream("\\My Documents\\My Pictures\\temp_decoded.jjpg", FileMode.CreateNew);
            int size_of_codes = codes.Length;
            StringReader codereader = new StringReader(codes);
            char[] cod = new char[codes.Length];
            byte[] array = new byte[codes.Length];
            byte[] temp = new byte[codes.Length];
            codereader.Read(cod, 0, codes.Length);
            int temp1 = 0;


            int i = 0;
            int l;
            byte j = 0;
            for (i = 0; i <size_of_pic / codes.Length ; i++)
            {

                //  picFile.Read(array, i, size_of_codes);

             l=   picFile.Read(array, 0, codes.Length);
                for (j = 0; j < l; j++)
                {
                    //Convert.ToChar(array[j]);
                    temp1 = array[j] ^ cod[j];
                    temp[j] = Convert.ToByte(temp1);

                }

                myfile.Write(temp, 0, codes.Length);




            }
            int residues;
            residues = (int)(size_of_pic % codes.Length);
          //  textBox2.Text = "";
          //  textBox2.Text = residues.ToString();
            char[] cod1 = new char[residues];
            byte[] array1 = new byte[residues];
            byte[] temp2 = new byte[residues];


            if (residues != 0)
            {
                StringReader codereader2 = new StringReader(codes);
                codereader2.Read(cod1, 0, residues);

                l = picFile.Read(array1, 0, residues);
                for (j = 0; j < residues; j++)
                {
                    //Convert.ToChar(array[j]);
                    temp1 = array1[j] ^ cod1[j];
                    temp2[j] = Convert.ToByte(temp1);

                }

                myfile.Write(temp2, 0, residues);
            }
            
            
 
            picFile.Close();
            File.Delete(address);

            myfile.Close();
            File.Move("\\My Documents\\My Pictures\\temp_decoded.jpg", address);
            


           
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

⌨️ 快捷键说明

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