public IActionResult Delete(string saveName) { saveName = saveName.ToLower(); DirectoryInfo directoryInfo = new DirectoryInfo(FileHelper.GetUploadPath(string.Empty)); FileInfo foundFileInfo = directoryInfo.GetFiles(saveName, SearchOption.AllDirectories).FirstOrDefault(); if (foundFileInfo == null) { foundFileInfo = directoryInfo.GetFiles(StringEncode.EncodeMy(saveName), SearchOption.AllDirectories).FirstOrDefault(); } if (foundFileInfo == null) { foundFileInfo = directoryInfo.GetFiles(HttpUtility.UrlDecode(saveName), SearchOption.AllDirectories).FirstOrDefault(); } if (foundFileInfo != null) { try { foundFileInfo.Delete(); return(new JsonResult("{\"datas\":\"\",\"message\": \"删除成功\",\"success\": true }")); } catch (Exception ex) { LogerHelper.Error(ex.ToString(), "Delete"); return(new JsonResult("{\"datas\":\"\",\"message\": \"删除失败+" + ex.Message + "+\",\"success\": false }")); } } return(NotFound()); }
// byte[] = byte[] /// <summary> /// 压缩后DES加密 /// </summary> /// <param name="hexStringKey"></param> /// <param name="byteSource"></param> /// <returns></returns> public static byte[] ZipAndDelphiDes(string hexStringKey, byte[] byteSource) { string hexStringIV = StringEncode.ByteArrayToHexString(DefaultDesIV); byte[] zipResult = ZipCompress.Compress(byteSource); return(DelphiDesEncrypt(hexStringKey, hexStringIV, zipResult)); }
/// <summary> /// DES解密后解压 /// </summary> /// <param name="hexStringKey"></param> /// <param name="encryptSource"></param> /// <returns></returns> public static byte[] UnDelphiDesAndUnZip(string hexStringKey, byte[] encryptSource) { string hexStringIV = StringEncode.ByteArrayToHexString(DefaultDesIV); byte[] desResult = DelphiDesDecryptToArray(hexStringKey, hexStringIV, encryptSource); return(ZipCompress.DeCompress(desResult)); }
public T Deserialize(byte[] bytes) { var str = StringEncode.GetString(bytes); if (str == null) { return(default(T)); } return(DeserializeFromString(str)); }
// RSA 相关 #region 生成RSA密钥对 /// <summary> /// 生成RSA密钥对 /// </summary> /// <param name="publicKey"></param> /// <param name="privateKey"></param> /// <param name="dwKeySize">密钥长度</param> public static void GenRSAKeyPair(out string publicKey, out string privateKey, int dwKeySize = 512) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(dwKeySize)) { RSAParameters rsaParams = rsa.ExportParameters(true); // 公钥 publicKey = StringEncode.ByteArrayToHexString(rsaParams.Modulus); // 私钥 privateKey = rsa.ToXmlString(true); } }
public byte[] Serialize(T obj) { var str = SerializeToString(obj); if (str == null) { return(new byte[0]); } var bytes = StringEncode.GetBytes(str); return(bytes); }
/// <summary> /// RSA 解密(解密的数据以16进制字符串表示) /// </summary> /// <param name="privateKey">Json表示的私钥</param> /// <param name="hexSource">需要解密的数据以16进制字符串表示</param> /// <returns>解密的数据以16进制字符串表示</returns> public static string Decrypt(string privateKey, string hexSource) { try { byte[] dataToDecrypt = hexSource.HexStringToByteArray(); byte[] dataDecrypted = Decrypt(privateKey, dataToDecrypt); if (dataDecrypted != null) { return(StringEncode.ByteArrayToHexString(dataDecrypted)); } else { return(""); } } catch { return(""); } }
/// <summary> /// RSA 加密 /// </summary> /// <param name="hexSource">需要加密的数据以16进制字符串表示</param> /// <param name="hexPublicKey">16进制字符串表示的公钥</param> /// <returns>加的数据以16进制字符串表示</returns> public static string RSAEncrypt(string hexSource, string hexPublicKey) { try { byte[] dataToEncrypt = hexSource.HexStringToByteArray(); byte[] dataEncrypted = RSAEncrypt(dataToEncrypt, hexPublicKey); if (dataEncrypted != null) { return(StringEncode.ByteArrayToHexString(dataEncrypted)); } else { return(""); } } catch { return(""); } }
/// <summary> /// RSA 加密 /// </summary> /// <param name="source">源字符串</param> /// <param name="hexPublicKey">16进制字符串表示的公钥</param> /// <returns>加的数据以16进制字符串表示</returns> public static string RSAEncryptString(string source, string hexPublicKey) { try { byte[] dataToEncrypt = Encoding.UTF8.GetBytes(source); byte[] dataEncrypted = RSAEncrypt(dataToEncrypt, hexPublicKey); if (dataEncrypted != null) { return(StringEncode.ByteArrayToHexString(dataEncrypted)); } else { return(""); } } catch { return(""); } }
/// <summary> /// 获取所有网卡的mac物理地址 /// </summary> /// <returns></returns> public static List <string> GetPhysicalAddresss() { List <string> macList = new List <string>(); try { IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties(); NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); // Console.WriteLine("Interface information for {0}.{1} ", computerProperties.HostName, computerProperties.DomainName); if (nics == null || nics.Length < 1) { // Console.WriteLine(" No network interfaces found."); return(null); } // Console.WriteLine(" Number of interfaces ... : {0}", nics.Length); foreach (NetworkInterface adapter in nics) { IPInterfaceProperties properties = adapter.GetIPProperties(); // Console.WriteLine(); // Console.WriteLine(adapter.Description); // Console.WriteLine(string.Empty.PadLeft(adapter.Description.Length, '=')); // Console.WriteLine(" Interface type ......... : {0}", adapter.NetworkInterfaceType); // Console.Write(" Physical address ....... : "); PhysicalAddress address = adapter.GetPhysicalAddress(); byte[] bytes = address.GetAddressBytes(); var mac = StringEncode.ByteArrayToHexString(bytes); if (!string.IsNullOrEmpty(mac) && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback) { macList.Add(mac); } // Console.WriteLine(mac); } } catch (Exception ex) { NLogUtility.ExceptionLog(ex, "GetPhysicalAddresss", "NetworkInterfaceHelper"); } return(macList); }
/// <summary> /// RSA 加密 /// </summary> /// <param name="data">需要加密的数据</param> /// <param name="hexPublicKey">16进制字符串表示的公钥</param> /// <returns></returns> public static byte[] RSAEncrypt(byte[] data, string hexPublicKey) { try { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { byte[] exponent = { 1, 0, 1 }; RSAParameters rsaParamsPub = new RSAParameters() { Modulus = StringEncode.HexStringToByteArray(hexPublicKey), Exponent = exponent, }; rsa.ImportParameters(rsaParamsPub); return(rsa.Encrypt(data, false)); } } catch { return(null); } }
/// <summary> /// 3Des加密,密钥长度必需是24字节 /// </summary> /// <param name="hexStringKey">十六进制密钥字符串</param> /// <param name="hexStringIV">十六进制向量字符串</param> /// <param name="source">源字符串</param> /// <returns>加密后的十六进制字符串</returns> public static string TripleDesEncryptToHex(string hexStringKey, string hexStringIV, string source) { byte[] result = TripleDesEncrypt(hexStringKey, hexStringIV, source); return(StringEncode.ByteArrayToHexString(result)); }
/// <summary> /// 生成随机3DES密钥 /// </summary> /// <returns>十六进制字符串表示的密钥</returns> public static string GenerateHexString3DESKey() { byte[] byteArray = GenerateByteArray3DESKey(); return(StringEncode.ByteArrayToHexString(byteArray)); }
/// <summary> /// 压缩原字符串到16进制字符串的结果 /// </summary> /// <param name="source">原字符串</param> /// <returns>Hex编码的结果</returns> public static string CompressStringToHex(string source) { byte[] byteDest = CompressStringToArray(source); return(StringEncode.ByteArrayToHexString(byteDest)); }
/// <summary> /// 3Des加密(默认密钥,默认向量) /// </summary> /// <param name="source">源字符串</param> /// <returns>加密后的十六进制字符串</returns> public static string TripleDesEncryptForPasswordToHex(string source) { byte[] result = TripleDesEncryptForPassword(source); return(StringEncode.ByteArrayToHexString(result)); }
public EPContent <FileResultDto> Upload(string virtualDic, string waterText = "", bool createThumbnail = true) { int currentChunks; int.TryParse(Request.Form["chunk"].ToString(), out currentChunks); int chunksTotal; int.TryParse(Request.Form["chunks"].ToString(), out chunksTotal); createThumbnail = true; var fullPath = FileHelper.GetUploadPath(HttpUtility.UrlDecode(virtualDic)); List <FileResultDto> files = new List <FileResultDto>(); if (Request.Form.Files.Count > 0) //批量上传文件 { try { for (int j = 0; j < Request.Form.Files.Count; j++) { var uploadFile = Request.Form.Files[j]; int offset = currentChunks; //当前分块 int total = chunksTotal; //总的分块数量 //文件没有分块 if (total == 0) { var fileOldName = StringEncode.EncodeMy(uploadFile.FileName.ToLower()); System.IO.FileInfo fiold = new FileInfo(fileOldName); //生成唯一的保存文件的名字和路径 var pointIdx = fiold.Name.LastIndexOf('.'); var newName = fiold.Name.Insert(pointIdx, $"_S_{ DateTime.Now.ToString("yyyyMMddHHmmssfff")}"); var newfilepath = $"{fullPath}\\{newName}"; LogerHelper.Debug($"saveFileLogs{newfilepath}", "Upload"); if (uploadFile.Length > 0) { uploadFile.SaveFile(newfilepath); var thumbnailName = FileProcess.Process(new FileInfo(newfilepath), waterText); var fileRst = new FileResultDto() { OriginalName = uploadFile.FileName, SaveName = newName, ThumbnailName = thumbnailName, VirtulPath = $"/Upload/{ HttpUtility.UrlDecode(virtualDic) }/{newName}" }; if (!string.IsNullOrEmpty(thumbnailName)) { fileRst.ThumbnailVirtulPath = $"/Upload/{ HttpUtility.UrlDecode(virtualDic) }/{thumbnailName}"; } files.Add(fileRst); } } else { // 分块上传,则从form里面取文件名,每次存储的都是分块,不是文件,files里面提交的也是文件块。 string name = Request.Form["name"].ToString().ToLower(); var hisName = name; name = StringEncode.EncodeMy(name); // 文件 分成多块上传 var fileName = WriteTempFile(uploadFile, offset, name); if (total - offset == 1) { //生成唯一的保存文件的名字和路径 System.IO.FileInfo fiold = new FileInfo(name); var pointIdx = fiold.Name.LastIndexOf('.'); var newName = fiold.Name.Insert(pointIdx, $"_S_{ DateTime.Now.ToString("yyyyMMddHHmmssfff")}"); var newfilepath = $"{fullPath}\\{newName}"; // 如果是最后一个分块文件 ,则把文件从临时文件夹中移到上传文件 夹中 FileInfo newFile = new FileInfo(newfilepath); if (newFile.Exists) { // 文件名存在则删除旧文件 newFile.Delete(); } lock (fileMoveLock) { System.IO.File.Move(fileName, newfilepath); } var thumbnailName = FileProcess.Process(new FileInfo(newfilepath), waterText); var fileRst = new FileResultDto() { OriginalName = hisName, SaveName = newName, ThumbnailName = thumbnailName, VirtulPath = $"/Upload/{ HttpUtility.UrlDecode(virtualDic) }/{newName}" }; if (!string.IsNullOrEmpty(thumbnailName)) { fileRst.ThumbnailVirtulPath = $"/Upload/{ HttpUtility.UrlDecode(virtualDic) }/{thumbnailName}"; } files.Add(fileRst); } } } } catch (Exception ex) { Response.WriteAsync("Message" + ex.ToString()); LogerHelper.Error(ex.ToString(), "Upload"); return(new EPContent <FileResultDto>(ex.Message, false, null)); } } Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.CacheControl] = "no-cache"; LogerHelper.Debug(SerializerHelper.ToJson(files)); return(new EPContent <FileResultDto>(string.Empty, true, files)); }
// 3. 默认向量 ? = string #region public static string DelphiDesDecrypt(string hexStringKey, byte[] encryptSource) /// <summary> /// Des解密(使用默认向量),密钥长度必需是8字节 /// </summary> /// <param name="hexStringKey">十六进制密钥字符串</param> /// <param name="encryptSource">加密后的字节数组</param> /// <returns>源字符串</returns> public static string DelphiDesDecrypt(string hexStringKey, byte[] encryptSource) { string hexStringIV = StringEncode.ByteArrayToHexString(DefaultDesIV); return(DelphiDesDecrypt(hexStringKey, hexStringIV, encryptSource)); }
/// <summary> /// Des加密(使用默认向量),密钥长度必需是8字节 /// </summary> /// <param name="hexStringKey">十六进制密钥字符串</param> /// <param name="source">源字符串</param> /// <returns>加密后的十六进制字符串</returns> public static string DelphiDesEncryptToHex(string hexStringKey, string source) { byte[] result = DelphiDesEncrypt(hexStringKey, source); return(StringEncode.ByteArrayToHexString(result)); }
// 3. 默认向量 string = ? #region public static byte[] DelphiDesEncrypt(string hexStringKey, string source) /// <summary> /// Des加密(使用默认向量),密钥长度必需是8字节 /// </summary> /// <param name="hexStringKey">十六进制密钥字符串</param> /// <param name="source">源字符串</param> /// <returns>加密后的字节数组</returns> public static byte[] DelphiDesEncrypt(string hexStringKey, string source) { string hexStringIV = StringEncode.ByteArrayToHexString(DefaultDesIV); return(DelphiDesEncrypt(hexStringKey, hexStringIV, source)); }
/// <summary> /// 3Des加密(默认密钥,默认向量) /// </summary> /// <param name="source">源字节数组</param> /// <returns>加密后的十六进制字符串</returns> public static string TripleDesEncryptToHex(byte[] source) { byte[] result = TripleDesEncrypt(source); return(StringEncode.ByteArrayToHexString(result)); }
public async Task <IActionResult> Get(string fileName) { fileName = fileName.ToLower(); if (string.IsNullOrEmpty(fileName)) { return(NotFound()); } try { DirectoryInfo directoryInfo = new DirectoryInfo(FileHelper.GetUploadPath(string.Empty)); FileInfo foundFileInfo = directoryInfo.GetFiles(fileName, SearchOption.AllDirectories).FirstOrDefault(); if (foundFileInfo == null) { foundFileInfo = directoryInfo.GetFiles(StringEncode.EncodeMy(fileName), SearchOption.AllDirectories).FirstOrDefault(); } if (foundFileInfo == null) { foundFileInfo = directoryInfo.GetFiles(HttpUtility.UrlDecode(fileName), SearchOption.AllDirectories).FirstOrDefault(); } if (foundFileInfo != null) { var memoryStream = new MemoryStream((int)foundFileInfo.Length); using (var stream = new FileStream(foundFileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)) { await stream.CopyToAsync(memoryStream); } memoryStream.Seek(0, SeekOrigin.Begin); // 获取文件的ContentType var provider = new FileExtensionContentTypeProvider(); var memi = provider.Mappings[foundFileInfo.Extension]; var idx = fileName.ToUpper().IndexOf("_S_", StringComparison.Ordinal); var srcName = fileName; if (idx > 0) { srcName = srcName.Substring(0, idx) + foundFileInfo.Extension; } // 文件名必须编码,否则会有特殊字符(如中文)无法在此下载。 string encodeFilename = HttpUtility.UrlEncode(srcName, System.Text.Encoding.GetEncoding("UTF-8")); if (!memi.Contains("image") && !memi.Contains("pdf") && !VideoHelper.VideoExts.Contains(foundFileInfo.Extension.ToLower())) { Response.Headers.Add("Content-Disposition", "attachment;filename=\"" + encodeFilename + "\""); } Response.Headers.Add("Accept-Charset", "UTF-8"); Response.Headers.Add("Content-type", memi); return(new FileContentResult(memoryStream.GetBuffer(), memi)); } } catch (Exception ex) { LogerHelper.Error(ex.ToString(), "Get"); Response.WriteAsync("Message" + ex.ToString()); } return(NotFound()); }