Asp.Net 中的DES和AES加密、解密

Asp.Net C#中除了MD5加密之外还经常用到了AES加密、解密与DES加密、解密,本次先介绍DES的加密及解密。注意的是DES的密匙是8位的。


private static readonly String strDesKey = "imaoblog";//加密所需8位密匙

///
/// DES加密
///
/// 要加密字符串
/// 返回加密后字符串
public static String Encrypt_DES(String str)
{
    System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();

    Byte[] inputByteArray = System.Text.Encoding.Default.GetBytes(str);
   des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey);
    des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey);
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();

    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    foreach (Byte b in ms.ToArray())
        sb.AppendFormat("{0:X2}", b);

    return sb.ToString();
}

 

 

///
/// DES解密
///
/// 要解密字符串
/// 返回解密后字符串
public static String Decrypt_DES(String str)
{
    System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
    Int32 x;
    Byte[] inputByteArray = new Byte[str.Length / 2];
    for (x = 0; x < str.Length / 2; x++)
        inputByteArray[x] = (Byte)(Convert.ToInt32(str.Substring(x * 2, 2), 16));

    des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey);
    des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey);
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();

    System.Text.StringBuilder ret = new System.Text.StringBuilder();

    return System.Text.Encoding.Default.GetString(ms.ToArray());
}

Asp.NetC#续上次的DES加密、解密之后,再发一个AES的加密、解密。AES要注意的是32位密匙。

 

private static readonly String strAesKey = "iwww.maoblog.comiwww.maoblog.com";//加密所需32位密匙

///
/// AES加密
///
/// 要加密字符串
/// 返回加密后字符串
public static String Encrypt_AES(String str)
{
    Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
    Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str);

    System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
    rDel.Key = keyArray;
    rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
    rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

    System.Security.Cryptography.ICryptoTransform. cTransform. = rDel.CreateEncryptor();
    Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

 

///
/// AES解密
///
/// 要解密字符串
/// 返回解密后字符串
public static String Decrypt_AES(String str)
{
    Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
    Byte[] toEncryptArray = Convert.FromBase64String(str);

    System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
    rDel.Key = keyArray;
    rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
    rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

    System.Security.Cryptography.ICryptoTransform. cTransform. = rDel.CreateDecryptor();
    Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

    return System.Text.UTF8Encoding.UTF8.GetString(resultArray);
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26828358/viewspace-719628/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26828358/viewspace-719628/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值