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(); } }
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位加密解密的主要内容,如果未能解决你的问题,请参考以下文章