Ejemplo n.º 1
0
        //encryption Eoutines

        public string Encrypt(pfEncryptorRequest encryptorRequest, ref bool encryptionSuccessful)
        {
            string result = string.Empty;

            try
            {
                if (encryptorRequest.SourceObjectType == pfEncryptorObjectType.File)
                {
                    result = EncryptFile(encryptorRequest, ref encryptionSuccessful);
                }
                else if (encryptorRequest.SourceObjectType == pfEncryptorObjectType.String)
                {
                    result = EncryptString(encryptorRequest, ref encryptionSuccessful);
                }
                else
                {
                    _msg.Length = 0;
                    _msg.Append("Unexpected or invalid encryptor object type: ");
                    _msg.Append(encryptorRequest.SourceObjectType.ToString());
                    throw new System.Exception(_msg.ToString());
                }
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                ;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public bool LoadEncryptionRequestFromFile(string filePath, ref pfEncryptorRequest er)
        {
            bool loadSuccessful = false;

            try
            {
                er = pfEncryptorRequest.LoadFromXmlFile(filePath);
                DecryptKeyAndIV(ref er);

                loadSuccessful = true;
            }
            catch (System.Exception ex)
            {
                loadSuccessful = false;
                _msg.Length    = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                ;
            }

            return(loadSuccessful);
        }
Ejemplo n.º 3
0
        public string EncryptString(pfEncryptorRequest encryptorRequest, ref bool encryptionSuccessful)
        {
            PFStringEncryptor encryptor     = null;
            string            encryptedText = string.Empty;

            try
            {
                if (String.IsNullOrEmpty(encryptorRequest.SourceObject))
                {
                    _msg.Length = 0;
                    _msg.Append("You must specify a string to encrypt.");
                    throw new System.Exception(_msg.ToString());
                }

                if (string.IsNullOrEmpty(encryptorRequest.EncryptionKey) ||
                    string.IsNullOrEmpty(encryptorRequest.EncryptionIV))
                {
                    _msg.Length = 0;
                    _msg.Append("You must specify both Key and IV values.");
                    throw new System.Exception(_msg.ToString());
                }


                encryptor = new PFStringEncryptor(encryptorRequest.EncryptionAlgorithm);

                encryptor.Key = encryptorRequest.EncryptionKey;
                encryptor.IV  = encryptorRequest.EncryptionIV;

                if (encryptorRequest.DestinationObjectType == pfEncryptorObjectType.String)
                {
                    encryptedText = encryptor.Encrypt(encryptorRequest.SourceObject);
                    encryptorRequest.DestinationObject = encryptedText;
                }
                else
                {
                    encryptedText = encryptor.Encrypt(encryptorRequest.SourceObject, encryptorRequest.DestinationObject);
                }

                encryptionSuccessful = true;
            }
            catch (System.Exception ex)
            {
                encryptionSuccessful = false;
                _msg.Length          = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                ;
            }



            return(encryptedText);
        }
Ejemplo n.º 4
0
        private void DecryptKeyAndIV(ref pfEncryptorRequest er)
        {
            pfEncryptorRequest enc      = new pfEncryptorRequest();
            string             newValue = string.Empty;
            bool decryptionSuccessful   = false;

            //encrypt the key value
            enc.EncryptionAlgorithm   = pfEncryptionAlgorithm.AES;
            enc.OperationType         = pfEncryptorOperationType.Encryption;
            enc.SourceObjectType      = pfEncryptorObjectType.String;
            enc.DestinationObjectType = pfEncryptorObjectType.String;
            enc.EncryptionKey         = _zenkPath.Substring(17, 16);
            enc.EncryptionIV          = _zenkPath.Substring(49, 16);
            enc.SourceObject          = er.EncryptionKey;
            enc.DestinationObject     = string.Empty;
            enc.UseBinaryEncryption   = false;
            newValue = DecryptString(enc, ref decryptionSuccessful);
            if (decryptionSuccessful)
            {
                er.EncryptionKey = newValue;
            }
            else
            {
                DecryptKeysError();
            }

            //encrypt the IV value
            enc.SourceObject      = er.EncryptionIV;
            enc.DestinationObject = string.Empty;
            newValue = DecryptString(enc, ref decryptionSuccessful);
            if (decryptionSuccessful)
            {
                er.EncryptionIV = newValue;
            }
            else
            {
                DecryptKeysError();
            }
        }
Ejemplo n.º 5
0
        public bool SaveEncryptionRequestToFile(pfEncryptorRequest er, string outputFilePath)
        {
            bool saveSuccessful = false;

            try
            {
                EncryptKeyAndIV(ref er);
                er.SaveToXmlFile(outputFilePath);
                saveSuccessful = true;
            }
            catch (System.Exception ex)
            {
                saveSuccessful = false;
                _msg.Length    = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                ;
            }

            return(saveSuccessful);
        }
Ejemplo n.º 6
0
        public string DecryptFile(pfEncryptorRequest decryptorRequest, ref bool decryptionSuccessful)
        {
            PFFileEncryptor decryptor       = null;
            string          decryptedResult = string.Empty;
            StatusTimer     st = new StatusTimer();

            try
            {
                if (String.IsNullOrEmpty(decryptorRequest.SourceObject))
                {
                    _msg.Length = 0;
                    _msg.Append("You must specify path to a file to decrypt.");
                    throw new System.Exception(_msg.ToString());
                }

                if (String.IsNullOrEmpty(decryptorRequest.DestinationObject))
                {
                    decryptorRequest.DestinationObject = decryptorRequest.SourceObject + ".decrypted";
                }

                if (string.IsNullOrEmpty(decryptorRequest.EncryptionKey) ||
                    string.IsNullOrEmpty(decryptorRequest.EncryptionIV))
                {
                    _msg.Length = 0;
                    _msg.Append("You must specify both Key and IV values.");
                    throw new System.Exception(_msg.ToString());
                }

                if (decryptorRequest.SourceObject == decryptorRequest.DestinationObject)
                {
                    _msg.Length = 0;
                    _msg.Append("Source and destination file paths are the same. They must be different for decryption routine to work.");
                    throw new System.Exception(_msg.ToString());
                }


                decryptor = new PFFileEncryptor(decryptorRequest.EncryptionAlgorithm);
                decryptor.currentStatusReport        += UpdateStatus;
                decryptor.StatusReportIntervalSeconds = _statusReportIntervalSeconds;

                decryptor.Key = decryptorRequest.EncryptionKey;
                decryptor.IV  = decryptorRequest.EncryptionIV;

                st.ShowElapsedTimeMilliseconds = false;
                st.Start();

                if (decryptorRequest.UseBinaryEncryption == false)
                {
                    if (decryptorRequest.DestinationObjectType == pfEncryptorObjectType.File)
                    {
                        decryptedResult = decryptor.Decrypt(decryptorRequest.SourceObject, decryptorRequest.DestinationObject, st);
                    }
                    else
                    {
                        decryptedResult = decryptor.Decrypt(decryptorRequest.SourceObject);
                    }
                }
                else
                {
                    decryptedResult = decryptor.DecryptBinary(decryptorRequest.SourceObject, decryptorRequest.DestinationObject, st);
                }

                decryptorRequest.DestinationObject = decryptedResult;

                decryptionSuccessful = true;
            }
            catch (System.Exception ex)
            {
                decryptionSuccessful = false;
                _msg.Length          = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                if (decryptor != null)
                {
                    decryptor.currentStatusReport -= UpdateStatus;
                    decryptor = null;
                }
                if (st != null)
                {
                    if (st.StatusTimerIsRunning)
                    {
                        st.Stop();
                    }
                    st = null;
                }
            }


            return(decryptedResult);
        }