67,549
社区成员




//加密
DataUnit.bytesToHex(DataSecurity.desEncrypt(DataUnit.hexToBytes(key),Rs.toString().getBytes()));
//解密
new String(DataSecurity.desDecrypt(DataUnit.hexToBytes(key), DataUnit.hexToBytes(extraparam))).trim()
static public byte[] hexToBytes(String str) {
int len = str.length() / 2;
str = str.toUpperCase();
byte[] bs = str.getBytes();
byte[] ret = new byte[len];
for (int i = 0; i < len * 2; i += 2) {
byte l = 0, h = 0;
l = bs[i + 1];
h = bs[i];
if (l >= '0' && l <= '9')
l -= '0';
else if (l >= 'A' && l <= 'F') {
l -= 'A';
l += 10;
} else
l = 0;
if (h >= '0' && h <= '9')
h -= '0';
else if (h >= 'A' && h <= 'F') {
h -= 'A';
h += 10;
} else
h = 0;
h = (byte) (h << 4);
ret[i / 2] = (byte) (h | l);
}
return ret;
}
/**
* 二进制字节数组转换为16进制字符串
*
* @param ch
* byte[] 二进制字节数组
* @return String 16进制字串
*/
static public String bytesToHex(byte[] ch, int offset, int length) {
String ret = "";
for (int i = 0; i < length; i++)
ret += byteToHex(ch[i + offset]);
return ret;
}
static public String byteToHex(byte ch) {
String str[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F" };
return str[ch >> 4 & 0xF] + str[ch & 0xF];
}
/// <summary>
/// DES解密,返回字符串
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DESDecodeToStr(string str, string key)
{
return Encoding.GetEncoding("utf-8").GetString(DESDecodeToByte(str, key));
}
/// <summary>
/// DES解密,返回16进制
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string DESDecodeTo16(string str, string key)
{
string sresult = byteToHexStr(DESDecodeToByte(str, key));
return sresult;
}
/// <summary>
/// DES解密,返回字节数组
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static byte[] DESDecodeToByte(string str, string key)
{
try
{
byte[] keys = new byte[8];
for (int i = 0; i < 8; i++)
{
keys[i] = Convert.ToByte(key.Substring(i * 2, 2), 16);
}
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Mode = CipherMode.ECB;
// 密钥
provider.Key = keys;
// 偏移量
provider.IV = keys;
byte[] buffer = new byte[str.Length / 2];
for (int i = 0; i < (str.Length / 2); i++)
{
int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
buffer[i] = Convert.ToByte(str.Substring(i * 2, 2), 16); //(byte)num2;
}
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream.Close();
return stream.ToArray();
}
catch (Exception) { return null; }
}
/// <summary>
/// 加密
/// </summary>
/// <param name="data"></param>
/// <param name="Key"></param>
/// <returns></returns>
public static string DESEncrypt16(string data, string Key)
{
try
{
byte[] shuju = System.Text.Encoding.UTF8.GetBytes(data);
byte[] keys = new byte[8];
for (int i = 0; i < 8; i++)
{
keys[i] = Convert.ToByte(Key.Substring(i * 2, 2), 16);
}
DES desEncrypt = new DESCryptoServiceProvider();
desEncrypt.Mode = CipherMode.ECB;
desEncrypt.Key = keys;
byte[] Buffer;
Buffer = shuju;
ICryptoTransform transForm = desEncrypt.CreateEncryptor();
byte[] R;
R = transForm.TransformFinalBlock(Buffer, 0, Buffer.Length);
string return_str = "";
foreach (byte b in R)
{
return_str += b.ToString("X2");
}
//return_str = return_str.Substring(0, 16);
return return_str;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}