读书人

C# 3DES加解密,该如何解决

发布时间: 2013-04-12 18:33:11 作者: rapoo

C# 3DES加解密
项目当中为了和别人统一写了一个加解密的东西.加密完全无压力..解密就找不着门儿 求对应的解密方法

方法调用 TripleDESEncrypt("12345678", "H5O79RCD6A11KOVS")
12345678使用key H5O79RCD6A11KOVS 加密结果 231D90593CB2269D


/// <summary>
/// 加密方法
/// </summary>
/// <param name="pToEncrypt">源字符串</param>
/// <param name="sKey">加密KEY</param>
/// <returns>加密好的字符串</returns>
public static string TripleDESEncrypt(string pToEncrypt, string sKey)
{
try
{
StringBuilder ret = new StringBuilder();
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();

foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
catch (Exception ex)
{
return ex.ToString();
}
}
TripleDES

3DES 加密解密 求解密 高分
[解决办法]
using System;
using System.Security.Cryptography;
using System.Text;
namespace Walters.UtilityBox
{
/// <summary>
/// DES加密/解密类。
/// </summary>
public class DESEncrypt
{
public DESEncrypt()
{
}

#region ========加密========

/// <summary>
/// 加密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Encrypt(string Text)
{
return Encrypt(Text, "LEE");
}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Encrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret=new StringBuilder();
foreach( byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}",b);
}
return ret.ToString();
}

#endregion

#region ========解密========


/// <summary>
/// 解密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Decrypt(string Text)
{
return Decrypt(Text, "LEE");
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();


int len;
len=Text.Length/2;
byte[] inputByteArray = new byte[len];
int x,i;
for(x=0;x<len;x++)
{
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x]=(byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}

#endregion


}
}

[解决办法]
搜索常用类,或是加解密类 好多
[解决办法]
把1楼的md5给去掉
[解决办法]
普通的加密,SecretKey有长度限制


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Xml;
using System.Security.Cryptography;


/// <summary>
/// 特殊加密算法
/// </summary>
public class jiami
{

public jiami()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

/// <summary>
/// DES对称加密方法
/// </summary>
/// <param name="InitData">原始待加密数据</param>
/// <param name="SecretKey">加密密钥,密钥长度必须为八位有效英文字符</param>
public static string EncryptData(object InitData ,string SecretKey)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
Byte[] inputByteArray = Encoding.Default.GetBytes(InitData.ToString());
//建立加密对象的密钥和偏移量
des.Key = ASCIIEncoding.ASCII.GetBytes(SecretKey);


//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
des.IV = ASCIIEncoding.ASCII.GetBytes(SecretKey);
//使得输入密码必须输入英文文本
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach(Byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
catch
{
return "";
}
}

/// <summary>
/// DES对称解密方法
/// </summary>
/// <param name="EncryptedData">待解密数据</param>
/// <param name="SecretKey">解密密钥,必须是加密时的密钥,密钥长度必须为八位有效英文字符,例如"12secret"</param>
public static string DecryptData(object EncryptedData ,string SecretKey)
{
try
{
string pToDecrypt = EncryptedData.ToString();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();


Byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for(int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(SecretKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(SecretKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch
{
return "";
}
}


}


[解决办法]
楼上几位随便复制粘贴有意思吗?也不看看所用算法。
解密方法如下,已经验证过,100%正确:
/// <summary>
/// 解密方法
/// </summary>
/// <param name="pToDecrypt">待解密串</param>
/// <param name="sKey">加密KEY</param>
/// <returns>解密后的字符串</returns>
public static string TripleDESDecrypt(string pToDecrypt, string sKey)
{
try
{
byte[] buffer = new byte[pToDecrypt.Length / 2];
for (int i = 0; i < (pToDecrypt.Length / 2); i++)
{
int num2 = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 0x10);


buffer[i] = (byte)num2;
}
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

cs.Write(buffer, 0, buffer.Length);
cs.FlushFinalBlock();
cs.Close();
ms.Close();
return Encoding.Default.GetString(ms.ToArray());
}
catch (Exception ex)
{
return ex.ToString();
}
}

读书人网 >C#

热点推荐