DES算法入口参数

DES算法的入口参数有三个:Key、Data、Mode。其中Key为7个字节共56位,是DES算法的工作密钥。Data为8个字节64位,是要被加密或解密的数据;Mode为DES的工作方法,有两种:加密或解密。

  1. 加密解密文件

             /// <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(); }
    }
    }
  2. 加密解密文本
             /// <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)数据加密标准的更多相关文章

  1. Oracle 10g R2 Transparent Data Encryption 透明数据加密

    Oracle 10g R2 Transparent Data Encryption 透明数据加密 本章介绍如何使用透明数据加密来保护Oracle数据库中的敏感数据,该功能使您可以加密数据库列并管理加密 ...

  2. SQL Server ->> Transparent Data Encryption(透明化数据加密)

    Comming later... 参考文献: Transparent Data Encryption (TDE)

  3. 目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;

    1.加密算法的类图结构 2.源代码 2.1代码运行截图 2.2代码的目录结构 2.3具体代码 MethodFactory.java package jiami; public interface Me ...

  4. 数据加密标准(DES)详解

    1 简介 1.1 历史 DES(Data Encryption Standard)是由IBM公司在1974年提出的加密算法,在1977年被NIST定位数据加密标准.随后的很多年里,DES都是最流行的对 ...

  5. 3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)

    3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...

  6. SQL Server安全(9/11):透明数据加密(Transparent Data Encryption)

    在保密你的服务器和数据,防备当前复杂的攻击,SQL Server有你需要的一切.但在你能有效使用这些安全功能前,你需要理解你面对的威胁和一些基本的安全概念.这篇文章提供了基础,因此你可以对SQL Se ...

  7. AES--高级数据加密标准

    AES--高级数据加密标准 对称密码体制的发展趋势将以分组密码为重点.分组密码算法通常由密钥扩展算法和加密(解密)算法两部分组成.密钥扩展算法将b字节用户主密钥扩展成r个子密钥.加密算法由一个密码学上 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. js bind的实现

    call,apply,bind都是用来挟持对象或者说更改this指向的,但是区别还是有的,call 传参是 fn.call(this,1,2,3) apply传参是 fn.apply(this,[1, ...

  2. 对象API

    遍历对象里的每个元素 var obj ={ a:32, b:12, c :342 } for (const key of obj){ if(obj.hasOwnProperty(key)){ cons ...

  3. ETO的公开赛T5《猎杀蓝色空间号》题解

    这道题别看题面这么长,其实题意很简单 就是让你求从起点开始的最长合法区间 合法的要求有两个:兜圈子和直飞 且这两个条件相互独立 (也就是说兜圈子的末尾不会对下面可能出现的直飞造成影响) 举个例子: 1 ...

  4. [SDOI2008]仪仗队(欧拉筛裸题)

    题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如右图 ...

  5. POJ 1066--Treasure Hunt(判断线段相交)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7857   Accepted: 3247 Des ...

  6. BZOJ1607: [Usaco2008 Dec]Patting Heads 轻拍牛头(模拟 调和级数)

    Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 3031  Solved: 1596[Submit][Status][Discuss] Descriptio ...

  7. Oracle VM VirtualBox 安装XP、Win 7

    测试要求 为了少写点lr脚本(其实是不会写),看到fiddler的saz格式文件可以由loadrunner 12读取,本机安装了lr 11,打算虚拟机安装lr 12.通过共享文件夹把文件传过去,生成脚 ...

  8. MySQL备份恢复之mysqldump

      Preface       The day before yesterday,there's a motif about the lock procedure when backing up My ...

  9. CSRF的原理和防范措施

    a)攻击原理: i.用户C访问正常网站A时进行登录,浏览器保存A的cookie ii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参 ...

  10. webpack 4.14配置详解

    1.安装nodejs 官网下载nodejs,安装时可能会爆 2503错误,解决办法是:使用管理员命令执行安装文件.cmd ->命令提示符(管理员)-> 输入: msiexec /packa ...