前言:

好久没有写C#代码了,很多原因吧,还是需要重新整装上阵,后续可能会写更多C#的文章,算是自己的工作积累吧。


关于密码加密有很多方法,这里先介绍utf-8这种方法,后续有机会再更新更多方法吧。

下面是demo的界面

1、在Form1.cs里面的

using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using Machine_LogCopy.Data.Interfaces;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        public static string GetSubString(string p_SrcString, int p_Length, string p_TailString)
        {
            string myResult = p_SrcString;
            if (p_Length >= 0)
            {
                byte[] bsSrcString = Encoding.Default.GetBytes(p_SrcString);
                if (bsSrcString.Length > p_Length)
                {
                    int nRealLength = p_Length;
                    int[] anResultFlag = new int[p_Length];
                    byte[] bsResult = null;
                    int nFlag = 0;
                    for (int i = 0; i < p_Length; i++)
                    {
                        if (bsSrcString[i] > 127)
                        {
                            nFlag++;
                            if (nFlag == 3)
                            {
                                nFlag = 1;
                            }
                        }
                        else
                        {
                            nFlag = 0;
                        }
                        anResultFlag[i] = nFlag;
                    }
                    if ((bsSrcString[p_Length - 1] > 127) && (anResultFlag[p_Length - 1] == 1))
                    {
                        nRealLength = p_Length + 1;
                    }
                    bsResult = new byte[nRealLength];
                    Array.Copy(bsSrcString, bsResult, nRealLength);
                    myResult = Encoding.Default.GetString(bsResult);
                    myResult = myResult + p_TailString;

                }
            }
            return myResult;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            string encryptKey = "12345678";
            string encryptString = textBox2.Text;
            encryptKey = GetSubString(encryptKey, 8, "");
            encryptKey = encryptKey.PadRight(8, ' ');
            byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            //return Convert.ToBase64String(mStream.ToArray());
            textBox1.Text = Convert.ToBase64String(mStream.ToArray());
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private static  string CustomDB = string.Empty;
        string sPathKeys = "12345678";
        private static string sCustomDB = string.Empty;
        private void button2_Click(object sender, EventArgs e)
        {
            CustomDB = textBox1.Text;
            sCustomDB = Machine_LogCopy.Data.Interfaces.DES.Decode(CustomDB, sPathKeys);
            textBox3.Text = sCustomDB;
        }

  
    }
}
View Code

相关文章:

  • 2021-12-31
  • 2022-12-23
  • 2021-10-12
  • 2021-08-15
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
猜你喜欢
  • 2022-12-23
  • 2021-05-17
  • 2021-12-21
  • 2021-08-13
相关资源
相似解决方案