贴别人的一个文件加密程序!
代码下载
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace WindowsFormsApplication1
{
public static class abc
{
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/**//// <summary>
/// 利用MD5对字符串进行加密
/// </summary>
/// <param name="someStr">待加密的字符串</param>
/// <returns>返回加密后的字符串</returns>
public static string EncryptMD5(string encryptStr)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
UTF8Encoding Encode = new UTF8Encoding();
byte[] HashedBytes = md5Hasher.ComputeHash(Encode.GetBytes(encryptStr));
return Encode.GetString(HashedBytes);
}
/**//// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptStr">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string EncryptDES(string encryptStr, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptStr);
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());
}
catch
{
return encryptStr;
}
}
/**//// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptStr">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string DecryptDES(string decryptStr, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptStr);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptStr;
}
}
/**//// <summary>
/// DES加密文件
/// </summary>
/// <param name="inFilePath">待加密文件</param>
/// <param name="outFilePath">加密后的文件</param>
/// <param name="encryptKey">加密密钥</param>
/// <returns></returns>
public static bool EncryptDES(string inFilePath, string outFilePath, string encryptKey)
{
byte[] rgbIV = Keys;
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
//读入的流
FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
//待写的流
FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
outFs.SetLength(0);
//创建一个变量来帮助读写
byte[] byteIn = new byte[100]; //临时存放读入的流
long readLen = 0; //读入流的长度
long totalLen = inFs.Length; //总共读入流的长度
int everyLen; //每次读入流动长度
//读入InFs,加密后写入OutFs
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(outFs, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
while (readLen < totalLen)
{
everyLen = inFs.Read(byteIn, 0, 100);
encStream.Write(byteIn, 0, everyLen);
readLen = readLen + everyLen;
}
encStream.Close();
outFs.Close();
inFs.Close();
return true;//加密成功
}
catch
{
return false;//加密失败
}
}
/**//// <summary>
/// DES解密文件
/// </summary>
/// <param name="inFilePath">待解密文件</param>
/// <param name="outFilePath">待加密文件</param>
/// <param name="decryptKey">解密密钥</param>
/// <returns></returns>
public static bool DecryptDES(string inFilePath, string outFilePath, string decryptKey)
{
byte[] rgbIV = Keys;
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
//读入的流
FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
//待写的流
FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
outFs.SetLength(0);
//创建一个变量来帮助读写
byte[] byteIn = new byte[100]; //临时存放读入的流
long readLen = 0; //读入流的长度
long totalLen = inFs.Length; //总共读入流的长度
int everyLen; //每次读入流动长度
//读入InFs,解密后写入OutFs
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(outFs, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
while (readLen < totalLen)
{
everyLen = inFs.Read(byteIn, 0, 100);
encStream.Write(byteIn, 0, everyLen);
readLen = readLen + everyLen;
}
encStream.Close();
outFs.Close();
inFs.Close();
return true;//解密成功
}
catch{
return false;//解密失败
}
}
}
}
转载于:https://www.cnblogs.com/gleamy_ming/archive/2008/12/30/1365265.html
贴别人的一个文件加密程序!相关推荐
- 手把手教你制作一个文件加密工具
目录 一.前言 二.思路 (1)加密算法的实现 (二)GUI等的实现 三.代码实现 一.前言 现在,互联网上的安全问题越来越严重.我们的文件虽然有杀毒软件保护,但光使用杀毒软件也不是完全安全的.于是, ...
- 3DES文件加密程序
参照<<密码学引论>> 第二版 张焕国 王张宜编著这本书,用MFC编写的框架,实现的使用3DES对文件进行加解密的程序 转载请说明来源 : enjoy5512的博客 http: ...
- Android破解之Lic文件加密程序(首例)
我不会写Android,这是我第一个破解Android的例子,耗时接近一天,希望大神不要见笑! 本程序为商业软件,不便发布APK程序. 不要给我发消息,我不得回,有问题,直接回帖就可以了. 准备工作 ...
- python编写加密程序_用Python实现一个简单的加密程序
生活中,有时候我们需要对一些重要的文件进行加密,Python 提供了诸如 hashlib,base64 等便于使用的加密库. 对于日常学习而言,我们可以借助异或操作,实现一个简单的文件加密程序,从而强 ...
- MFC 基于对文件加密处理
很多人担心自己的文件被别人看到,因此本人开发一个文件加密的工具,用于加密解密当前文件使用,界面如下:
- 勒索病毒服务器文件加密后缀,WannaCry勒索病毒文件加密密码结构
引言勒索病毒Wannacry爆发已经过去一周了,由于该病毒采用加密用户文件方式进行勒索,许多朋友对该病毒文件加解密过程比较感兴趣.在已见到的病毒分析报告[1-4]中对文件加解密过程描述的比较零散.为便 ...
- eCryptfs,文件系统级加密,在登出时自动为文件加密。通过挂载文件解密和卸载文件加密的方式保护文件
Table of Contents 一.文件目录加密与磁盘加密 1.文件目录加密 2.磁盘加密 二.eCryptFS 1.eCryptfs介绍 2.eCrypFS架构 三.eCrypFS安装 四.eC ...
- c语言文件加密大写字母,C语言 文件加密解密
#includejiemi() { int k,l; char fn[40],fs[40]; char ch,mima[40],x; file *f1,*f2; printf("输入要解密的 ...
- Zxy97Encryption 加密程序
Zxy97Encryption 加密程序 ,支持国际化语言(中/英),使用方法: java -jar jar包路径 <文件输入路径> <文件输出路径> <数字密码,0表示 ...
最新文章
- LeetCode Generate Parentheses
- linux如何取文件列名,Linux_根据表名和索引获取需要的列名的存储过程,复制代码 代码如下: create proc p - phpStudy...
- 第一年的要求 工程系的研究生
- Java期末复习——ch02基本类型(进制转换,数据类型转换,汉字编码)
- python怎么做乘法表_python怎么写乘法表
- Apache Flink 零基础入门(十三)Flink 计数器
- Ubuntu Linux中配置Mplayer万能播放器
- 前端开发 margin外边距 0229
- 安装 Tensorflow
- php 实现树状组织图插件,使用jstree插件实现树形结构
- 你了解的工厂模式可能有误
- 1.编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现。
- 拓端tecdat|Python实现谱聚类Spectral Clustering算法和改变簇数结果可视化比较
- Xmind 8 下载以及破解
- mysql 题库_题库Mysql表设计案例
- Win11自动关机设置在哪?Win11设置自动关机的两种方法
- Html怎么判断ie5,css怎样判断IE浏览器?
- 备份win10的驱动程序
- 苹果手机对html的要求,原神iPhone 8能玩吗?苹果手机最低配置要求一览
- Unity3D教程笔记——unity初始03
热门文章
- 从0到1构建数据科学竞赛知识体系,有夕,鱼佬,茂霖等竞赛大咖将特邀分享...
- 开发者必备!Github 上 1.6W 星的「黑魔法」,早知道就不会秃头了
- 为什么我劝你不要当数据科学家?
- 武汉大学:全校本科毕业论文答辩,以线上方式进行!
- ICCV 2019:谷歌获最佳论文!中国入选论文最多,中科院、清华领跑
- 漫画:生活很苦!当你扛不下去想放弃时,一定要过来看看!
- 520这天,我突然意识到,她根本配不上我这么聪明的男人
- RasberryPi快速启动(适合首次接触树莓派学习者)
- 美多商城之购物车(购物车存储方案)
- 迪杰斯特拉算法。简单理解。内含示例