求3DES算法(c#.net),具体见说明

叶子哟 2004-06-21 01:13:41
联通的要求:
以3des按ECB模式加密算法加密字符串,然后用BASE64算法与URLEncoding算法进行编码,它的例子是jsp的,在jsp中是如下:
EncodeStr = URLEncoding(Base64(Encrypt(mystr)))),其中mystr是欲加密的串

请问大家谁做过c#的和asp(vbscript)的,找了几个3des的算法,都不行!
...全文
1023 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wtadminxjeri 2004-06-24
  • 打赏
  • 举报
回复
怎么,我上面提供的类编译不过?
支持密钥、编码集
叶子哟 2004-06-24
  • 打赏
  • 举报
回复
虽然问题还没解决,但非常感谢大家,给分,
wtadminxjeri 2004-06-22
  • 打赏
  • 举报
回复
/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name="a_strString">要解密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <returns>解密后的字符串</returns>
/// <exception cref="">密钥错误</exception>
/// <remarks>静态方法,采用默认ascii编码</remarks>
public string Decrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;

ICryptoTransform DESDecrypt = DES.CreateDecryptor();

string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
catch(Exception e)
{
#if DEBUG
Console.WriteLine("错误:{0}" , e) ;
#endif//DEBUG
throw(new Exception("Invalid Key or input string is not a valid base64 string" , e)) ;
}

return result ;
}//end method

/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name="a_strString">要解密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <param name="encoding">编码方式</param>
/// <returns>解密后的字符串</returns>
/// <exception cref="">密钥错误</exception>
/// <remarks>静态方法,指定编码方式</remarks>
public string Decrypt3DES(string a_strString, string a_strKey , Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;

ICryptoTransform DESDecrypt = DES.CreateDecryptor();

string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = encoding.GetString(DESDecrypt.TransformFinalBlock
(Buffer, 0, Buffer.Length));
}
catch(Exception e)
{
#if DEBUG
Console.WriteLine("错误:{0}" , e) ;
#endif//DEBUG
throw(new Exception("Invalid Key or input string is not a valid base64 string" , e)) ;
}

return result ;
}//end method

public string ww_jx(string var) //字符串解析函数
{
string result="";
string s="";
int i=var.Length;
if(i==0) return result;
for(int j=0;j<i;j++)
{
s=var.Substring(j,1);
char s1;
s1=s[0];
int asc=(int)s1;//得到AscII
result=result+"&#"+asc.ToString()+";";
}

return result;
}

}
}
wtadminxjeri 2004-06-22
  • 打赏
  • 举报
回复
using System;
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Security ;
using System.Security.Cryptography ;
using System.Diagnostics ;
namespace app1_jm
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class jm //加密解密类
{


/// <summary>
/// 转换string到Byte树组
/// </summary>
/// <param name="s">要转换的字符串</param>
/// <returns>转换的Byte数组</returns>
public Byte[] StringToByteArray(String s)
{
/*
Char[] ca = s.ToCharArray();
Byte[] ba = new Byte[ca.Length];
for(int i=0; i<ba.Length; i++) ba[i] = (Byte)ca[i];
return ba;*/

return Encoding.UTF8.GetBytes(s) ;
}

/// <summary>
/// 转换Byte数组到字符串
/// </summary>
/// <param name="a_arrByte">Byte数组</param>
/// <returns>字符串</returns>
public string ByteArrayToString(Byte[] a_arrByte)
{
/*
//char[] ca = new char[a_arrByte.Length] ;
for(int i = 0 ; i < a_arrByte.Length ; i ++)
{
result += (char)a_arrByte[i] ;
}*/

return Encoding.UTF8.GetString(a_arrByte) ;
}


/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name="a_strString">要加密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <returns>加密后并经base64编码的字符串</returns>
/// <remarks>静态方法,采用默认ascii编码</remarks>
public string Encrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;

ICryptoTransform DESEncrypt = DES.CreateEncryptor();

byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}//end method

/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name="a_strString">要加密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <param name="encoding">编码方式</param>
/// <returns>加密后并经base63编码的字符串</returns>
/// <remarks>重载,指定编码方式</remarks>
public string Encrypt3DES(string a_strString, string a_strKey , Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;

ICryptoTransform DESEncrypt = DES.CreateEncryptor();


byte[] Buffer = encoding.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}

listhome 2004-06-22
  • 打赏
  • 举报
回复
顶!·
叶子哟 2004-06-21
  • 打赏
  • 举报
回复
顶一下
叶子哟 2004-06-21
  • 打赏
  • 举报
回复
楼上的,编译时出了一个错

“/websip”应用程序中的服务器错误。
--------------------------------------------------------------------------------

指定键的大小对于此算法无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Security.Cryptography.CryptographicException: 指定键的大小对于此算法无效。

源错误:


行 166: tdes.Mode = CipherMode.ECB;
行 167: tdes.Padding = PaddingMode.PKCS7;
行 168: ICryptoTransform des = tdes.CreateEncryptor(cKey,null);
行 169:
行 170: return des.TransformFinalBlock(bStr,0,bStr.Length);


源文件: d:\inetpub\wwwroot\websip\bjsgip.aspx.cs 行: 168
从今天开始吧 2004-06-21
  • 打赏
  • 举报
