コード例 #1
0
        private string RecreateKeyFile()
        {
            ulong uVersion = m_vRecFormat[m_cmbRecFormat.SelectedIndex].Version;

            string strHash = StrUtil.RemoveWhiteSpace(m_tbRecKeyHash.Text);

            // If the hash is empty, set it to null in order to generate one
            if (strHash.Length == 0)
            {
                strHash = null;
            }

            KfxFile kf = KfxFile.Create(uVersion, m_tbRecKey.Text, strHash);

            // Ask for the file path after verifying the key hash
            string strFilePath = GetKeyFilePath();

            if (string.IsNullOrEmpty(strFilePath))
            {
                return(null);
            }

            IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFilePath);

            using (Stream s = IOConnection.OpenWrite(ioc))
            {
                kf.Save(s);
            }

            return(strFilePath);
        }
コード例 #2
0
        private void OnClickKeyFileBrowse(object sender, EventArgs e)
        {
            string           strFilter = AppDefs.GetKeyFileFilter();
            OpenFileDialogEx ofd       = UIUtil.CreateOpenFileDialog(KPRes.KeyFileUseExisting,
                                                                     strFilter, 1, null, false, AppDefs.FileDialogContext.KeyFile);

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            string strFile = ofd.FileName;

            try
            {
                // Test whether we can read the file
                IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile);
                IOConnection.OpenRead(ioc).Close();

                // Check the file size?
            }
            catch (Exception ex) { MessageService.ShowWarning(ex); return; }

            if (!KfxFile.CanLoad(strFile))
            {
                if (!MessageService.AskYesNo(strFile + MessageService.NewParagraph +
                                             KPRes.KeyFileNoXml + MessageService.NewParagraph +
                                             KPRes.KeyFileUseAnywayQ, null, false))
                {
                    return;
                }
            }

            m_cmbKeyFile.Items.Add(strFile);
            m_cmbKeyFile.SelectedIndex = m_cmbKeyFile.Items.Count - 1;

            EnableUserControls();
        }
コード例 #3
0
        private void OnClickKeyFileBrowse(object sender, EventArgs e)
        {
            string strFile = FileDialogsEx.ShowKeyFileDialog(false,
                                                             KPRes.KeyFileUseExisting, null, false, m_bSecureDesktop);

            if (string.IsNullOrEmpty(strFile))
            {
                return;
            }

            try
            {
                IOConnectionInfo ioc = IOConnectionInfo.FromPath(strFile);
                if (!IOConnection.FileExists(ioc, true))
                {
                    throw new FileNotFoundException();
                }

                // Check the file size?
            }
            catch (Exception ex) { MessageService.ShowWarning(strFile, ex); return; }

            if (!KfxFile.CanLoad(strFile))
            {
                if (!MessageService.AskYesNo(strFile + MessageService.NewParagraph +
                                             KPRes.KeyFileNoXml + MessageService.NewParagraph +
                                             KPRes.KeyFileUseAnywayQ, null, false))
                {
                    return;
                }
            }

            m_cmbKeyFile.Items.Add(strFile);
            m_cmbKeyFile.SelectedIndex = m_cmbKeyFile.Items.Count - 1;

            UpdateUIState();
        }
