DES(Data Encryption Standard)数据加密标准
DES算法入口参数
DES算法的入口参数有三个:Key、Data、Mode。其中Key为7个字节共56位,是DES算法的工作密钥。Data为8个字节64位,是要被加密或解密的数据;Mode为DES的工作方法,有两种:加密或解密。
- 加密解密文件
/// <summary>
/// Enctypt File
/// </summary>
/// <param name="sInputFilename"></param>
/// <param name="sOutputFilename"></param>
/// <param name="sKey"></param>
public void EncryptFile(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); FileStream fin = null;
FileStream fout = null;
CryptoStream cryptoStream = null;
try
{
fin = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
fout = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write);
cryptoStream = new CryptoStream(fout, DES.CreateEncryptor(), CryptoStreamMode.Write); byte[] bin = new byte[]; //This is intermediate storage for the decryption.
long rdlen = ; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time. //Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = fin.Read(bin, , );
cryptoStream.Write(bin, , len);
rdlen = rdlen + len;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cryptoStream != null) { cryptoStream.Close(); }
if (fout != null) { fout.Close(); }
if (fin != null) { fin.Close(); }
}
} /// <summary>
/// Decrypt File
/// </summary>
/// <param name="sInputFilename"></param>
/// <param name="sOutputFilename"></param>
/// <param name="sKey"></param>
public 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); FileStream fin = null;
FileStream fout = null;
CryptoStream cryptoStream = null;
try
{
fin = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
fout = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write);
cryptoStream = new CryptoStream(fin, DES.CreateDecryptor(), CryptoStreamMode.Read); byte[] bin = new byte[]; //This is intermediate storage for the decryption.
long rdlen = ; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time. //Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = cryptoStream.Read(bin, , );
if (len == ) { break; }
fout.Write(bin, , len);
rdlen = rdlen + len;
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (cryptoStream != null) { cryptoStream.Close(); }
if (fout != null) { fout.Close(); }
if (fin != null) { fin.Close(); }
}
} - 加密解密文本
/// <summary>
/// Encrypt Text
/// </summary>
public string DesEncrypt(string pToEncrypt, string sKey)
{
MemoryStream ms = null;
CryptoStream ctyptoStream = null;
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); byte[] inputByteArray = Encoding.ASCII.GetBytes(pToEncrypt); ms = new System.IO.MemoryStream();
ctyptoStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
ctyptoStream.Write(inputByteArray, , inputByteArray.Length);
ctyptoStream.FlushFinalBlock();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (ctyptoStream != null) { ctyptoStream.Close(); }
if (ms != null) { ms.Close(); }
}
return Convert.ToBase64String(ms.ToArray());
} /// <summary>
/// Dectypt Text
/// </summary>
public string DesDecrypt(string pToDecrypt, string sKey)
{
MemoryStream ms = null;
CryptoStream ctyptoStream = null;
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); byte[] inputByteArray = Convert.FromBase64String(pToDecrypt); ms = new System.IO.MemoryStream();
ctyptoStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
ctyptoStream.Write(inputByteArray, , inputByteArray.Length);
ctyptoStream.FlushFinalBlock();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (ctyptoStream != null) { ctyptoStream.Close(); }
if (ms != null) { ms.Close(); }
}
return Encoding.ASCII.GetString(ms.ToArray());
}
DES(Data Encryption Standard)数据加密标准的更多相关文章
- Oracle 10g R2 Transparent Data Encryption 透明数据加密
Oracle 10g R2 Transparent Data Encryption 透明数据加密 本章介绍如何使用透明数据加密来保护Oracle数据库中的敏感数据,该功能使您可以加密数据库列并管理加密 ...
- SQL Server ->> Transparent Data Encryption(透明化数据加密)
Comming later... 参考文献: Transparent Data Encryption (TDE)
- 目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;
1.加密算法的类图结构 2.源代码 2.1代码运行截图 2.2代码的目录结构 2.3具体代码 MethodFactory.java package jiami; public interface Me ...
- 数据加密标准(DES)详解
1 简介 1.1 历史 DES(Data Encryption Standard)是由IBM公司在1974年提出的加密算法,在1977年被NIST定位数据加密标准.随后的很多年里,DES都是最流行的对 ...
- 3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)
3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...
- SQL Server安全(9/11):透明数据加密(Transparent Data Encryption)
在保密你的服务器和数据,防备当前复杂的攻击,SQL Server有你需要的一切.但在你能有效使用这些安全功能前,你需要理解你面对的威胁和一些基本的安全概念.这篇文章提供了基础,因此你可以对SQL Se ...
- AES--高级数据加密标准
AES--高级数据加密标准 对称密码体制的发展趋势将以分组密码为重点.分组密码算法通常由密钥扩展算法和加密(解密)算法两部分组成.密钥扩展算法将b字节用户主密钥扩展成r个子密钥.加密算法由一个密码学上 ...
- TDE: Transparent Data Encryption brief introduction
1. What is TDE? Briefly speaking, TDE is used to encrypted data. 2. The benifits: Belows are come fr ...
- Data Encryption Errors After Restoring Microsoft Dynamics CRM Database
If you’re seeing an error similar to the one above, you’ve probably done a database backup and resto ...
随机推荐
- kali linux 中文出现乱码问题的解决
确定locales已经安装,用”apt-get install locales”命令:之后可用”locale -a”查看当前系统支持的字符集. 1. 在命令行输入”dpkg-reconfigure l ...
- 一个关于 json ,加密,测试,集多功能为一体的在线工具
很多情况下我们会序列化json或者解析json,那么要解析json也许使用json实体类会好很多,或者你有时候需要压缩转义json的时候, 有一个网站真的是非常好用,里面什么都有......是真的啥都 ...
- CentOS6.5安装zabbix3.2.6
1.安装好httpd和php Zabbix 3需要PHP是至少5.4或更高版本 rpm -ivh http://repo.webtatic.com/yum/el6/latest.rpm yum -y ...
- typecho博客组插件:openSug.js百度搜索框下拉提示免费代码
Typecho候选搜索增强插件:安装openSug插件即可获得带有“搜索框提示”功能的搜索框,让Typecho搜索更便捷! 支持百度.谷歌.雅虎.Yandex.360好搜.UC神马.酷狗.优酷.淘 ...
- 字典树(Trie)的学习笔记
按照一本通往下学,学到吐血了... 例题1 字典树模板题吗. 先讲讲字典树: 给出代码(太简单了...)! #include<cstdio> #include<cstring> ...
- 【转】Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法
Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法 [原文链接] 这篇文章完美的解决了我几个月没结论的bug... 感谢热爱分享的技术达人~ 我是怎么走进这个大坑的 ...
- 3155: Preprefix sum
3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这 ...
- Migrating from MapReduce 1 (MRv1) to MapReduce 2 (MRv2, YARN)...
This is a guide to migrating from Apache MapReduce 1 (MRv1) to the Next Generation MapReduce (MRv2 o ...
- Android Studio引入AAR文件
一.编译生成AAR文件 二.把AAR文件复制到项目的libs目录下 三.在项目的配置文件中加入如下代码: android { //other code repositories{ flatDir{ d ...
- No resources found. Error from server (NotAcceptable): unknown (get pods)
问题:正确安装kubectl后,可查询到当前使用集群服务,也可切换不同的集群,但无法获取当前运行的pods的信息与所有的service,具体表现为 $ kubectl get po -nwx No r ...