public void verify_asimmetric_file_encryption_file_decrypt()
        {
            var fileName          = "TestFiles\\SampleFile.txt";
            var encryptedFileName = "TestFiles\\SampleFile.encrypted";
            var decryptedFileName = "TestFiles\\SampleFile.decrypted";

            if (File.Exists(encryptedFileName))
            {
                File.Delete(encryptedFileName);
            }

            var key       = EncryptionUtils.GenerateAsimmetricKey();
            var publicKey = key.GetPublicKey();

            var encryptionKeyAsString = EncryptionUtils.EncryptFile(fileName, encryptedFileName, publicKey);

            //now we need to decrypt the key used to encrypt the file
            var encryptedKey = EncryptionKey.CreateFromSerializedString(encryptionKeyAsString);
            var decryptedKey = EncryptionUtils.Decrypt(key, encryptedKey);

            EncryptionUtils.DecryptFile(decryptedKey, encryptedFileName, decryptedFileName);
            var decryptedString = File.ReadAllText(decryptedFileName);

            Assert.That(decryptedString, Is.EqualTo("This is a sample text file."));
        }
        public void Verify_basic_key_serialization()
        {
            var key        = EncryptionUtils.GenerateKey();
            var serialized = key.SerializeAsString();
            var restored   = EncryptionKey.CreateFromSerializedString(serialized);

            CollectionAssert.AreEqual(key.IV, restored.IV);
            CollectionAssert.AreEqual(key.Key, restored.Key);
        }
예제 #3
0
        private void DecryptFile(string tempFolder, string keyFile, string encryptedFile, Customer customer)
        {
            var encryptionKeyString = File.ReadAllText(keyFile);
            var encryptedKey        = EncryptionKey.CreateFromSerializedString(encryptionKeyString);
            var asymmetricKey       = AsymmetricEncryptionKey.CreateFromString(customer.PrivateKey, true);
            var decryptedKey        = EncryptionUtils.Decrypt(asymmetricKey, encryptedKey);

            //now we can decrypt
            var decryptedFileName = Path.Combine(tempFolder, "decrypted.zip");

            EncryptionUtils.DecryptFile(decryptedKey, encryptedFile, decryptedFileName);
            Logger.DebugFormat("Decrypted file {0} ", decryptedFileName);

            //now we can unzip
            ZipFile.ExtractToDirectory(decryptedFileName, tempFolder);
            Logger.DebugFormat("Unzipped file {0} ", decryptedFileName);
        }
 public static Byte[] Decrypt(String key, Byte[] data)
 {
     return Decrypt(EncryptionKey.CreateFromSerializedString(key), data);
 }
예제 #5
0
 public static String Decrypt(String key, String data)
 {
     return(Decrypt(EncryptionKey.CreateFromSerializedString(key), data));
 }