/** * Creates PDFDocuments to create shares, encrypt the original PDF and draws texts on the PDF shares * */ public void createShares() { List <PDFDocument> pdfShares = new List <PDFDocument>(); Dictionary <int, string> coefShares = new Dictionary <int, string>(); for (int i = 0; i < _numOfShares; i++) { pdfShares.Add(new PDFDocument()); pdfShares[i].DisplayMode = _plainPDF.DisplayMode; pdfShares[i].DocumentInformation = _plainPDF.DocumentInformation; pdfShares[i].PageLayout = _plainPDF.PageLayout; pdfShares[i].PageOrientation = _plainPDF.PageOrientation; pdfShares[i].PortfolioInformation = _plainPDF.PortfolioInformation; pdfShares[i].PageLayout = _plainPDF.PageLayout; pdfShares[i].PageSize = _plainPDF.PageSize; pdfShares[i].PageWidth = _plainPDF.PageWidth; pdfShares[i].PageHeight = _plainPDF.PageHeight; } foreach (KeyValuePair <Object, Object> entry in _words) { if (entry.Key.GetType() != typeof(string)) { List <Words> Words = (List <Words>)entry.Value; int pageIndex = (int)entry.Key; //add next page for future processing for (int l = 0; l < pdfShares.Count; l++) { pdfShares[l].AddPage(); pdfShares[l].Pages[pageIndex].Document.DocumentInformation.Keywords = ""; } for (int i = 0; i < Words.Count; i++) { Words word = Words[i]; string secret = word.Word; int[] coefficients = new int[_shareforRec - 1]; // string str = ""; if (_shareforRec == 2) { coefficients[0] = _coefficients[0]; } else { //Generate the random values and make sure our coefficients are less than the modulus value for (int index = 0; index < coefficients.Length; index++) { int coefIndex = _rand.Next(1, _coefficients.Count) - 1; coefficients[index] = _coefficients[coefIndex]; } } Share[] textShares = ShareGenerator.GenerateShares(secret, _numOfShares, _shareforRec, coefficients, _avgFrequencyMap); for (int l = 0; l < textShares.Length; l++) { pdfShares[l].Pages[pageIndex].Canvas.DrawText(textShares[l].GetCipherText(), word.FontBase, word.FontBrush, word.Left, word.Top); if (secret == "because") { //pdfShares[l].Pages[pageIndex].Canvas.DrawText(textShares[i].ToString().Length, word.FontBase, word.FontBrush, word.Left, word.Top); } } } } } for (int i = 0; i < _coefficients.Count; i++) { int[] coefficientShares = new CoefficientCodec(_coefficients[i], _numOfShares, _shareforRec, "Encrypt").shares; for (int shareIndex = 0; shareIndex < coefficientShares.Length; shareIndex++) { if (!coefShares.ContainsKey(shareIndex)) { coefShares.Add(shareIndex, coefficientShares[shareIndex].ToString() + ","); } else { coefShares[shareIndex] += coefficientShares[shareIndex].ToString() + ","; } } } for (int i = 0; i < pdfShares.Count; i++) { pdfShares[i].DocumentInformation.Keywords = coefShares[i]; pdfShares[i].Save(_encryptedFilePath + (i + 1) + ".pdf"); pdfShares[i].Dispose(); } }
/** * 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."); } } }