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 ...
随机推荐
- C++ vector用法积累
1. vector的初始化 2. vector基本操作 2.1 vector属性 size resize 2.2 vector操作 插入 在最后插入一个元素 push_back() 删除 在最后删除一 ...
- linux总结及常用命令
一.操作系统的作用: 1.是现代计算机系统中最基本和最重要的系统软件 2.承上启下的作用 3.向下对硬件操作进行封装 4.向上对用户和应用程序提供方便访问硬件的接口 二.不同领域的操作系统: 1 ...
- 程序设计的SOLID原则
要想设计一个良好的程序,建议采用SOLID原则,若考虑了SOLID,可以使程序在模块内具有高内聚.而模块间具有低耦合的特点. SOLID原则包括5方面的内容: S---单责任原则(SRP) 一个模块只 ...
- Python入门学习笔记4:他人的博客及他人的学习思路
看其他人的学习笔记,可以保证自己不走弯路.并且一举两得,即学知识又学方法! 廖雪峰:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958 ...
- POJ2762 单向连通图(缩点+拓扑排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19552 ...
- JAVA 泛型之类型擦除
★ 泛型是 JDK 1.5 版本引进的概念,之前是没有泛型的概念的,但泛型代码能够很好地和之前版本的代码很好地兼容. CollectionTest.java ---编译成CollectionTest. ...
- mysql 时间相关sql , 按天、月、季度、年等条件进行查询
#今天 select * from or_order_task where to_days(created_date)=to_days(now()); #近七天 select * day )<= ...
- ABAP CDS ON HANA-(10)項目結合して一つ項目として表示
Numeric Functions ABS(arg) CEIL(arg) DIV(arg1, arg2) DIVISION(arg1, arg2, dec) FLOOR(arg) MOD(arg1, ...
- shell重温---基础篇(输入/输出重定向)
大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回到您的终端.一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端.同样,一个命令通常将其输出写入到标准 ...
- Oozie 配合 sqoop hive 实现数据分析输出到 mysql
文件/RDBMS -> flume/sqoop -> HDFS -> Hive -> HDFS -> Sqoop -> RDBMS 其中,本文实现了 使用 sqoo ...