示例#1
0
        public void EncryptTest()
        {
            string cipherText = AesAlgorithm.Encrypt(m_sourceString);

            if (!String.IsNullOrWhiteSpace(cipherText))
            {
                Assert.IsNotNull(cipherText);
            }
            else
            {
                Assert.Fail();
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            //Guid key generated with base64encoded and uppercase combination
            var key = "A+W2nzdpbEe3UHrCBZU5Qw==";

            //Console.WriteLine("Please enter a secret key for the symmetric algorithm.");
            //var key = Console.ReadLine();

            Console.WriteLine("Please enter a string for encryption");
            var str             = Console.ReadLine();
            var encryptedString = AesAlgorithm.Encrypt(key, str);

            Console.WriteLine($"encrypted string = {encryptedString}");

            var decryptedString = AesAlgorithm.Decrypt(key, encryptedString);

            Console.WriteLine($"decrypted string = {decryptedString}");

            Console.ReadKey();
        }
示例#3
0
        /// <summary>
        /// Creates a new <see cref="Fortress"/> with a <see cref="MasterKey"/> and saves it encrypted.
        /// </summary>
        internal void WriteFortress(Fortress fortress, bool overwrite = false)
        {
            try
            {
                Logger.log.Info("Starting to write a fortress...");
                var databasePath = Path.Combine(fortress.FullPath, TermHelper.GetDatabaseTerm());

                // =========================================================== Create the root directory

                IOPathHelper.CreateDirectory(fortress.FullPath);
                Logger.log.Debug($"Created outer walls {fortress.FullPath}.");

                // =========================================================== Create the sub directories for the database

                IOPathHelper.CreateDirectory(databasePath);
                Logger.log.Debug($"Created the {TermHelper.GetDatabaseTerm()}");

                // =========================================================== Create the file which holds the salt to unlock the database

                StoreSalt(fortress.FullPath, fortress.Salt);
                Logger.log.Debug("Stored salt");

                // =========================================================== Store the user Input and initial data in the database

                foreach (var modelList in _unsecureDatacache.Values) // UnsecureDatacache
                {
                    foreach (var model in modelList)
                    {
                        StoreOne(model);
                    }
                }

                foreach (var pair in _secureDatacache)
                {
                    // We filter: Only if the sensible data has a parent we want to save it. Otherwise the parent has been deleted,
                    // which makes the sensible counterpart useless.
                    if (_unsecureDatacache.Values.Any(l => l.Any(m => m.Id == pair.Key)))
                    {
                        var byteModel = new ByteModel(pair.Key, pair.Value);
                        StoreOne(null, true, byteModel);
                    }
                }

                Logger.log.Debug("Stored fortress information.");

                // =========================================================== Zip only the database

                ZipHelper.ZipSavedArchives(databasePath, $"{databasePath}{TermHelper.GetZippedFileEnding()}");
                Directory.Delete(databasePath, true);
                Logger.log.Debug($"{TermHelper.GetDatabaseTerm()} has been zipped.");

                // =========================================================== Encrypt the database

                var aesAlg = new AesAlgorithm();
                // Read all bytes from the database directory
                var data = File.ReadAllBytes($"{databasePath}{TermHelper.GetZippedFileEnding()}");
                // Encrypt it
                var encryptedData = aesAlg.Encrypt(data, fortress.MasterKey.Value, fortress.Salt);
                // Write the encrypted file
                File.WriteAllBytes($"{databasePath}{TermHelper.GetDatabaseEnding()}", encryptedData);
                // Delete the zip
                File.Delete($"{databasePath}{TermHelper.GetZippedFileEnding()}");
                Logger.log.Debug($"Encrypted {TermHelper.GetDatabaseTerm()}");

                // =========================================================== Zip the whole fortress

                if (overwrite)
                {
                    File.Delete($"{fortress.FullPath}{TermHelper.GetZippedFileEnding()}");
                }

                ZipHelper.ZipSavedArchives(fortress.FullPath, $"{fortress.FullPath}{TermHelper.GetZippedFileEnding()}");
                Directory.Delete(fortress.FullPath, true);
                Logger.log.Debug("Fortress has been zipped.");

                Logger.log.Info("Fortress has been sucessfully written!");
            }
            catch (Exception ex)
            {
                // Delete all changes that have been made to this point. We do not want half-built fortresses.
                if (Directory.Exists(fortress.FullPath))
                {
                    Directory.Delete(fortress.FullPath, true);
                }
                if (File.Exists(Path.Combine(fortress.FullPath, TermHelper.GetZippedFileEnding())))
                {
                    File.Delete(fortress.FullPath + TermHelper.GetZippedFileEnding());
                }

                ex.SetUserMessage(WellKnownExceptionMessages.DataExceptionMessage());
                throw ex;
            }
        }
示例#4
0
 /// <summary>
 /// Encrypts the full file name using the file Key and Iv values with AES-OFB algorithm and encodes it to <see href="https://en.wikipedia.org/wiki/Base64">Base64</see>.
 /// Since Base64 contains forward slash ('/') which is a <see href="https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file">reserved character</see> that can't be used for file naming, every '/' is replaced with '$'.
 /// </summary>
 /// <param name="name">Full name of the file (name + extension) that is being encrypted.</param>
 /// <param name="aes">AES algorithm used for decryption of the full file name.</param>
 public void NameEncryption(string name, AesAlgorithm aes)
 {
     EncryptedName = Convert.ToBase64String(aes.Encrypt(Encoding.ASCII.GetBytes(name)));
     EncryptedName = EncryptedName.Replace('/', '$');
 }