回复
送你一个,我也是做联通短信的
====================================================
public class encrty
{


public encrty()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private string EncryptData(string spnumber,string usernumber,string servicetag,string accesstime)
{
string EncodeStr = null;
string SpNumber = spnumber;
string UserNumber = usernumber;
string ServiceTag = servicetag;

string AccessTime = accesstime;
string Str =SpNumber + "$" + UserNumber + "$" + ServiceTag + "$" + AccessTime ;

EncodeStr =Encode(Encrypt(Str));
return EncodeStr;
}

public static string Encode(byte[] source)
{
byte[] dest;

dest = Base64Encode(source);

string ret;
ret = new ASCIIEncoding().GetString(dest);
// ret = ret.Replace("+","~");
// ret = ret.Replace("/","@");
// ret = ret.Replace("=","$");
//ret = HttpUtility.UrlEncode(ret);
return ret;

}

//改进的base64的解码
public static byte[] Decode(string source)
{

string dest;
// dest = source.Replace("~","+");
// dest = dest.Replace("@","/");
// dest = dest.Replace("$","=");
dest = source;//HttpUtility.UrlDecode(source);
byte[] ret;
ret = Base64Decode(new ASCIIEncoding().GetBytes(dest));

return ret;
}

//原始base64编码
public static byte[] Base64Encode(byte[] source)
{
if((source==null)||(source.Length ==0))
throw new ArgumentException("source is not valid");

ToBase64Transform tb64 = new ToBase64Transform();
MemoryStream stm = new MemoryStream();
int pos = 0;
byte[] buff;

while (pos + 3 < source.Length)
{
buff = tb64.TransformFinalBlock (source, pos, 3);
stm.Write (buff, 0, buff.Length);
pos += 3;
}

buff = tb64.TransformFinalBlock (source, pos, source.Length - pos);
stm.Write (buff, 0, buff.Length);

return stm.ToArray();

}

public static byte[] Base64Decode(byte[] source)
{
if ((source==null)||(source.Length==0))
throw new ArgumentException("source is not valid");

FromBase64Transform fb64 = new FromBase64Transform();
MemoryStream stm = new MemoryStream();
int pos = 0;
byte[] buff;

while (pos + 4 < source.Length)
{
buff = fb64.TransformFinalBlock (source, pos, 4);
stm.Write (buff, 0, buff.Length);
pos += 4;
}

buff = fb64.TransformFinalBlock (source, pos, source.Length - pos);
stm.Write (buff, 0, buff.Length);
return stm.ToArray();

}

private byte[] Encrypt(string Str)
{
ASCIIEncoding ae = new ASCIIEncoding();

string Key = System.Configuration.ConfigurationSettings.AppSettings["tdesKey"];


byte[] cKey = Decode(Key);


byte[] bStr = Encoding.ASCII.GetBytes(Str);


TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform des = tdes.CreateEncryptor(cKey,null);

return des.TransformFinalBlock(bStr,0,bStr.Length);
}

}
叶子哟 2004-06-21
  • 打赏
  • 举报
回复
彩E也要这个?麻烦
俺现在在做北京联通的短信
gabriel1 2004-06-21
  • 打赏
  • 举报
回复
老兄在做联通彩E吧,这个应该是SSO接口中的spticket的生成。
联通文挡附带一个DemoSp的程序,你可以看看
以下是其中一段。
/// <summary>
/// 解密方法
/// </summary>
/// <param name="content">需要解密的密文内容</param>
/// <param name="secret">解密密钥</param>
/// <returns>返回解密后明文字符串</returns>
public static string Decrypt(string content,string secret)
{
if ((content == null) || (secret == null) || (content.Length == 0) || (secret.Length == 0))
throw new ArgumentNullException("Invalid Argument");

try
{


byte[] Key = TicketTool.GetKey(secret);

byte[] CryByte = TicketTool.Base64Decode(Encoding.ASCII.GetBytes(content));
byte[] DecByte = TicketTool.Decrypt(CryByte,Key);

// if (DecByte.Length < 2)
// throw new System.Exception("Invalid content");

byte[] RealDecByte;
string RealDecStr;
// int NextLen = TicketTool.GetPart(DecByte,0,out RealDecByte);

RealDecByte = DecByte;
// byte[] ClearDecByte = new byte[RealDecByte.Length - UnicodeOrderPrefix.Length];
byte[] Prefix = new byte[Constants.Operation.UnicodeReversePrefix.Length];
Array.Copy(RealDecByte,Prefix,2);


if (TicketTool.CompareByteArrays(Constants.Operation.UnicodeReversePrefix,Prefix))
{
byte SwitchTemp = 0;
for (int i = 0; i < RealDecByte.Length - 1; i = i + 2)
{
SwitchTemp = RealDecByte[i];
RealDecByte[i] = RealDecByte[i + 1];
RealDecByte[i + 1] = SwitchTemp;

}

}

RealDecStr = Encoding.Unicode.GetString(RealDecByte);
return RealDecStr;


}
catch(Exception e)
{
throw e;
}
}
}


完整的加密方法我这有。
叶子哟 2004-06-21
  • 打赏
  • 举报
回复
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
这个是怎么得到的?
g_robin 2004-06-21
  • 打赏
  • 举报
回复
const string m_strEncryptKey = "abcdefgh";

public static string DesEncrypt(string p_strInput)
{
byte[] byKey=null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(m_strEncryptKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(p_strInput);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(System.Exception ex)
{
throw(ex);
}
}

public static string DesDecrypt(string p_strInput)
{
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = new Byte[p_strInput.Length];

try
{
byKey = System.Text.Encoding.UTF8.GetBytes(m_strEncryptKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(p_strInput);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception ex)
{
throw(ex);
}
}
叶子哟 2004-06-21
  • 打赏
  • 举报
回复
忘了说了,密钥是abcdefgh

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