예제 #1
0
        /// <summary>
        /// Restores a Cryptographic key from previously exported file. Use to transfer key to new computer
        /// </summary>
        /// <param name="ImportFile">Previously exported symmetric key to be restored</param>
        /// <param name="Password">Password used to secure previously exported key</param>
        /// <param name="NewProviderKey">Location and filename of key to be restored.</param>
        /// <returns></returns>
        public static RestoreSecureKeyResult Restore(FileInfo ImportFile,
                                                     string Password,
                                                     string NewProviderKey)
        {
            RestoreSecureKeyResult result = new RestoreSecureKeyResult
            {
                IsInError   = false,
                ImportFile  = ImportFile.FullName,
                RestoredKey = NewProviderKey,
                Password    = Password
            };
            ProtectedKey RestoredSecureKey;

            KeyManager.ClearCache();

            try
            {
                using (FileStream fs = File.OpenRead(ImportFile.FullName))
                {
                    RestoredSecureKey = KeyManager.RestoreKey(fs, Password, DataProtectionScope.LocalMachine);
                }

                using (FileStream ofs = File.OpenWrite(NewProviderKey))
                {
                    KeyManager.Write(ofs, RestoredSecureKey);
                }
            } catch (Exception ex)
            {
                result.IsInError   = true;
                result.ErrorString = ex.Message.ToString();
                if (result.ErrorString.StartsWith("Padding is invalid and cannot be removed"))
                {
                    result.ErrorString += "\nIt may be due to an incorrect password.";
                }
                result.Exception = ex;
            }

            return(result);
        }
예제 #2
0
        public static int RunRestore(Options.RestoreKey RestoreKeyOpts)
        {
            // Checks
            int           inError    = 0;
            StringBuilder errMsgText = new StringBuilder();

            if (!File.Exists(RestoreKeyOpts.ArchiveFile))
            {
                // Import file Doesn't exist
                inError = 1;
                errMsgText.Append($"- Provided import file, '{RestoreKeyOpts.ArchiveFile}', doesn't exist.\n");
            }

            try
            {
                using (FileStream tfs = File.OpenWrite(RestoreKeyOpts.OutputKeyFile))
                { }
            } catch (UnauthorizedAccessException)
            {
                inError = 2;
                errMsgText.Append($"- Unable to open output file, '{RestoreKeyOpts.OutputKeyFile}', for writting\n");
            } catch (DirectoryNotFoundException)
            {
                inError = 3;
                errMsgText.Append($"- Directory not found, cannot create output file: '{RestoreKeyOpts.OutputKeyFile}'\n");
            } catch (Exception ex)
            {
                inError = 9;
                errMsgText.Append($"- Unknown error encountered:\n{ex.Message}\n");
            }

            if (RestoreKeyOpts.Password.Trim() == string.Empty)
            {
                inError = 4;
                errMsgText.Append($"- Provided password value is empty or contains only whitespace\n");
            }

            if (inError > 0)
            {
                Console.Write($"\nThere is a problem with the provided parameters:\n\n{errMsgText.ToString()}\n\n");
                return(inError);
            }

            // Work

            FileInfo importFile           = new FileInfo(RestoreKeyOpts.ArchiveFile);
            RestoreSecureKeyResult result = RestoreSecureKey.Restore(importFile, RestoreKeyOpts.Password, RestoreKeyOpts.OutputKeyFile);

            if (result.IsInError)
            {
                Console.Write($"\nUnable to restore secure key. Exception message is:\n{result.ErrorString}\n");
                return(5);
            }
            if (File.Exists(RestoreKeyOpts.OutputKeyFile) && new FileInfo(RestoreKeyOpts.OutputKeyFile).Length > 10)
            {
                Console.Write($"\nKey, {RestoreKeyOpts.OutputKeyFile}, created successfully!\n");
                return(0);
            }
            else
            {
                Console.Write($"\nKey, {RestoreKeyOpts.OutputKeyFile}, creation failed!\n");
                return(-1);
            }
        }