DES64位加密解密
阿新 • • 發佈:2018-06-15
aps object ret click tle 需要 crypt 加密 AS
需要引入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, stringDESCryptoServerceOrivuder類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位加密解密