You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
8.1 KiB
220 lines
8.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
|
|
namespace QZWebApi.Bus
|
|
{
|
|
public class TjSignUtil
|
|
{
|
|
//public static readonly string HospitalId = "6";
|
|
//public static readonly string AESKey = "QpD3TQJNBsloXVNg9mmguc9ql5Y09Vb7";
|
|
//public static readonly string AESJavaToNetKey = "tkvkfP3UFaV/AyJYnoLTCg==";
|
|
|
|
public static long Get_Timestamp()
|
|
{
|
|
var currentTimeMillis = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
|
|
return currentTimeMillis / 1000L;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="DecryptString"></param>
|
|
/// <returns></returns>
|
|
public static string GetSignature(string DecryptString)
|
|
{
|
|
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
|
|
byte[] str1 = Encoding.UTF8.GetBytes(DecryptString);
|
|
byte[] str2 = sha1.ComputeHash(str1);
|
|
sha1.Clear();
|
|
(sha1 as IDisposable).Dispose();
|
|
|
|
return byteToHexStr(str2);
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
|
|
///// <summary>
|
|
///// 无符号右移, 相当于java里的 value>>>pos
|
|
///// </summary>
|
|
///// <param name="value"></param>
|
|
///// <param name="pos"></param>
|
|
///// <returns></returns>
|
|
//public static int RightMove(int value, int pos)
|
|
//{
|
|
// //移动 0 位时直接返回原值
|
|
// if (pos != 0)
|
|
// {
|
|
// // int.MaxValue = 0x7FFFFFFF 整数最大值
|
|
// int mask = int.MaxValue;
|
|
// //无符号整数最高位不表示正负但操作数还是有符号的,有符号数右移1位,正数时高位补0,负数时高位补1
|
|
// value = value >> 1;
|
|
// //和整数最大值进行逻辑与运算,运算后的结果为忽略表示正负值的最高位
|
|
// value = value & mask;
|
|
// //逻辑运算后的值无符号,对无符号的值直接做右移运算,计算剩下的位
|
|
// value = value >> pos - 1;
|
|
// }
|
|
|
|
// return value;
|
|
//}
|
|
|
|
/**
|
|
* 生成验证签名
|
|
* @param timestamp
|
|
* @param openid
|
|
* @return signature
|
|
*/
|
|
public static string GetSignatureBytoken(string token, string timestamp, string openid)
|
|
{
|
|
string[] arr = new string[] { token, timestamp, openid };
|
|
// 将token、timestamp、openid三个参数进行字典排序
|
|
//System.out.println("FF_token:"+WxParamUtil.getToken()+" time:"+timestamp+" id:"+openid);
|
|
Array.Sort(arr);
|
|
StringBuilder content = new StringBuilder();
|
|
for (int i = 0; i < arr.Length; i++)
|
|
{
|
|
content.Append(arr[i]);
|
|
}
|
|
//MessageDigest md = null;
|
|
string tmpStr = null;
|
|
//System.out.println("FF_content:"+content.toString());
|
|
try
|
|
{
|
|
//md = MessageDigest.getInstance("SHA-1");
|
|
// 将三个参数字符串拼接成一个字符串进行sha1加密
|
|
|
|
tmpStr = GetSignature(content.ToString());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
//e.printStackTrace();
|
|
}
|
|
//System.out.println("FF_Signature:"+tmpStr);
|
|
return tmpStr;
|
|
}
|
|
|
|
/// <summary>
|
|
/// AES解密
|
|
/// </summary>
|
|
/// <param name="toDecrypt"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static string Decrypt(string toDecrypt, string key)
|
|
{
|
|
toDecrypt = HexToBase64String(toDecrypt);
|
|
byte[] keyArray = Convert.FromBase64String(key); //将TestGenAESByteKey类输出的字符串转为byte数组
|
|
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
|
|
RijndaelManaged rDel = new RijndaelManaged();
|
|
rDel.Key = keyArray;
|
|
rDel.Mode = CipherMode.ECB; //必须设置为ECB
|
|
rDel.Padding = PaddingMode.PKCS7; //必须设置为PKCS7
|
|
ICryptoTransform cTransform = rDel.CreateDecryptor();
|
|
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
|
return UTF8Encoding.UTF8.GetString(resultArray);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 16进制转Base64
|
|
/// </summary>
|
|
/// <param name="retrunValue"></param>
|
|
/// <returns></returns>
|
|
private static string HexToBase64String(string retrunValue)
|
|
{
|
|
MatchCollection mc = Regex.Matches(retrunValue.ToString(), "[A-F0-9]{2}"); byte[] bytes = new byte[mc.Count]; for (int i = 0; i < mc.Count; i++) { bytes[i] = byte.Parse(mc[i].Value, System.Globalization.NumberStyles.HexNumber); }
|
|
return Convert.ToBase64String(bytes);
|
|
}
|
|
|
|
/// <summary>
|
|
/// AES加密
|
|
/// </summary>
|
|
/// <param name="encryptString"></param>
|
|
/// <param name="encryptKey"></param>
|
|
/// <returns></returns>
|
|
public static string AESEncode(string encryptString, string encryptKey)
|
|
{
|
|
if (string.IsNullOrEmpty(encryptString)) return null;
|
|
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(encryptString);
|
|
RijndaelManaged rm = new RijndaelManaged
|
|
{
|
|
Key = Convert.FromBase64String(encryptKey),
|
|
Mode = CipherMode.ECB,
|
|
Padding = PaddingMode.PKCS7
|
|
};
|
|
ICryptoTransform cTransform = rm.CreateEncryptor();
|
|
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
|
|
//return Convert.ToBase64String(resultArray, 0, resultArray.Length);
|
|
return byteToHexStr(resultArray);
|
|
}
|
|
}
|
|
|
|
public class FileStreamResult : IHttpActionResult
|
|
{
|
|
readonly Stream _stream;
|
|
readonly string _mediaType;
|
|
readonly string _fileName;
|
|
|
|
public FileStreamResult(Stream stream, string mediaType) : this(stream, mediaType, null) { }
|
|
|
|
public FileStreamResult(Stream stream, string mediaType, string fileName)
|
|
{
|
|
_stream = stream;
|
|
_mediaType = mediaType;
|
|
_fileName = fileName;
|
|
}
|
|
|
|
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
|
|
{
|
|
return Task.FromResult<HttpResponseMessage>(Execute());
|
|
}
|
|
|
|
private HttpResponseMessage Execute()
|
|
{
|
|
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
|
|
try
|
|
{
|
|
httpResponseMessage.Content = new StreamContent(_stream);
|
|
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(_mediaType);
|
|
if (!string.IsNullOrEmpty(_fileName))
|
|
{
|
|
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
|
|
{
|
|
FileName = HttpUtility.UrlEncode(_fileName, Encoding.UTF8),
|
|
};
|
|
}
|
|
return httpResponseMessage;
|
|
}
|
|
catch
|
|
{
|
|
httpResponseMessage.Dispose();
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|