博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DES文件字符加密解密
阅读量:5832 次
发布时间:2019-06-18

本文共 7626 字,大约阅读时间需要 25 分钟。

Codeclass DES    {        ///         /// Call this function to remove the key from memory after use for security        ///         ///         ///         /// 
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")] public static extern bool ZeroMemory(IntPtr Destination, int Length); /// /// Function to Generate a 64 bits Key. /// ///
返回生成的密钥
public static string GenerateKey() { // Create an instance of Symetric Algorithm. Key and IV is generated automatically. DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); // Use the Automatically generated key for Encryption. return ASCIIEncoding.ASCII.GetString(desCrypto.Key); } #region 加密文件 /// /// 加密文件 /// /// 要加密的文件 /// 加密后保存的文件 /// 密钥 public static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey) { using (FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read)) { byte[] bytearrayinput = new byte[fsInput.Length]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); fsInput.Close(); FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Close(); fsEncrypted.Close(); } } #endregion #region 解密文件 /// /// 解密文件 /// /// 要解密的文件 /// 解决后保存的文件 /// 密钥 public static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); //A 64 bit key and IV is required for this provider. //Set secret key For DES algorithm. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //Set initialization vector. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //Create a file stream to read the encrypted file back. using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read)) { //Create a DES decryptor from the DES instance. ICryptoTransform desdecrypt = DES.CreateDecryptor(); //Create crypto stream set to read and do a //DES decryption transform on incoming bytes. CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read); //Print the contents of the decrypted file. StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); } } /// /// 解密文件并返回内容 /// /// 要解密的文件路径 /// 密钥 ///
返回内容
public static string DecryptFile(string sInputFilename, string sKey) { try { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); //A 64 bit key and IV is required for this provider. //Set secret key For DES algorithm. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //Set initialization vector. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //Create a file stream to read the encrypted file back. using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read)) { byte[] byt = new byte[fsread.Length]; fsread.Read(byt, 0, byt.Length); fsread.Flush(); fsread.Close(); //Create a DES decryptor from the DES instance. ICryptoTransform desdecrypt = DES.CreateDecryptor(); MemoryStream ms = new MemoryStream(); CryptoStream cryptostreamDecr = new CryptoStream(ms, desdecrypt, CryptoStreamMode.Write); cryptostreamDecr.Write(byt, 0, byt.Length); cryptostreamDecr.FlushFinalBlock(); cryptostreamDecr.Close(); return Encoding.UTF8.GetString(ms.ToArray()).Trim(); } } catch { return ""; } } #endregion #region DES加密 /// /// DES加密 /// /// 需要加密的字符串 ///
加密后的字符串
public static string DESEncrypt(string pToEncrypt, string DesKeyStr) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(DesKeyStr); des.IV = ASCIIEncoding.ASCII.GetBytes(DesKeyStr); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString(); } catch { System.Windows.Forms.MessageBox.Show("输入注册码错误,请重新输入!!"); return ""; } } #endregion #region DES解密 /// /// DES解密 /// /// 加密后的字符串 ///
解密后的字符串
public static string DESDecrypt(string pToDecrypt, string DesKeyStr) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(DesKeyStr); des.IV = ASCIIEncoding.ASCII.GetBytes(DesKeyStr); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } catch { System.Windows.Forms.MessageBox.Show("输入注册码错误,请重新输入!!"); return ""; } } #endregion }

转载地址:http://fqedx.baihongyu.com/

你可能感兴趣的文章
RPC框架的可靠性设计
查看>>
Rust编程语言的核心部件
查看>>
WiFi万能钥匙万玉权:管理应该是“自下而上”
查看>>
Visual Studio最新特性分析
查看>>
Kubernetes集群中的高性能网络策略
查看>>
eBay是如何将Envoy作为边缘代理的:将硬件负载均衡器替换为软件解决方案
查看>>
本地vs云:大数据厮杀的最终幸存者会是谁?
查看>>
Java的内存 -JVM 内存管理
查看>>
antigen简介
查看>>
Angular访问WebApi出现options方法
查看>>
报表富文本使用介绍
查看>>
Java实现定时任务
查看>>
ZIP 算法详解 (转!)
查看>>
solidity_mapping_implementation
查看>>
《编程珠玑,字字珠玑》读书笔记完结篇——AVL树
查看>>
当学术大家遇到技术大拿,如何攻克数据库应用头号难题?数位产学研大咖这样解读...
查看>>
Elasticsearch支持6.3版本
查看>>
机器学习探索之路1:机器学习相关工具介绍与安装
查看>>
[ 好文分享 ] jQuery最佳实践
查看>>
(30 gadget day 6) 主人,早上好 - iBeacon
查看>>