예제 #1
0
        private static async ValueTask <string> EncryptFileAsync(
            string fileName, Cryptographer cryptographer)
        {
            string encryptedFileName = $"{fileName}.encrypt";

            await using FileStream outputFileStream =
                            new FileStream(encryptedFileName, FileMode.Create);

            string data = await File.ReadAllTextAsync(fileName);

            await cryptographer.EncryptAsync(data, outputFileStream);

            return(encryptedFileName);
        }
예제 #2
0
        public void EncryptAesManaged()
        {
            string text = "You've fallen for one of the two classic blunders! The first being never get involved in a land war in Asia but only slightly lesser known: never go in against a cicelean when DEATH is on the line! HAHAHAHAHAHAHA *dies* You only think I guessed wrong! That's what's so funny! I switched glasses when your back was turned! Ha ha! You fool! You fell victim to one of the classic blunders - The most famous of which is 'never get involved in a land war in Asia' - but only slightly less well - known is this: 'Never go against a Sicilian when death is on the line!' Ha ha ha ha ha ha ha!";

            // Create Aes that generates a new key and initialization vector (IV).
            // Same key must be used in encryption and decryption
            using (AesManaged aes = new AesManaged())
            {
                // Encrypt string
                byte[] encrypted = Cryptographer.EncryptAsync(text, aes.Key, aes.IV).Result;
                // Decrypt the bytes to a string.
                string decrypted = Cryptographer.DecryptAsync(encrypted, aes.Key, aes.IV).Result;
                // Print decrypted string. It should be same as raw data
                Assert.AreEqual <string>(text, decrypted);
            }
        }
예제 #3
0
        static public async IAsyncEnumerable <string> EncryptFilesAsync(
            string directoryPath = null, string searchPattern = "*",
            [EnumeratorCancellation] CancellationToken cancellationToken = default)
        {
            IEnumerable <string> files = Directory.EnumerateFiles(
                directoryPath ?? Directory.GetCurrentDirectory(), searchPattern,
                SearchOption.AllDirectories);

            foreach (string fileName in files)
            {
                string encryptedFileName = $"{fileName}.encrypt";
                using (FileStream outputFileStream =
                           new FileStream(encryptedFileName, FileMode.Create))
                {
                    string data = await File.ReadAllTextAsync(fileName);

                    await Cryptographer.EncryptAsync(data, outputFileStream);

                    yield return(encryptedFileName);

                    cancellationToken.ThrowIfCancellationRequested();
                }
            }
        }