public static void EnCryptFile(string sourceFileName) { string tempfilename = sourceFileName + ".temp"; AESReader tsourcefile = new AESReader(sourceFileName); if (tsourcefile.IsEncrypt) { DLog.LogError("尝试对已经加密的文件进行2次加密.sourceFileName = " + sourceFileName); tsourcefile.Dispose(); return; } AESWriter twriter = new AESWriter(tempfilename); bool tisfinised = false; try { long tlength = tsourcefile.Length; byte[] tbuffer = new byte[sOnelen]; long readedsize = 0; int treadsize = tsourcefile.Read(tbuffer, 0, sOnelen); while (treadsize > 0) { readedsize += treadsize; twriter.WriteBytesWhere(tbuffer, 0, treadsize); twriter.Flush(); treadsize = tsourcefile.Read(tbuffer, 0, sOnelen); } tisfinised = true; } catch (System.Exception _error) { DLog.LogError(_error); File.Delete(tempfilename); } tsourcefile.Close(); tsourcefile.Dispose(); twriter.Close(); twriter.Dispose(); if (tisfinised) { File.Delete(sourceFileName); File.Move(tempfilename, sourceFileName); } }
public static void EnCryptFile(string sourceFileName) { string tempfilename = sourceFileName + ".temp"; AESReader tsourcefile = new AESReader(sourceFileName); if (tsourcefile.IsEncrypt) { DLog.LogError("尝试对已经加密的文件进行2次加密.sourceFileName = " + sourceFileName); tsourcefile.Dispose(); return; } byte[] tallbytes = tsourcefile.ReadAllBytes(); tsourcefile.Dispose(); AESWriter twriter = new AESWriter(tempfilename); twriter.WriteBytes(tallbytes); twriter.Close(); File.Delete(sourceFileName); File.Move(tempfilename, sourceFileName); }