private void btnDecryptZip_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtNotificationZip.Text) || string.IsNullOrWhiteSpace(txtReceiverCert.Text))
            {
                // files validation
                MessageBox.Show("Either the ZIP file or certificate was not specified!", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string zipFolder = "";

            try
            {
                //Deflate the zip archive
                zipFolder = ZipManager.ExtractArchive(txtNotificationZip.Text, txtNotificationFolder.Text);
            }
            catch (Exception ex)
            {
                ex.DisplayException(Text);
                return;
            }
            // select encrypted key file
            string encryptedKeyFile     = "";
            string encryptedPayloadFile = "";

            string[] keyFiles     = Directory.GetFiles(zipFolder, "*_Key", SearchOption.TopDirectoryOnly);
            string[] payloadFiles = Directory.GetFiles(zipFolder, "*_Payload", SearchOption.TopDirectoryOnly);

            if (keyFiles.Length == 0)
            {
                // key file validation
                MessageBox.Show("There was no file found containing the encrypted AES key!", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (payloadFiles.Length == 0)
            {
                // key file validation
                MessageBox.Show("There was no file found containing the encrypted Payload!", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            encryptedKeyFile     = keyFiles[0];
            encryptedPayloadFile = payloadFiles[0];



            byte[] encryptedAesKey = null;
            byte[] decryptedAesKey = null;
            byte[] aesVector       = null;

            try
            {
                // load encrypted AES key
                encryptedAesKey = File.ReadAllBytes(encryptedKeyFile);

                // decrypt AES key & generate default (empty) initialization vector
                decryptedAesKey = AesManager.DecryptAesKey(encryptedAesKey, txtReceiverCert.Text, txtRecKeyPassword.Text);
                aesVector       = AesManager.GenerateRandomKey(16, true);
                if (radECB.Checked != true)
                {
                    aesVector       = decryptedAesKey.Skip(32).Take(16).ToArray();
                    decryptedAesKey = decryptedAesKey.Take(32).ToArray();
                }

                // decrypt encrypted ZIP file using decrypted AES key
                string decryptedFileName = encryptedPayloadFile.Replace("_Payload", "_Payload_decrypted.zip");
                AesManager.DecryptFile(encryptedPayloadFile, decryptedFileName, decryptedAesKey, aesVector, radECB.Checked);


                //Deflate the decrypted zip archive
                ZipManager.ExtractArchive(decryptedFileName, decryptedFileName, false);


                // success
                MessageBox.Show("Notification decryption process is complete!", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ex.DisplayException(Text);
            }
            finally
            {
                if (encryptedAesKey != null)
                {
                    encryptedAesKey = null;
                }

                if (decryptedAesKey != null)
                {
                    decryptedAesKey = null;
                }

                if (aesVector != null)
                {
                    aesVector = null;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// This will decrypt the payload from a downloaded and decompressed notification
        /// </summary>
        /// <param name="xmlProcessingFolder">The path to folder that contains the decompressed notification files</param>
        /// <param name="decryptionKey">The key file used to decrypt the AES key</param>
        /// <param name="decryptionPass">The password to the key file above</param>
        /// <param name="isECB">Determines the cipher mode, CBC or ECB</param>
        /// <returns>the file path to the decrypted payload file</returns>
        public static string DecryptNotification(string xmlProcessingFolder, string decryptionKey, string decryptionPass, bool isECB)
        {
            // select encrypted key file
            string encryptedKeyFile     = "";
            string encryptedPayloadFile = "";
            string metadataFile         = "";

            string[] keyFiles      = Directory.GetFiles(xmlProcessingFolder, "*_Key", SearchOption.TopDirectoryOnly);
            string[] payloadFiles  = Directory.GetFiles(xmlProcessingFolder, "*_Payload", SearchOption.TopDirectoryOnly);
            string[] metadataFiles = Directory.GetFiles(xmlProcessingFolder, "*_Metadata*", SearchOption.TopDirectoryOnly);

            if (keyFiles.Length == 0)
            {
                // key file validation
                throw new Exception("There was no file found containing the encrypted AES key!");
            }
            if (payloadFiles.Length == 0)
            {
                // key file validation
                throw new Exception("There was no file found containing the encrypted Payload!");
            }
            if (metadataFiles.Length == 0)
            {
                // key file validation
                throw new Exception("There was no file found containing the Metadata!");
            }

            encryptedKeyFile     = keyFiles[0];
            encryptedPayloadFile = payloadFiles[0];
            metadataFile         = metadataFiles[0];

            //Check the metadata and see what we have
            string metadataContentType = XmlManager.CheckMetadataType(metadataFile);

            byte[] encryptedAesKey  = null;
            byte[] decryptedAesKey  = null;
            byte[] aesVector        = null;
            string decryptedPayload = "";

            // load encrypted AES key
            encryptedAesKey = File.ReadAllBytes(encryptedKeyFile);

            // decrypt AES key & generate default (empty) initialization vector
            decryptedAesKey = AesManager.DecryptAesKey(encryptedAesKey, decryptionKey, decryptionPass);
            aesVector       = AesManager.GenerateRandomKey(16, true);
            if (isECB != true)
            {
                aesVector       = decryptedAesKey.Skip(32).Take(16).ToArray();
                decryptedAesKey = decryptedAesKey.Take(32).ToArray();
            }

            // decrypt encrypted ZIP file using decrypted AES key
            string decryptedFileName = encryptedPayloadFile.Replace("_Payload", "_Payload_decrypted.zip");

            AesManager.DecryptFile(encryptedPayloadFile, decryptedFileName, decryptedAesKey, aesVector, isECB);

            //Deflate the decrypted zip archive
            ZipManager.ExtractArchive(decryptedFileName, xmlProcessingFolder, true);
            decryptedPayload = decryptedFileName.Replace("_Payload_decrypted.zip", "_Payload.xml");

            //If the metadata is something other than XML, read the wrapper and rebuild the non-XML file
            if (metadataContentType != "XML")
            {
                //Some non-XML files may not have _Payload in the file name, if not remove it
                if (!File.Exists(decryptedPayload))
                {
                    decryptedPayload = decryptedPayload.Replace("_Payload.xml", ".xml");
                }

                //This will give us the base64 encoded data from the XML file
                string encodedData = XmlManager.ExtractXMLImageData(decryptedPayload);

                //We will convert the base64 data back to bytes
                byte[] binaryData;
                string decodedPayload = decryptedPayload.Replace(".xml", "." + metadataContentType);
                binaryData = System.Convert.FromBase64String(encodedData);

                //We can write the bytes back to rebuild the file
                FileStream decodedFile;
                decodedFile = new FileStream(decodedPayload, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                decodedFile.Write(binaryData, 0, binaryData.Length);
                decodedFile.Close();
            }

            return(decryptedPayload);
        }
예제 #3
0
        private void btnDecryptZip_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtNotificationZip.Text) || string.IsNullOrWhiteSpace(txtReceiverCert.Text))
            {
                // files validation
                MessageBox.Show("Either the ZIP file or certificate was not specified!", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string zipFolder = "";

            try
            {
                //Deflate the zip archive
                zipFolder = ZipManager.ExtractArchive(txtNotificationZip.Text, txtNotificationFolder.Text);
            }
            catch (Exception ex)
            {
                ex.DisplayException(Text);
                return;
            }
            // select encrypted key file
            string encryptedKeyFile     = "";
            string encryptedPayloadFile = "";
            string metadataFile         = "";

            string[] keyFiles      = Directory.GetFiles(zipFolder, "*_Key", SearchOption.TopDirectoryOnly);
            string[] payloadFiles  = Directory.GetFiles(zipFolder, "*_Payload", SearchOption.TopDirectoryOnly);
            string[] metadataFiles = Directory.GetFiles(zipFolder, "*_Metadata*", SearchOption.TopDirectoryOnly);

            if (keyFiles.Length == 0)
            {
                // key file validation
                MessageBox.Show("There was no file found containing the encrypted AES key!", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (payloadFiles.Length == 0)
            {
                // key file validation
                MessageBox.Show("There was no file found containing the encrypted Payload!", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            encryptedKeyFile     = keyFiles[0];
            encryptedPayloadFile = payloadFiles[0];
            metadataFile         = metadataFiles[0];

            //Check the metadata and see what we have
            string metadataContentType = XmlManager.CheckMetadataType(metadataFile);


            byte[] encryptedAesKey = null;
            byte[] decryptedAesKey = null;
            byte[] aesVector       = null;

            try
            {
                // load encrypted AES key
                encryptedAesKey = File.ReadAllBytes(encryptedKeyFile);

                // decrypt AES key & generate default (empty) initialization vector
                decryptedAesKey = AesManager.DecryptAesKey(encryptedAesKey, txtReceiverCert.Text, txtRecKeyPassword.Text);
                aesVector       = AesManager.GenerateRandomKey(16, true);
                if (radECB.Checked != true)
                {
                    aesVector       = decryptedAesKey.Skip(32).Take(16).ToArray();
                    decryptedAesKey = decryptedAesKey.Take(32).ToArray();
                }

                // decrypt encrypted ZIP file using decrypted AES key
                string decryptedFileName = encryptedPayloadFile.Replace("_Payload", "_Payload_decrypted.zip");
                AesManager.DecryptFile(encryptedPayloadFile, decryptedFileName, decryptedAesKey, aesVector, radECB.Checked);


                //Deflate the decrypted zip archive
                ZipManager.ExtractArchive(decryptedFileName, decryptedFileName, false);
                string decryptedPayload = decryptedFileName.Replace("_Payload_decrypted.zip", "_Payload.xml");
                //If the metadata is something other than XML, read the wrapper and rebuild the non-XML file

                if (metadataContentType != "XML")
                {
                    //Some non-XML files may not have _Payload in the file name, if not remove it
                    if (!File.Exists(decryptedPayload))
                    {
                        decryptedPayload = decryptedPayload.Replace("_Payload.xml", ".xml");
                    }

                    //This will give us the base64 encoded data from the XML file

                    string encodedData = XmlManager.ExtractXMLImageData(decryptedPayload);

                    //We will convert the base64 data back to bytes
                    byte[] binaryData;
                    string decodedPayload = decryptedPayload.Replace(".xml", "." + metadataContentType);
                    binaryData = System.Convert.FromBase64String(encodedData);

                    //We can write the bytes back to rebuild the file
                    FileStream decodedFile;
                    decodedFile = new FileStream(decodedPayload, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                    decodedFile.Write(binaryData, 0, binaryData.Length);
                    decodedFile.Close();
                }

                // success
                MessageBox.Show("Notification decryption process is complete!", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ex.DisplayException(Text);
            }
            finally
            {
                if (encryptedAesKey != null)
                {
                    encryptedAesKey = null;
                }

                if (decryptedAesKey != null)
                {
                    decryptedAesKey = null;
                }

                if (aesVector != null)
                {
                    aesVector = null;
                }
            }
        }