C#的DES加密解密算法

des是常用的对称加密解密方法,下面是C#下的核心代码

/// <summary>
        
/// 进行DES加密。
        
/// </summary>
        
/// <param name="pToEncrypt">要加密的字符串。</param>
        
/// <param name="sKey">密钥,且必须为8位。</param>
        
/// <returns>以Base64格式返回的加密字符串。</returns>

         public  string Encrypt( string pToEncrypt,  string sKey)
         {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }

                string str = Convert.ToBase64String(ms.ToArray());
                ms.Close();
                return str;
            }

        }


         /// <summary>
        
/// 进行DES解密。
        
/// </summary>
        
/// <param name="pToDecrypt">要解密的以Base64</param>
        
/// <param name="sKey">密钥,且必须为8位。</param>
        
/// <returns>已解密的字符串。</returns>

         public  string Decrypt( string pToDecrypt,  string sKey)
         {
            byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }

                string str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }

        }
        


在代码中还要应用个类库:using System.Security.Cryptography;
des加密在java中也有相应的算法,而且和.net下的一致,可以用于不同语言编写的系统的加解密调用
下面一段为java下的des加密,C#下的解密小例子:

java中使用DES加密,C#中解密过程如下,
java中的加密代码,DES.java类代码:

————————————————————————————————————————————————
package welcome;

import javax.crypto.*;
import javax.crypto.*;
import java.io.UnsupportedEncodingException;
import java.security.spec.*;
import javax.crypto.spec.*;

public  class DES {

     public DES()
    {
    }

      public String encrypt(String str) {

         byte[] enc =  null;
         try {
          enc = desEncrypt(str, "abcdefgh");
        }
         catch (Exception ex) {
        }
       
          return  new sun.misc.BASE64Encoder().encode(enc);
     }
     
      public  static  byte[] desEncrypt(String message, String key)  throws Exception {
             Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
 
             DESKeySpec desKeySpec =  new DESKeySpec(key.getBytes("UTF-8"));
 
             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
             SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
             IvParameterSpec iv =  new IvParameterSpec(key.getBytes("UTF-8"));
             cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
 
              return cipher.doFinal(message.getBytes("UTF-8"));
         }
}

————————————————————————————————————————————————

调用示例代码,jsp1.jsp:

————————————————————————————————————————————————

<%@ page contentType="text/html; charset=gb2312" %>
<jsp:useBean id="DES" scope="page"  class="welcome.DES" />
<html>
<head><title>DES File</title></head>

<body bgcolor="#FFFFFF">
<div align="center"><center>
<%
String Test = request.getParameter("Test");
if(Test== null || Test.equals("")) {
%>
    <form name="form" method="post" action="jsp1.jsp">
    <input type="text" name="Test" size="25" value=""/>
    <input type="submit" name="button" value=" 确定 "/>
    </form>
    <%
} else{
            out.println("加密前的数据:"+Test +"<br/>");
            out.println("加密后的数据:"+DES.encrypt(Test) +"<br/><a href='http://localhost:9003/JiaMi.aspx?str="+DES.encrypt(Test)+"' target=_blank>连接</a>");
      }
    %>
</center></div>
</body>
</html>


————————————————————————————————————————————————

C#中的解码函数:

// / <summary>
//
/ 进行DES解密。
//
/ </summary>
//
/ <param name="pToDecrypt">要解密的以Base64</param>
//
/ <returns>已解密的字符串。</returns>
public string Decrypt(string pToDecrypt,string sKey)
{
     byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
    using(DESCryptoServiceProvider des =  new DESCryptoServiceProvider())
    {
        des.Key=ASCIIEncoding.ASCII.GetBytes(sKey);
        des.IV=ASCIIEncoding.ASCII.GetBytes(sKey);
        System.IO.MemoryStream ms =  new System.IO.MemoryStream();
        using(CryptoStream cs =  new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write))
        {
            cs.Write(inputByteArray,0,inputByteArray.Length);
            cs.FlushFinalBlock();
            cs.Close();
        }
        string str = Encoding.UTF8.GetString(ms.ToArray());
        ms.Close();
         return str;
    }
}
————————————————————————————————————————————————
调用:
string str = Page.Request.QueryString["str"];
Page.Response.Write("得到的为:"+Decrypt(str,"abcdefgh"));
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DES(Data Encryption Standard)是一种对称加密算法,用于保护计算机和网络中的数据安全。DES算法使用相同的密钥进行加密和解密操作。 首先,我们来讨论DES的加密过程。在加密过程中,原始数据会被分成64位的数据块,并经过一系列复杂的置换、替代和混合等步骤,最终生成一个64位的密文数据块。加密过程中所使用的密钥也是64位的,通过子密钥的生成和迭代,将密钥与数据进行混合运算,从而实现加密操作。DES算法的强度在于它的迭代次数,通常会进行16轮迭代来确保数据的安全性。 接下来,我们来探讨DES的解密过程。在解密过程中,密文数据块会经过与加密过程相反的步骤,通过逆向置换、逆向替代和逆向混合等步骤,最终还原为原始的64位明文数据块。与加密过程类似,解密过程中使用的密钥也是相同的。通过迭代生成的子密钥,将密钥与密文数据块进行反向混合运算,实现解密操作。解密过程同样进行16轮迭代,以确保数据的正确性和完整性。 总之,DES算法是一种使用相同密钥进行加密和解密的对称加密算法。加密过程将原始数据分成64位数据块,并通过一系列复杂的步骤生成相应的密文数据块。解密过程则是将密文数据块经过相反的操作还原为原始的明文数据块。DES算法可以保护数据的机密性和安全性,广泛应用于计算机和网络领域。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值