private void decryption(string encFilename, byte[] userPW_MD5, string PW, string path) { try { RIPEMD160 myRIPEMD160 = RIPEMD160.Create(); byte[] ivArray = myRIPEMD160.ComputeHash(stringToByteArray(PW)); byte[] inputIV = new byte[MD5_SIZE]; Array.Copy(ivArray, inputIV, MD5_SIZE); if (checkBox2.Checked) { inputIV = stringToByteArray(getCPUID()); } Crypt crypt = new Crypt(userPW_MD5, inputIV); FileInfo info = new FileInfo(encFilename); Maximum = (int)info.Length * 2; // 복호화, 파일 나누기 Progress = Crypt.Progress = 0; crypt.decrypt(encFilename, tempFileName, MD5_SIZE); int delimPos = getPostionFileChar(tempFileName, '|'); if (delimPos == -1) { MessageBox.Show("INCORRECT FILE FORMAT"); endDecryption(); return; } byte[] fInfoByte = new byte[delimPos]; FileStream inputFile = File.OpenRead(tempFileName); inputFile.Read(fInfoByte, 0, delimPos); // inputTempFile.Close(); Progress += delimPos; string fInfoBase64 = ByteArrayToString(fInfoByte); string fileInfo = Base64Decoding(fInfoBase64); // FileStream inputFile = File.OpenRead(Filename); // using (FileStream fsSource = new FileStream(Filename, FileMode.Open, FileAccess.Read)) // { int filecount = int.Parse(fileInfo.Substring(0, fileInfo.IndexOf("\""))); string[] filename = new string[filecount]; string[] checksum = new string[filecount]; int[] filesize = new int[filecount]; fileInfo = fileInfo.Remove(0, fileInfo.IndexOf("\"") + 1); int i = 0; for (i = 0; i < filecount; i++) { filename[i] = fileInfo.Substring(0, fileInfo.IndexOf("\\")); fileInfo = fileInfo.Remove(0, fileInfo.IndexOf("\\") + 1); filesize[i] = int.Parse(fileInfo.Substring(0, fileInfo.IndexOf("\\"))); fileInfo = fileInfo.Remove(0, fileInfo.IndexOf("\\") + 1); checksum[i] = fileInfo.Substring(0, fileInfo.IndexOf("\"")); fileInfo = fileInfo.Remove(0, fileInfo.IndexOf("\"") + 1); } if (inputFile.ReadByte() != Convert.ToByte('|')) { inputFile.Close(); MessageBox.Show("FILE INFOMATION ERROR"); endDecryption(); return; } // inputFile.Seek(getPostionFileChar(tempFileName, '|') + 1, SeekOrigin.Begin); for (i = 0; i < filecount; i++) { int readBytes, readTryBytes, leftBytes; byte[] readBuffer = new byte[10000]; FileStream outputFile = File.Create(path + "\\" + filename[i]); leftBytes = filesize[i]; while (leftBytes > 0) { readTryBytes = (leftBytes > 10000) ? 10000 : leftBytes; readBytes = inputFile.Read(readBuffer, 0, readTryBytes); if (readBytes != readTryBytes) { inputFile.Close(); outputFile.Close(); MessageBox.Show("FILE SIZE ERROR"); endDecryption(); return; } outputFile.Write(readBuffer, 0, readBytes); Progress += readBytes; leftBytes -= readBytes; } outputFile.Close(); if (checksum[i] != GetfileMD5(path + "\\" + filename[i])) { inputFile.Close(); MessageBox.Show("FILE CRASHED"); endDecryption(); return; } } inputFile.Close(); Progress = Maximum; endDecryption(); } catch (Exception e) { MessageBox.Show("There is some problem to decrypt \n maybe it using CPU ID Lock"); } }
private void encrypt(string encFileName, string PW) { File.Delete(tempFileName); FileStream inputFile; FileStream outputFile = File.Create(tempFileName); string fInfoBase64 = getFileInfoBase64(); byte[] fInfoBase64Array = stringToByteArray(fInfoBase64); outputFile.Write(fInfoBase64Array, 0, fInfoBase64Array.Length); outputFile.WriteByte(Convert.ToByte('|')); FileInfo info = null; int readBytes; byte[] readBuffer = new byte[10000]; Maximum = 0; for (int filec = 0; filec < listBox2.Items.Count; filec++) { info = new FileInfo(listBox2.Items[filec].ToString()); Maximum += (int)info.Length; } Maximum *= 2; // 파일 합치기, 암호화 Progress = Crypt.Progress = 0; for (int filec = 0; filec < listBox2.Items.Count; filec++) { inputFile = File.OpenRead(listBox2.Items[filec].ToString()); while (true) { readBytes = inputFile.Read(readBuffer, 0, 10000); if (readBytes <= 0) break; outputFile.Write(readBuffer, 0, readBytes); Progress = Progress + readBytes; /* info = new FileInfo(listBox2.Items[filec].ToString()); maximum = (int)info.Length; for (long i = 0; i < info.Length; i++) { int toWrite = inputFile.ReadByte(); if (toWrite < 0) break; outputFile.WriteByte((byte)toWrite); progress = progress + 1; } */ } inputFile.Close(); Thread.Sleep(10); } outputFile.Close(); FileStream encFile = File.Create(encFileName); encFile.Write(GetByteMD5(PW), 0, MD5_SIZE); encFile.Close(); RIPEMD160 myRIPEMD160 = RIPEMD160.Create(); byte[] keyArray = GetByteMD5(PW); byte[] ivArray = myRIPEMD160.ComputeHash(stringToByteArray(PW)); byte[] inputIV = new byte[MD5_SIZE]; Array.Copy(ivArray, inputIV, MD5_SIZE); if (checkBox2.Checked) { inputIV = stringToByteArray(getCPUID()); } crypt = new Crypt(keyArray, inputIV); crypt.encrypt(tempFileName, encFileName); File.Delete(tempFileName); Progress = Maximum; canClose = true; Progress = Crypt.Progress = 0; }