public static void DeCryptFile(string sourceFileName) { string tempfilename = sourceFileName + ".temp"; AESReader treader = new AESReader(sourceFileName); if (!treader.IsEncrypt) { treader.Dispose(); DLog.LogError("试图解密一个非加密文件或文件无法解密."); return; } FileStream tfilesource = File.Create(tempfilename); bool tisfinished = false; try { byte[] tbuffer = new byte[sOnelen]; long tlength = treader.Length; long readedsize = 0; int treadsize = treader.Read(tbuffer, 0, sOnelen); while (treadsize > 0) { readedsize += treadsize; int twritesize = treadsize; if (readedsize + SafeByteLen >= tlength) { twritesize -= SafeByteLen; } tfilesource.Write(tbuffer, 0, twritesize); tfilesource.Flush(); treadsize = treader.Read(tbuffer, 0, sOnelen); } tisfinished = true; } catch (System.Exception _error) { DLog.LogError(_error); File.Delete(tempfilename); } treader.Close(); treader.Dispose(); tfilesource.Flush(); tfilesource.Close(); tfilesource.Dispose(); if (tisfinished) { 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; } 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 DeCryptFile(string sourceFileName) { string tempfilename = sourceFileName + ".temp"; AESReader treader = new AESReader(sourceFileName); if (!treader.IsEncrypt) { treader.Dispose(); DLog.LogError("试图解密一个非加密文件或文件无法解密."); return; } byte[] tallbytes = treader.ReadAllBytes(); treader.Dispose(); File.WriteAllBytes(tempfilename, tallbytes); 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); }