コード例 #4
0
        private static string GenerateDoc(PwDatabase pd, bool bEmSheet, bool bKeyFile)
        {
            string strDbFile = pd.IOConnectionInfo.Path;

            KcpKeyFile kf         = (pd.MasterKey.GetUserKey(typeof(KcpKeyFile)) as KcpKeyFile);
            string     strKeyFile = ((kf != null) ? kf.Path : string.Empty);

            if (bKeyFile)
            {
                if (strKeyFile.Length == 0)
                {
                    Debug.Assert(false); return(string.Empty);
                }

                if (!KfxFile.CanLoad(strKeyFile))
                {
                    throw new FormatException(strKeyFile + MessageService.NewParagraph +
                                              KPRes.KeyFileNoXml + MessageService.NewParagraph +
                                              KPRes.KeyFilePrintReqXml + MessageService.NewParagraph +
                                              KPRes.KeyFileGenHint);
                }
            }

            string strName = UrlUtil.StripExtension(UrlUtil.GetFileName(bEmSheet ?
                                                                        strDbFile : strKeyFile));

            if (strName.Length == 0)
            {
                strName = (bEmSheet ? KPRes.Database : KPRes.KeyFile);
            }

            string strDocKind = (bEmSheet ? KPRes.EmergencySheet : KPRes.KeyFileBackup);

            bool   bRtl        = Program.Translation.Properties.RightToLeft;
            string strLogLeft  = (bRtl ? "right" : "left");
            string strLogRight = (bRtl ? "left" : "right");

            GFunc <string, string> h  = new GFunc <string, string>(StrUtil.StringToHtml);
            GFunc <string, string> ne = delegate(string str)
            {
                if (string.IsNullOrEmpty(str))
                {
                    return("&nbsp;");
                }
                return(str);
            };
            GFunc <string, string> ltrPath = delegate(string str)
            {
                return(bRtl ? StrUtil.EnsureLtrPath(str) : str);
            };

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<!DOCTYPE html>");

            sb.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\"");
            string strLang = Program.Translation.Properties.Iso6391Code;

            if (string.IsNullOrEmpty(strLang))
            {
                strLang = "en";
            }
            strLang = h(strLang);
            sb.Append(" xml:lang=\"" + strLang + "\" lang=\"" + strLang + "\"");
            if (bRtl)
            {
                sb.Append(" dir=\"rtl\"");
            }
            sb.AppendLine(">");

            sb.AppendLine("<head>");
            sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            sb.AppendLine("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />");
            sb.AppendLine("<meta http-equiv=\"expires\" content=\"0\" />");
            sb.AppendLine("<meta http-equiv=\"cache-control\" content=\"no-cache\" />");
            sb.AppendLine("<meta http-equiv=\"pragma\" content=\"no-cache\" />");

            sb.Append("<title>");
            sb.Append(h(strName + " - " + strDocKind));
            sb.AppendLine("</title>");

            sb.AppendLine("<style type=\"text/css\">");
            sb.AppendLine("/* <![CDATA[ */");

            string strFont = "\"Arial\", \"Tahoma\", \"Verdana\", sans-serif;";

            // https://sourceforge.net/p/keepass/discussion/329220/thread/f98dece5/
            if (Program.Translation.IsFor("fa"))
            {
                strFont = "\"Tahoma\", \"Arial\", \"Verdana\", sans-serif;";
            }

            sb.AppendLine("body {");
            sb.AppendLine("\tcolor: #000000;");
            sb.AppendLine("\tbackground-color: #FFFFFF;");
            sb.AppendLine("\tfont-family: " + strFont);
            sb.AppendLine("\tfont-size: 12pt;");
            sb.AppendLine("}");

            sb.AppendLine("h3, p {");
            sb.AppendLine("\tmargin: 0.5em 0em 0.5em 0em;");
            sb.AppendLine("}");

            sb.AppendLine("ol, ul {");
            sb.AppendLine("\tmargin-bottom: 0em;");
            sb.AppendLine("}");

            sb.AppendLine("h1, h2, h3 {");
            sb.AppendLine("\tfont-family: \"Verdana\", \"Arial\", \"Tahoma\", sans-serif;");
            sb.AppendLine("}");
            sb.AppendLine("h1, h2 {");
            sb.AppendLine("\tmargin: 0pt 0pt 0pt 0pt;");
            sb.AppendLine("\ttext-align: center;");
            sb.AppendLine("}");
            sb.AppendLine("h1 {");
            sb.AppendLine("\tpadding: 3pt 0pt 0pt 0pt;");
            sb.AppendLine("}");
            sb.AppendLine("h2 {");
            sb.AppendLine("\tpadding: 1pt 0pt 3pt 0pt;");
            sb.AppendLine("}");
            // sb.AppendLine("h3 {");
            // sb.AppendLine("\tpadding: 3pt 3pt 3pt 3pt;");
            // sb.AppendLine("\tcolor: #000000;");
            // sb.AppendLine("\tbackground-color: #EEEEEE;");
            // sb.AppendLine("\tbackground-image: -webkit-linear-gradient(top, #E2E2E2, #FAFAFA);");
            // sb.AppendLine("\tbackground-image: -moz-linear-gradient(top, #E2E2E2, #FAFAFA);");
            // sb.AppendLine("\tbackground-image: -ms-linear-gradient(top, #E2E2E2, #FAFAFA);");
            // sb.AppendLine("\tbackground-image: linear-gradient(to bottom, #E2E2E2, #FAFAFA);");
            // sb.AppendLine("}");

            sb.AppendLine("a {");
            sb.AppendLine("\tcolor: #0000DD;");
            sb.AppendLine("\ttext-decoration: none;");
            sb.AppendLine("}");
            sb.AppendLine("a:hover, a:active {");
            sb.AppendLine("\tcolor: #6699FF;");
            sb.AppendLine("\ttext-decoration: underline;");
            sb.AppendLine("}");

            sb.AppendLine("img {");
            sb.AppendLine("\tborder: 0px none;");
            sb.AppendLine("}");

            sb.AppendLine(".withspc > li + li {");
            sb.AppendLine("\tmargin-top: 0.5em;");
            sb.AppendLine("}");

            sb.AppendLine("table.docheader {");
            sb.AppendLine("\twidth: 100%;");
            sb.AppendLine("\tbackground-color: #EEEEEE;");
            sb.AppendLine("\tmargin: 0px 0px 0px 0px;");
            sb.AppendLine("\tpadding: 0px 0px 0px 0px;");
            sb.AppendLine("\tborder: thin solid #808080;");
            // border-collapse is incompatible with border-radius
            // sb.AppendLine("\tborder-collapse: collapse;");
            sb.AppendLine("\t-webkit-border-radius: 5px;");
            sb.AppendLine("\t-moz-border-radius: 5px;");
            sb.AppendLine("\tborder-radius: 5px;");
            sb.AppendLine("}");

            sb.AppendLine("table.docheader tr td {");
            sb.AppendLine("\tborder: 0px none;");
            sb.AppendLine("\tborder-collapse: collapse;");
            sb.AppendLine("\tpadding: 0px 15px 0px 15px;");
            sb.AppendLine("\tvertical-align: middle;");
            sb.AppendLine("}");

            sb.AppendLine("table.fillinline {");
            sb.AppendLine("\twidth: 100%;");
            sb.AppendLine("\tmargin: 0px 0px 0px 0px;");
            sb.AppendLine("\tpadding: 0px 0px 0px 0px;");
            sb.AppendLine("\tborder: thin solid #808080;");
            sb.AppendLine("\tborder-collapse: collapse;");
            sb.AppendLine("\ttable-layout: fixed;");
            sb.AppendLine("\tempty-cells: show;");
            sb.AppendLine("}");
            sb.AppendLine("table.fillinline tr td {");
            sb.AppendLine("\tpadding: 4pt 4pt 4pt 4pt;");
            sb.AppendLine("\tvertical-align: middle;");
            sb.AppendLine("\tword-break: break-all;");
            sb.AppendLine("\toverflow-wrap: break-word;");
            sb.AppendLine("\tword-wrap: break-word;");
            sb.AppendLine("}");
            sb.AppendLine("table.fillinline tr td img {");
            sb.AppendLine("\tdisplay: block;");             // Inline results in additional space at the bottom
            sb.AppendLine("\tmargin: 0px " + (bRtl ? "auto 0px 0px;" : "0px 0px auto;"));
            sb.AppendLine("}");
            // sb.AppendLine("span.fillinlinesym {");
            // sb.AppendLine("\tdisplay: inline-block;");
            // sb.AppendLine("\ttransform: scale(1.75, 1.75) translate(-0.5pt, -0.5pt);");
            // sb.AppendLine("}");

            sb.AppendLine("table.fillinline tr td pre {");
            sb.AppendLine("\tmargin: 0px 0px 0px 0px;");
            sb.AppendLine("\tpadding: 0px 0px 0px 0px;");
            sb.AppendLine("\tborder: 0px none;");
            sb.AppendLine("\tborder-collapse: collapse;");
            sb.AppendLine("\twhite-space: pre-wrap;");
            sb.AppendLine("\toverflow: auto;");
            sb.AppendLine("\toverflow-wrap: break-word;");
            sb.AppendLine("\tword-wrap: break-word;");
            sb.AppendLine("\tword-break: break-all;");
            sb.AppendLine("}");

            // sb.AppendLine("@media print {");
            // sb.AppendLine(".scronly {");
            // sb.AppendLine("\tdisplay: none;");
            // sb.AppendLine("}");
            // sb.AppendLine("}");

            sb.AppendLine("@media print {");
            sb.AppendLine(".ems_break_before {");
            sb.AppendLine("\tpage-break-before: always;");      // CSS 2
            sb.AppendLine("\tbreak-before: page;");             // CSS 3
            sb.AppendLine("}");
            sb.AppendLine("}");

            // Add the temporary content identifier
            // (always, as the sheet should be printed, not saved)
            sb.AppendLine("." + Program.TempFilesPool.TempContentTag + " {");
            sb.AppendLine("\tfont-size: 12pt;");
            sb.AppendLine("}");

            sb.AppendLine("/* ]]> */");
            sb.AppendLine("</style>");
            sb.AppendLine("</head><body>");

            ImageArchive ia = new ImageArchive();

            ia.Load(Properties.Resources.Images_App_HighRes);

            sb.AppendLine("<table class=\"docheader\"><tr>");
            sb.AppendLine("<td style=\"text-align: " + strLogLeft + ";\">");
            Debug.Assert(Properties.Resources.B16x16_KeePass != null);             // Name ref.
            sb.AppendLine("<img src=\"" + GfxUtil.ImageToDataUri(ia.GetForObject(
                                                                     "KeePass")) + "\" width=\"48\" height=\"48\" alt=\"" +
                          h(PwDefs.ShortProductName) + "\" /></td>");
            sb.AppendLine("<td style=\"text-align: center;\">");
            sb.AppendLine("<h1>" + h(PwDefs.ShortProductName) + "</h1>");
            sb.AppendLine("<h2>" + h(strDocKind) + "</h2>");
            sb.AppendLine("</td>");
            sb.AppendLine("<td style=\"text-align: " + strLogRight + ";\">");
            Debug.Assert(Properties.Resources.B16x16_KOrganizer != null);             // Name ref.
            sb.AppendLine("<img src=\"" + GfxUtil.ImageToDataUri(ia.GetForObject(
                                                                     "KOrganizer")) + "\" width=\"48\" height=\"48\" alt=\"" +
                          h(strDocKind) + "\" /></td>");
            sb.AppendLine("</tr></table>");

            sb.AppendLine("<p style=\"text-align: " + strLogRight + ";\">" +
                          h(TimeUtil.ToDisplayStringDateOnly(DateTime.Now)) + "</p>");

            const string strFillInit    = "<table class=\"fillinline\"><tr><td>";
            const string strFillInitLtr = "<table class=\"fillinline\"><tr><td dir=\"ltr\">";
            const string strFillEnd     = "</td></tr></table>";
            string       strFillSym     = "<img src=\"" + GfxUtil.ImageToDataUri(
                Properties.Resources.B48x35_WritingHand) +
                                          "\" style=\"width: 1.3714em; height: 1em;" +
                                          (bRtl ? " transform: scaleX(-1);" : string.Empty) +
                                          "\" alt=\"&#x270D;\" />";
            string strFill = strFillInit + ne(string.Empty) +
                             // "</td><td style=\"text-align: right;\"><span class=\"fillinlinesym\">&#x270D;</span>" +
                             // "</td><td style=\"text-align: " + strLogRight + ";\">" +
                             "</td><td>" +
                             strFillSym + strFillEnd;

            string strFillInitEx = (bRtl ? strFillInitLtr : strFillInit);

            if (bEmSheet)
            {
                GenerateEms(sb, pd, strDbFile,
                            h, ne, ltrPath, strFillInit, strFillInitEx, strFillEnd, strFill);
            }
            if (bEmSheet && bKeyFile)
            {
                sb.AppendLine("<table class=\"docheader ems_break_before\"><tr>");
                sb.AppendLine("<td style=\"text-align: center;\">");
                sb.AppendLine("<h2>" + h(KPRes.KeyFileBackup) + "</h2>");
                sb.AppendLine("</td></tr></table><br />");
            }
            if (bKeyFile)
            {
                GenerateKfb(sb, pd, strDbFile, strKeyFile,
                            h, ne, ltrPath, strFillInit, strFillInitEx, strFillEnd, strFill);
            }

            sb.AppendLine("</body></html>");

            string strDoc = sb.ToString();

#if DEBUG
            XmlUtilEx.ValidateXml(strDoc, true);
#endif
            return(strDoc);
        }