.net C# ADC接口中DES加密算法
原文档加密算法描述如下:
1.1 消息安全性
Des加密保证消息的不可阅读性。
对消息的加密算法:Base64(DES(MD5(消息体) + 消息体))
消息采用的DES加密算法规则如下:
1.密钥是一个长度16、由16进制字符组成的字符串,如:1234567890ABCDEF
使用时,相临的两位理解为一个16进制数的明文,然后转换为实际使用的8位密钥
2. 待加密数据按照PKCS5规则进行补位。(缺7位补7个0x07,缺6位则补6个0x06,以次类推,如果正好8位,也需要补8个0x08)
3. 实际加密模式选择DES-ECB
4. 经过DES加密后的数据必须通过Base64编码转换为明文的字符串
华为提示java的jar包,但.net 方面是没有方法,网上查询了很多,des方法有很多,加密结果不一样,经常仔细阅读加密算法,终于实现在,提供给大家
代码
using System;
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Security;
using System.Security.Cryptography;
using System.Diagnostics;
using System.IO;
namespace AdcLib.Util
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class SecurityUtil //加密解密类
{
/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name= "a_strString "> 要加密的字符串 </param>
/// <param name= "a_strKey "> 密钥 </param>
/// <returns> 加密后并经base64编码的字符串 </returns>
/// <remarks> 静态方法,采用默认ascii编码 </remarks>
public static string EncryptDES(string a_strString, string a_strKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
//MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
byte[] b = strToToHexByte(a_strKey);
DES.Key = b;
// DES.IV = b;
// DES.Key = b;//ASCIIEncoding.ASCII.GetBytes(a_strKey);//hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.PKCS7;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = ASCIIEncoding.UTF8.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}//end method
private static byte[] strToToHexByte(string hexString)
{
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2),16);
return returnBytes;
}
/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name= "a_strString "> 要解密的字符串 </param>
/// <param name= "a_strKey "> 密钥 </param>
/// <returns> 解密后的字符串 </returns>
/// <exception cref= " "> 密钥错误 </exception>
/// <remarks> 静态方法,采用默认ascii编码 </remarks>
public static string DecryptDES(string a_strString, string a_strKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = strToToHexByte(a_strKey);
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.PKCS7;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = " ";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = ASCIIEncoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
catch (Exception e)
{
throw (new Exception("Invalid Key or input string is not a valid base64 string ", e));
}
return result;
}//end method
/// <summary>
/// MD5
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string MD5(string data)
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", "").ToLower();
}
}
}
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Security;
using System.Security.Cryptography;
using System.Diagnostics;
using System.IO;
namespace AdcLib.Util
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class SecurityUtil //加密解密类
{
/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name= "a_strString "> 要加密的字符串 </param>
/// <param name= "a_strKey "> 密钥 </param>
/// <returns> 加密后并经base64编码的字符串 </returns>
/// <remarks> 静态方法,采用默认ascii编码 </remarks>
public static string EncryptDES(string a_strString, string a_strKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
//MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
byte[] b = strToToHexByte(a_strKey);
DES.Key = b;
// DES.IV = b;
// DES.Key = b;//ASCIIEncoding.ASCII.GetBytes(a_strKey);//hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.PKCS7;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = ASCIIEncoding.UTF8.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}//end method
private static byte[] strToToHexByte(string hexString)
{
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2),16);
return returnBytes;
}
/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name= "a_strString "> 要解密的字符串 </param>
/// <param name= "a_strKey "> 密钥 </param>
/// <returns> 解密后的字符串 </returns>
/// <exception cref= " "> 密钥错误 </exception>
/// <remarks> 静态方法,采用默认ascii编码 </remarks>
public static string DecryptDES(string a_strString, string a_strKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = strToToHexByte(a_strKey);
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.PKCS7;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = " ";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = ASCIIEncoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
catch (Exception e)
{
throw (new Exception("Invalid Key or input string is not a valid base64 string ", e));
}
return result;
}//end method
/// <summary>
/// MD5
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string MD5(string data)
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", "").ToLower();
}
}
}