DES64位加密解密

Posted lovable

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DES64位加密解密相关的知识,希望对你有一定的参考价值。

需要引入System.Security.Cryptography命名空间

 private static byte[] DESKey = new byte[] { 11, 23, 93, 102, 72, 41, 18, 12 };
        private static byte[] DESIV = new byte[] { 75, 158, 46, 97, 78, 57, 17, 36 };
技术分享图片
 public class DESCryptoServerceOrivuder
    {
        private static void EncrtotData(string inName, string outName, byte[] desKey, byte[] desIV)
        {
            FileStream fin = new FileStream(inName, FileMode.OpenOrCreate, FileAccess.Read);
            FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
            fout.SetLength(0);
            byte[] bin = new byte[100];
            long rdlen = 0;
            long totlen = fin.Length;
            int len;
            DES des = new DESCryptoServiceProvider();
            CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
            MessageBox.Show("Encrypting....");
            while (rdlen < totlen)
            {
                len = fin.Read(bin, 0, 100);
                encStream.Write(bin, 0, len);
                rdlen = rdlen + len;

            }
            encStream.Close();
            fout.Close();
            fin.Close();

        }
    }
DESCryptoServerceOrivuder类
技术分享图片
 public static string Decode(string Encode_string)
        {
            DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
            byte[] input = Convert.FromBase64String(Encode_string);
            MemoryStream ibhnenirtstrean = new MemoryStream(input);
            CryptoStream objcryptostream = new CryptoStream(ibhnenirtstrean, objDES.CreateDecryptor(DESKey, DESIV), CryptoStreamMode.Read);
            StreamReader red = new StreamReader(objcryptostream);
            return red.ReadToEnd();
        }
解密方法
技术分享图片
public static string Encode(string Encode_string)
        {
            DESCryptoServiceProvider ibjEES = new DESCryptoServiceProvider();
            MemoryStream objMemoryStream = new MemoryStream();
            CryptoStream objcryptostream = new CryptoStream(objMemoryStream, ibjEES.CreateEncryptor(DESKey, DESIV), CryptoStreamMode.Write);
            StreamWriter objstreamwrite = new StreamWriter(objcryptostream);
            objstreamwrite.Write(Encode_string);
            objstreamwrite.Flush();
            objcryptostream.FlushFinalBlock();
            objMemoryStream.Flush();
            return Convert.ToBase64String(objMemoryStream.GetBuffer(), 0, (int)objMemoryStream.Length);

        }
加密方法
技术分享图片
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string temp = textBox1.Text;
                textBox2.Text = Encode(temp);
            }
            catch (Exception)
            {

                MessageBox.Show("成功过");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox3.Text = Decode(textBox2.Text);
        }
加密解密使用

以上是关于DES64位加密解密的主要内容,如果未能解决你的问题,请参考以下文章

DES加密

DES加密

基于FPGA的DES加解密系统开发(5000字详细介绍欢迎订阅)

java 实现 DES加密 解密算法

DES算法实现

des算法加密解密的实现