public MainWindow()
        {
            InitializeComponent();
            EncryptViewModel encryptViewModel = new EncryptViewModel();
            //encryptViewModel.AllDatas = new ObservableCollection<EncryptData>();
            //AddDataAsSync addDataAsSync = new AddDataAsSync(encryptViewModel);
            //Thread thread = new Thread(addDataAsSync.AddDataToEndataList);
            //thread.Start();
            /////////////////////////////////////////////////////////////////////
            //Thread thread2 = new Thread(addDataAsSync.AddDataToDeDataList);
            //thread2.Start();

            Helper        helper        = new Helper();
            AddDataAsSync addDataAsSync = new AddDataAsSync(encryptViewModel);
            //addDataAsSync.AllEnDatas = new ObservableCollection<EncryptData>();

            var isExist = File.Exists("configEndata.json");

            if (!isExist)
            {
                for (int i = 0; i < 20; i++)
                {
                    EncryptData encryptData = new EncryptData()
                    {
                        No   = i + 1,
                        Text = "Salam - >" + i.ToString()
                    };

                    App.encryptDatas.Add(encryptData);
                }
                helper.EnDatas = new List <EncryptData>(addDataAsSync.AllEnDatas);
                helper.SeriailizeEndatasToJson();
            }
            else
            {
                //addDataAsSync.AllEnDatas = new ObservableCollection<EncryptData>(helper.DeserializeEnDatasFromJson());
                App.encryptDatas = helper.DeserializeEnDatasFromJson();
            }

            DataContext = encryptViewModel;
        }
Esempio n. 2
0
 public DePlayCommand(EncryptViewModel encryptViewModel)
 {
     EncryptViewModel = encryptViewModel;
 }
Esempio n. 3
0
 public DePauseCommand(EncryptViewModel encryptViewModel)
 {
     EncryptViewModel = encryptViewModel;
 }
 public DeResumeCommand(EncryptViewModel encryptViewModel)
 {
     EncryptViewModel = encryptViewModel;
 }
 public AddDataAsSync(EncryptViewModel encryptViewModel)
 {
     this.encryptViewModel = encryptViewModel;
     AllEnDatas            = App.encryptDatas;
 }
