Jack-Leung

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
/// <summary>
/// 请求数据加密
/// </summary>
/// <param name="dic">待验签集合</param>
/// <returns>待验签字符串</returns>
public static string getSHA256Sign(Dictionary<string, string> dic)
{
var re = from pair in dic orderby pair.Key select pair;
StringBuilder temp = new StringBuilder("");
foreach (KeyValuePair<string, string> pair in re)
{
if (string.IsNullOrEmpty(pair.Value)) continue;
temp.Append(pair.Key.ToLower()).Append("=").Append(pair.Value).Append("&");
}
string signStr = temp.ToString().Substring(0, temp.Length - 1);
string sign = SHA256Encrypt(signStr);
return sign;
}

//SHA256加密
public static string SHA256Encrypt(string str)
{
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] by = Sha256.ComputeHash(SHA256Data);
return BitConverter.ToString(by).Replace("-", "").ToLower();
}



/// <summary>
/// 加密接口
/// </summary>
/// <param name="urlString">加密原串(不传入访问地址,只需要传入数据)</param>
/// <param name="key">公钥后30位</param>
/// <returns></returns>
public static string doDecrypt(string urlString, string key)
{
byte[] cipherBytes = DESEncrypt("UTF-16BE", urlString, "ISO-8859-1", key);
string basedString = Base64Encode(cipherBytes);
basedString = Regex.Replace(basedString, @"\S{76}", "$0\r\n"); //在JAVA中 Base64每76个字符加一个换行符
string resultString = basedString.Replace("+", ",");
return UrlEncode(resultString) + "\r\n";
}

public static string UrlEncode(string str)
{
StringBuilder builder = new StringBuilder();
foreach (char c in str)
{
if (HttpUtility.UrlEncode(c.ToString()).Length > 1)
{
builder.Append(HttpUtility.UrlEncode(c.ToString(), Encoding.GetEncoding("ISO-8859-1")).ToUpper());
}
else
{
builder.Append(c);
}
}
return builder.ToString();
}

/// <summary>
/// DES 加密(数据加密标准,速度较快,适用于加密大量数据的场合)
/// </summary>
/// <param name="EncryptString">待加密的密文</param>
/// <param name="EncryptKey">加密的密钥</param>
/// <returns>returns</returns>
public static byte[] DESEncrypt(string dataCoding, string data, string keyCoding, string key)
{
try
{
byte[] keyBytes = Encoding.GetEncoding(keyCoding).GetBytes(key.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = Encoding.GetEncoding(dataCoding).GetBytes(data);

byte[] inputByteJava = new byte[inputByteArray.Length + 2];//JAVA在编码转换时 多两个字节FE FF
inputByteJava[0] = 0xFE;
inputByteJava[1] = 0xFF;

Buffer.BlockCopy(inputByteArray, 0, inputByteJava, 2, inputByteArray.Length);

DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();

// java 默认的是ECB模式,PKCS5padding;c#默认的CBC模式,PKCS7padding 所以这里我们默认使用ECB方式
desProvider.Mode = CipherMode.ECB;
desProvider.Padding = PaddingMode.PKCS7;
MemoryStream memStream = new MemoryStream();
CryptoStream crypStream = new CryptoStream(memStream, desProvider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);

crypStream.Write(inputByteJava, 0, inputByteJava.Length);
crypStream.FlushFinalBlock();
return memStream.ToArray();

}
catch
{
return null;
}
}

/// <summary>
/// Base64加密
/// </summary>
/// <param name="source">待加密的明文</param>
/// <returns></returns>
public static string Base64Encode(byte[] bytes)
{
string encode = string.Empty;
try
{
encode = Convert.ToBase64String(bytes);
}
catch { }
return encode;
}

/// <summary>
/// MD5 加密
/// </summary>
/// <param name="str1"></param>
/// <returns></returns>
public static string Md5(string str1)
{
string cl1 = str1;
string pwd = "";

if (str1 == "")
{
return pwd;
}

MD5 md5 = MD5.Create();
// 加密后是一个字节类型的数组 
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl1));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得 
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 
pwd = pwd + s[i].ToString("x2");
}
return pwd;
}

 

posted on 2018-10-22 16:38  Jack.leung  阅读(242)  评论(0编辑  收藏  举报