예제 #1
0
        /// <summary>
        /// Generates a new Cryptographic key from Security.Cryptography. Use to initiate new basis for encrypting objects.
        /// </summary>
        /// <param name="NewKeyFile">Name of new keyfile to be generated</param>
        /// <param name="Password">Password used to secure the new keyfile</param>
        /// <returns></returns>
        public static bool Generate(FileInfo NewKeyFile, string Password)
        {
            KeyManager.ClearCache();

            byte[]       managerByteKey  = KeyManager.GenerateSymmetricKey("Rijndael");
            ProtectedKey newProtectedKey = ProtectedKey.CreateFromPlaintextKey(managerByteKey, DataProtectionScope.LocalMachine);

            using (FileStream ofs = File.OpenWrite(NewKeyFile.FullName))
            {
                KeyManager.ArchiveKey(ofs, newProtectedKey, Password);
            }

            return(true);
        }
예제 #2
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);
        }
예제 #3
0
        public void CleanUpStream()
        {
            KeyManager.ClearCache();

            stream.Dispose();
        }