Esempio n. 6
0
        public ActionResult Index(EncryptViewModel encryption)
        {
            if (ModelState.IsValid)
            {
                // Paths for each file that we are going to store on disk
                string mainDirectory         = Server.MapPath("~/App_Data/Encrypt/");
                string certificatesDirectory = Server.MapPath("~/App_Data/Certificates/");
                string subDir             = DateTime.UtcNow.ToString("yyyyMMddTHHmmssfff") + "\\";
                string encryptedDirectory = Path.Combine(mainDirectory, subDir);

                string xmlReportPath           = Path.Combine(encryptedDirectory, encryption.Report.FileName);
                string senderCertificatePath   = Path.Combine(certificatesDirectory, "sender.pfx");
                string receiverCertificatePath = Path.Combine(certificatesDirectory, "receiver.cer");
                string hctaCertificatePath     = ""; // Optional

                // Create subdirectory
                Directory.CreateDirectory(encryptedDirectory);

                // Save xml report to the disk
                encryption.Report.SaveAs(xmlReportPath);

                // Read certificates
                byte[] senderCertificate;
                using (Stream inputStream = encryption.SenderCertificate.InputStream)
                {
                    MemoryStream memoryStream = inputStream as MemoryStream;
                    if (memoryStream == null)
                    {
                        memoryStream = new MemoryStream();
                        inputStream.CopyTo(memoryStream);
                    }
                    senderCertificate = memoryStream.ToArray();
                }

                byte[] irsCertificate;
                using (Stream inputStream = encryption.IRSCertificate.InputStream)
                {
                    MemoryStream memoryStream = inputStream as MemoryStream;
                    if (memoryStream == null)
                    {
                        memoryStream = new MemoryStream();
                        inputStream.CopyTo(memoryStream);
                    }
                    irsCertificate = memoryStream.ToArray();
                }

                byte[] hctaCertificate = { };
                if (encryption.Model1Option2)
                {
                    using (Stream inputStream = encryption.HCTACertificate.InputStream)
                    {
                        MemoryStream memoryStream = inputStream as MemoryStream;
                        if (memoryStream == null)
                        {
                            memoryStream = new MemoryStream();
                            inputStream.CopyTo(memoryStream);
                        }
                        hctaCertificate = memoryStream.ToArray();
                    }
                }

                // Perform the schema validation
                //string validationError = XmlManager.CheckSchema(xmlReportPath, "Schemas folder");

                //if (validationError != "")
                //{
                //   return Json(new { StatusCode = HttpStatusCode.InternalServerError, errorMessage = "Schema validation error: " + validationError });
                //}

                // Load XML report content
                byte[] xmlContent    = System.IO.File.ReadAllBytes(xmlReportPath);
                string senderGIIN    = Path.GetFileNameWithoutExtension(encryption.Report.FileName);
                string fileExtension = Path.GetExtension(encryption.Report.FileName.ToUpper()).Replace(".", "");

                // Perform signature
                byte[] signedReport     = XmlManager.Sign(XmlSignatureType.Enveloping, xmlContent, senderCertificate, encryption.SenderCertPassword, xmlReportPath);
                string signedReportPath = xmlReportPath.Replace(".xml", "_Payload.xml");

                string zipPackagePath = signedReportPath.Replace(".xml", ".zip");

                // Save signed xml report to the disk
                System.IO.File.WriteAllBytes(signedReportPath, signedReport);

                // Create the zip archive that will hold the generated files, and add the signed report to it
                ZipManager.CreateArchive(signedReportPath, zipPackagePath);

                // Generate AES key (32 bytes) & default initialization vector (empty)
                byte[] aesEncryptionKey    = AesManager.GenerateRandomKey(AesManager.KeySize / 8);
                byte[] aesEncryptionVector = AesManager.GenerateRandomKey(16, false);

                // Encrypt file and save to disk
                string encryptedFilePath     = zipPackagePath.Replace(".zip", "");
                string encryptedHCTAFilePath = zipPackagePath.Replace(".zip", "");
                string payloadFilePath       = encryptedFilePath + "";
                AesManager.EncryptFile(zipPackagePath, encryptedFilePath, aesEncryptionKey, aesEncryptionVector, false);

                // Encrypt AES key with the public key of the certificate and save to disk
                encryptedFilePath = Path.GetDirectoryName(zipPackagePath) + "\\000000.00000.TA.840_Key";
                AesManager.EncryptAesKey(aesEncryptionKey, aesEncryptionVector, irsCertificate, "yourpasswordhere", encryptedFilePath, false);

                //For Model1 Option2 Only, encrypt the AES Key with the HCTA Public Key
                if (encryption.Model1Option2)
                {
                    encryptedHCTAFilePath = Path.GetDirectoryName(zipPackagePath) + "\\000000.00000.TA." + "901" + "_Key";
                    AesManager.EncryptAesKey(aesEncryptionKey, aesEncryptionVector, hctaCertificate, "", encryptedHCTAFilePath, false);
                }

                // cleanup
                signedReport     = null;
                aesEncryptionKey = aesEncryptionVector = null;

                string packageName = "";
                try
                {
                    DateTime uDat = new DateTime();
                    uDat        = DateTime.UtcNow;
                    packageName = uDat.ToString("yyyyMMddTHHmmssfffZ") + "_" + senderGIIN;
                    string metadataFilePath = Path.Combine(encryptedDirectory, senderGIIN + "_Metadata.xml");
                    XmlManager.CreateMetadataFile(metadataFilePath, fileExtension, true, "2018", senderGIIN, packageName);

                    // Check the signature to make sure it is valid, this requires the KeyInfo to be present
                    bool result = XmlManager.CheckSignature(signedReportPath);
                    if (result == false)
                    {
                        return(Json(new { statusCode = HttpStatusCode.InternalServerError, errorMessage = "The document signature is not valid!" }));
                    }

                    //Add the metadata, payload, and key files to the final zip package
                    // Add signed file to the zip package
                    ZipManager.CreateArchive(metadataFilePath, encryptedDirectory + "\\" + packageName + ".zip");
                    ZipManager.UpdateArchive(encryptedFilePath, encryptedDirectory + "\\" + packageName + ".zip");
                    ZipManager.UpdateArchive(payloadFilePath, encryptedDirectory + "\\" + packageName + ".zip");

                    //Add the HCTA Key file for a Model 1 Option 2 packet
                    if (encryption.Model1Option2)
                    {
                        ZipManager.UpdateArchive(encryptedHCTAFilePath, encryptedDirectory + "\\" + packageName + ".zip");
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }

                encryption.PackagePath = encryptedDirectory + "\\" + packageName + ".zip";

                return(RedirectToAction("Download", new { packagePath = encryptedDirectory + "\\" + packageName + ".zip" }));
            }
            else
            {
                return(View(encryption));
            }
        }