Esempio n. 1
0
        /// <summary>
        /// Create the RSACryptoServiceProvider for the domain
        /// </summary>
        /// <param name="basePath">Path of the private key to open</param>
        /// <returns></returns>
        public bool initElement(string basePath)
        {
            string path;

            if (Path.IsPathRooted(privateKeyFile))
            {
                path = @"keys\" + privateKeyFile;
            }
            else
            {
                path = Path.Combine(basePath, @"keys\" + privateKeyFile);
            }

            if (!File.Exists(path))
            {
                Logger.LogError("PrivateKey for domain " + domain + " not found: " + path);
                return(false);
            }

            byte[] key = File.ReadAllBytes(path);
            switch (RSACryptoHelper.GetFormatFromEncodedRsaPrivateKey(key))
            {
            case RSACryptoFormat.PEM:
                string pemkey = File.ReadAllText(path, Encoding.ASCII);
                cryptoProvider = RSACryptoHelper.GetProviderFromPemEncodedRsaPrivateKey(pemkey);
                break;

            case RSACryptoFormat.DER:
                cryptoProvider = RSACryptoHelper.GetProviderFromDerEncodedRsaPrivateKey(key);
                break;

            case RSACryptoFormat.XML:
                string xmlkey = File.ReadAllText(path, Encoding.ASCII);
                cryptoProvider = RSACryptoHelper.GetProviderFromXmlEncodedRsaPrivateKey(xmlkey);
                break;

            default:
                throw new ArgumentException(
                          Resources.RSACryptHelper_UnknownFormat,
                          "encodedKey");
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Button "Upload" in domain configuration action - Upload the selected private key file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btUpload_Click(object sender, EventArgs e)
        {
            if (dgvDomainConfiguration.SelectedCells.Count > 0)
            {
                using (OpenFileDialog fileDialog = new OpenFileDialog())
                {
                    fileDialog.CheckFileExists = true;
                    fileDialog.CheckPathExists = true;
                    fileDialog.Filter          = "All Files|*.*";
                    fileDialog.Title           = "Select a file";
                    fileDialog.Multiselect     = false;

                    if (fileDialog.ShowDialog() == DialogResult.OK)
                    {
                        FileInfo fileInfo   = new FileInfo(fileDialog.FileName);
                        byte[]   binaryData = File.ReadAllBytes(fileDialog.FileName);
                        if (RSACryptoHelper.GetFormatFromEncodedRsaPrivateKey(binaryData) != RSACryptoFormat.UNKNOWN)
                        {
                            dgvDomainConfiguration.Rows[dgvDomainConfiguration.SelectedCells[0].RowIndex].Cells[2].Value = fileInfo.Name;

                            if (attachments.ContainsKey(dgvDomainConfiguration.SelectedCells[0].RowIndex))
                            {
                                attachments[dgvDomainConfiguration.SelectedCells[0].RowIndex] = binaryData;
                            }
                            else
                            {
                                attachments.Add(dgvDomainConfiguration.SelectedCells[0].RowIndex, binaryData);
                            }
                        }
                        else
                        {
                            MessageBox.Show("The format of the private key file you try to import is invalid.");
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Select a row for upload the private key.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }