/** * Reads input file to get PDFObjects from the original PDF * */ public Dictionary <object, object> ReadinputFile(PDFDocument pdfDoc) { PDFImportedPage ip; Dictionary <Object, Object> PlainDictionary = new Dictionary <Object, Object>(); PDFObjectsProcessor objectsProcessor = new PDFObjectsProcessor(); for (int PageIndex = 0; PageIndex < pdfDoc.Pages.Count; PageIndex++) { ip = (PDFImportedPage)pdfDoc.Pages[PageIndex]; PDFPageObjectCollection Objects = ip.ExtractPageObjects(); //SHA256 mySHA256 = SHA256Managed.Create(); //string content = ip.ExtractText(); //byte[] hashedContentKey = mySHA256.ComputeHash(GenerateStreamFromString(content)); List <Words> WordObjects = new List <Words>(); List <Blocks> BlockList = new List <Blocks>(); for (int j = 0; j < Objects.Count; j++) { if (Objects[j] is PDFTextPageObject) { PDFTextPageObject TextObject = (PDFTextPageObject)Objects[j]; WordObjects = objectsProcessor.GetWords(TextObject, WordObjects); //BlockList = objectsProcessor.GetBlocks(TextObject, BlockList); } else if (Objects[j] is PDFImagePageObject) { _imageObjects.Add((PDFImagePageObject)Objects[j]); } else if (Objects[j] is PDFPathPageObject) { _pathObjects.Add((PDFPathPageObject)Objects[j]); } } //PlainDictionary.Add(PageIndex, BlockList); PlainDictionary.Add(PageIndex, WordObjects); } pdfDoc.Dispose(); return(PlainDictionary); }
public static List <PDFDocument> getPdf(PDFDocument pdf, String xyz) { Share[] textShares; System.Drawing.Bitmap[] imageShares = new System.Drawing.Bitmap[5]; //PdfFont font = secretPdf.AddFont("Arial"); System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); //timer.Start(); ////lOOP THROUGH EACH PAGE IN THE SECRET PDF //for (int i = 0; i < secretPdf.PageCount; i++ ) //{ // //Text Extraction // for (int j = 0; j < secretPdf.Pages[i].Canvas.GetTextData().Count; j++ ) // { // //int test = secretPdf.Pages[i].Canvas.GetTextData().Count; // PdfTextData secretData = secretPdf.Pages[i].GetWords()[j]; //our pdf text chunk to process // textShares = ShareGenerator.GenerateShares(secretData.Text, n, k); // ////Write the share data to the pdf shares // //for (int l = 0; l < sharePdfs.Count; l++) // //{ // //} // System.Console.WriteLine("Chunk Complete"); // } // //add next page for future processing // for (int l = 0; l < sharePdfs.Count; l++) // sharePdfs[l].AddPage(); // System.Console.WriteLine("Page Complete"); //} //timer.Stop(); //System.Console.WriteLine("\n\nTime Elapsed: " + timer.ElapsedMilliseconds+"\n\n\n"); //timer.Reset(); //secretPdf.Dispose(); PDFDocument pdfDoc = pdf; //new PDFDocument("pdfimage.pdf"); //PDFDocument secondDoc = new PDFDocument("white.pdf"); PDFDocument recon = new PDFDocument(); List <int> usedShares = new List <int>(); usedShares.Add(2); usedShares.Add(3); usedShares.Add(4); List <PDFDocument> pdfShares = new List <PDFDocument>(); for (int i = 0; i < 2; i++) { pdfShares.Add(new PDFDocument()); } PDFImportedPage ip; O2S.Components.PDF4NET.Text.PDFTextRunCollection textRuns; timer.Start(); for (int i = 0; i < pdfDoc.Pages.Count; i++) { ip = (PDFImportedPage)pdfDoc.Pages[i]; textRuns = ip.ExtractTextRuns(); PDFPageObjectCollection objects = ip.ExtractPageObjects(); //add next page for future processing for (int l = 0; l < pdfShares.Count; l++) { pdfShares[l].AddPage(); } recon.AddPage(); for (int j = 0; j < textRuns.Count; j++) { textShares = ShareGenerator.GenerateShares(textRuns[j].Text, 5, 3); PDFFont font = new PDFFont(PDFFontFace.Helvetica, textRuns[j].FontSize); PDFBrush fontBrush = new PDFBrush(textRuns[j].FillColor); //PDFPen pen = new PDFPen(textRuns[j].StrokeColor); string[] shareString = new string[5]; for (int l = 0; l < 2; l++) { shareString[l] = textShares[l].GetCipherText(); pdfShares[l].Pages[i].Canvas.DrawText(shareString[l], font, fontBrush, textRuns[j].DisplayBounds.Left, textRuns[j].DisplayBounds.Top); //pdfShares[l].Pages[i].Canvas.DrawTextBox(textShares[l].GetCipherText(), font, pen, fontBrush, textRuns[j].DisplayBounds.Left, textRuns[j].DisplayBounds.Top, textRuns[j].DisplayBounds.Width, textRuns[j].DisplayBounds.Height); } //System.Console.WriteLine("Chunk Complete"); string reconString = ShareAssembler.TextReconstruction(shareString, usedShares, 5, 3); //System.Console.WriteLine(reconString); recon.Pages[i].Canvas.DrawText(reconString, font, fontBrush, textRuns[j].DisplayBounds.Left, textRuns[j].DisplayBounds.Top); } for (int j = 0; j < objects.Count; j++) { PDFTextPageObject text; PDFImagePageObject image; PDFFont font; PDFBrush fontBrush; if (objects[j] is PDFTextPageObject) { text = (PDFTextPageObject)objects[j]; Console.WriteLine(text.FontName); Console.WriteLine("textpdf"); font = new PDFFont(PDFFontFace.Helvetica, text.FontSize); fontBrush = new PDFBrush(text.FillColor); textShares = ShareGenerator.GenerateShares(text.Text, 2, 2); string[] shareString = new string[2]; for (int l = 0; l < textShares.Length; l++) { shareString[l] = textShares[l].GetCipherText(); pdfShares[l].Pages[i].Canvas.DrawText(shareString[l], font, fontBrush, text.DisplayBounds.Left, text.DisplayBounds.Top); } string reconString = ShareAssembler.TextReconstruction(shareString, usedShares, 5, 3); recon.Pages[i].Canvas.DrawText(reconString, font, fontBrush, text.DisplayBounds.Left, text.DisplayBounds.Top); } else if (objects[j] is PDFImagePageObject) { Console.WriteLine("imagepdf"); image = (PDFImagePageObject)objects[j]; /* * * following line is what needs to be revised until end * * * * * * * */ //imageShares = ShareGenerator.GenerateReducedShares(image.Image, 5, 3, 15); //imageShares = ShareGenerator.GenerateShares(image.Image, 5, 3); ShareGenerator genShares = new ShareGenerator(); imageShares = genShares.GenShares(image.Image, 2, 2); for (int l = 0; l < imageShares.Length; l++) { pdfShares[l].Pages[i].Canvas.DrawImage(imageShares[l], image.DisplayBounds.Left, image.DisplayBounds.Top, image.DisplayBounds.Width, image.DisplayBounds.Height); } shares = pdfShares; ShareAssembler assembler = new ShareAssembler(); System.Drawing.Bitmap reconImage = assembler.ImageConstruction(imageShares, 2, 2); recon.Pages[i].Canvas.DrawImage(reconImage, image.DisplayBounds.Left, image.DisplayBounds.Top, image.DisplayBounds.Width, image.DisplayBounds.Height); } else { } } System.Console.WriteLine("Page Complete"); //return shares; } timer.Stop(); System.Console.WriteLine("\n\nTime Elapsed: " + timer.ElapsedMilliseconds + "\n\n\n"); timer.Reset(); for (int i = 0; i < pdfShares.Count; i++) { pdfShares[i].Save("Share" + (i + 1) + ".pdf"); pdfShares[i].Dispose(); } recon.Save(@"D:\Recon123-" + xyz + ".pdf"); recon.Dispose(); return(shares); }
/** * Iterates through encrypted documents and encrypted keywords to check if provided keyword exists or not * * */ public void SearchKeyWord() { List <PDFDocument> documentsList; foreach (KeyValuePair <string, List <PDFDocument> > document in _encryptedDocuments) { Stopwatch watch = new Stopwatch(); watch.Start(); documentsList = document.Value; List <string[]> coefficientList = RetrieveCoefficients(documentsList); List <int> decryptedCoef = new CoefficientCodec(0, _numOfShares, _shareforRec, "Decrypt", coefficientList).coefficients; List <int> CoefNeededToSearch = PerformCombinationOfCoef(decryptedCoef, _shareforRec - 1); PDFDocument docShare = documentsList[0]; bool keywordFound = false; Console.WriteLine("CoefNeededToSearch : " + CoefNeededToSearch.Count); for (int coefIndex = 0; coefIndex < CoefNeededToSearch.Count; coefIndex++) { Share[] share = ShareGenerator.GenerateShares(_keyword, _numOfShares, _shareforRec, new int[] { CoefNeededToSearch[coefIndex] }, new SortedDictionary <int, int>()); for (int pageIndex = 0; pageIndex < docShare.Pages.Count; pageIndex++) { PDFPage page = docShare.Pages[pageIndex]; string text = ((PDFImportedPage)page).ExtractText(); PDFImportedPage ip = (PDFImportedPage)docShare.Pages[pageIndex]; PDFPageObjectCollection Objects = ip.ExtractPageObjects(); for (int j = 0; j < Objects.Count; j++) { if (Objects[j] is PDFTextPageObject) { PDFTextPageObject TextObject = (PDFTextPageObject)Objects[j]; if (TextObject.Text == share[0].GetCipherText()) { keywordFound = true; } } } } } if (keywordFound) { watch.Stop(); Console.WriteLine("Search Time : " + watch.ElapsedMilliseconds); watch.Reset(); watch.Start(); ShareAssemblerController shareAssembler = new ShareAssemblerController(_numOfShares, _shareforRec, _keyword); shareAssembler.SharesReconstructor(); keywordFound = true; watch.Stop(); Console.WriteLine("Decrypt and Highlight Time : " + watch.ElapsedMilliseconds); } else { Console.WriteLine("NO FILES CONTAIN THE KEYWORD YOU'RE LOOKING FOR."); } } }
/** * Recontructs the encrypted shares of the PDF to get original PDF. * Hightlights every occurrences of the word that a user has searched for. * */ public void SharesReconstructor() { List <List <PDFPageObjectCollection> > pDFPageObjects = ExtractObjects(); List <PDFPageObjectCollection> ObjectsPerPage; PDFBrush redBrush = new PDFBrush(new PDFRgbColor(204, 255, 51)); PDFPen pen = new PDFPen(new PDFRgbColor(204, 255, 51), 1); string[] shares; for (int pageIndex = 0; pageIndex < pDFPageObjects.Count; pageIndex++) { _decryptedPDF.AddPage(); ObjectsPerPage = pDFPageObjects[pageIndex]; for (int wordIndex = 0; wordIndex < ObjectsPerPage[0].Count; wordIndex++) { shares = new string[_shareforRec]; PDFTextPageObject TextObject = null; FontsProcessor fontsProcessor = null; bool shouldProcess = false; for (int shareIndex = 0; shareIndex < _shareforRec; shareIndex++) { PDFPageObjectCollection collection = ObjectsPerPage[shareIndex]; if (collection[wordIndex] is PDFTextPageObject) { TextObject = (PDFTextPageObject)collection[wordIndex]; if (TextObject.Text == "PDF4NET evaluation version 5.0.1.0") { shouldProcess = false; break; } shares[shareIndex] = TextObject.Text; fontsProcessor = new FontsProcessor(); shouldProcess = true; } } if (shouldProcess) { string decryptedText = ShareAssembler.TextReconstruction(shares, _shareNumbers, _numOfShares, _shareforRec); PDFBrush brush = new PDFBrush(TextObject.FillColor); PDFFontBase FontBase = fontsProcessor.GetFontBase(TextObject.FontName, TextObject.FontSize); if (decryptedText.ToLower().Equals(_keyword.ToLower())) { _decryptedPDF.Pages[pageIndex].Canvas.DrawRectangle(pen, redBrush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top, 4 * decryptedText.Length, FontBase.Size, 0); for (int i = 0; i < _encryptedPdf.Count; i++) { _encryptedPdf[i].Pages[pageIndex].Canvas.DrawRectangle(pen, redBrush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top, 4 * decryptedText.Length, FontBase.Size, 0); _encryptedPdf[i].Pages[pageIndex].Canvas.DrawText(shares[i > shares.Length - 1 ? 0 : i], FontBase, brush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top); } } _decryptedPDF.Pages[pageIndex].Canvas.DrawText(decryptedText, FontBase, brush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top); } } } for (int i = 0; i < _encryptedPdf.Count; i++) { _encryptedPdf[i].Save(_encryptedFilePath + (i + 1) + ".pdf"); } _decryptedPDF.Save(_decryptedFilePath); }