private void DecodeFileOnSeparateThread(PFFileEncoder encoder, string encryptedInputFile, string tempFileName, StatusTimer st) { TimeSpan ts = new TimeSpan(0, 0, _statusReportIntervalSeconds); PFThread t = new PFThread(new ParameterizedThreadStart(DecodeFileFromBase64), "DecodeFileFromBase64"); List <object> parms = new List <object>(); parms.Add(encoder); parms.Add(encryptedInputFile); parms.Add(tempFileName); parms.Add(st); t.ThreadDescription = "Decode File from Base64 Text."; t.ShowElapsedMilliseconds = false; t.StartTime = DateTime.Now; st.ShowElapsedTimeMilliseconds = false; st.NumSecondsInterval = _statusReportIntervalSeconds; if (st.StatusTimerIsRunning == false) { st.Start(); } t.Start(parms); while (t.ThreadObject.Join(ts) == false) { if (st.StatusReportDue()) { FileInfo fi = new FileInfo(tempFileName); currentStatusReport("DecodeEncryptionFromBase64", "InProgress", fi.Length, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime()); fi = null; } } t.FinishTime = DateTime.Now; }
private void DecodeFileFromBase64(object parameterObject) { try { //_encodingSucceeded = false; List <object> parms = (List <object>)parameterObject; PFFileEncoder encoder = (PFFileEncoder)parms[0]; string encryptedInputFile = Convert.ToString(parms[1]); string tempFileName = Convert.ToString(parms[2]); StatusTimer st = (StatusTimer)parms[3]; encoder.DecodeFileFromBase64(encryptedInputFile, tempFileName); //_encodingSucceeded = true; } catch (System.Exception ex) { //_encodingSucceeded = false; _msg.Length = 0; _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); AppMessages.DisplayErrorMessage(_msg.ToString()); } finally { ; } }
/// <summary> /// Decrypts by loading encryptedInputFile to a string and then decrypting the string. Decrypted string is then written out as text to the outputFile. /// </summary> /// <param name="encryptedInputFile">Full path to file containing the encrypted data.</param> /// <param name="outputFile">Full path to file that will contain decrypted data.</param> /// <param name="st">A StatusTimer object to use for timing the encryption.</param> /// <returns>Returns path to output file.</returns> /// <remarks>This routine assumes the encrypted data is in base64 text format.</remarks> public string Decrypt(string encryptedInputFile, string outputFile, StatusTimer st) { //IStringEncryptor encryptor = new StringEncryptorAES(); //PFStringEncryptor encryptor = new PFStringEncryptor(_encryptionAlgorithm); if (st == null) { throw new ArgumentNullException("StatusTime must be specified for this routine."); } if (String.IsNullOrEmpty(encryptedInputFile) || String.IsNullOrEmpty(outputFile)) { throw new ArgumentNullException("Paths to both the encrypted input file and the output file need to be specified."); } if (KeyIsValid(GetStringFromByteArray(_key)) == false) { throw new System.Exception("Invalid length for Key."); } if (IVIsValid(GetStringFromByteArray(_iv)) == false) { throw new System.Exception("Invalid length for IV."); } try { //string encryptedData = File.ReadAllText(encryptedInputFile); //encryptor.Key = this.Key; //encryptor.IV = this.IV; //string decryptedData = encryptor.Decrypt(encryptedData); //File.WriteAllText(outputFile, decryptedData); PFFileEncoder encoder = new PFFileEncoder(); PFTempFile tempFile = new PFTempFile(); FileInfo fi = new FileInfo(encryptedInputFile); //************************************************************************************************** //dont't setup the timer callback: EncodeFileToBase64 routine slows down by a factor of 3x slower. //encoder.currentStatusReport += FileEncoderStatusReport; //st.NumSecondsInterval = 60; //************************************************************************************************** currentStatusReport("DecodeFromBase64", "In Progress", fi.Length, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime()); //encoder.DecodeFileFromBase64(encryptedInputFile, tempFile.TempFileName); DecodeFileOnSeparateThread(encoder, encryptedInputFile, tempFile.TempFileName, st); fi = new FileInfo(tempFile.TempFileName); currentStatusReport("DecodeFromBase64", "Completed", fi.Length, (int)st.GetElapsedTime().TotalSeconds, st.GetFormattedElapsedTime()); DecryptBinary(tempFile.TempFileName, outputFile, st); } catch (System.Exception ex) { _msg.Length = 0; _msg.Append("Attempt to decrypt file "); _msg.Append(encryptedInputFile); _msg.Append(" failed. Verify you are using same key/iv pair used to encrypt. Error message: "); _msg.Append("\r\n"); _msg.Append(AppMessages.FormatErrorMessage(ex)); throw new System.Exception(_msg.ToString()); } finally { ; } return(outputFile); }