Encrypt() public method

Encrypt the specified files into the directory with the given provider.
public Encrypt ( IEnumerable files, String directory, Aes provider ) : bool
files IEnumerable
directory String
provider System.Security.Cryptography.Aes
return bool
        public void TestImageEncryption()
        {
            File.Copy ("Embedded/application-sidebar-list.nopng", "dummy.png", true);

            FileEncrypter encrypter = new FileEncrypter ();
            encrypter.Encrypt (new String[]{"dummy.png"}, ".", KEY);
        }
Example #2
0
        public override bool Execute()
        {
            if (this.SourceFiles.Length == 0) {
                return true;
            }

            if (this.DestinationFiles != null && this.SourceFiles.Length != this.DestinationFiles.Length) {
                this.Log.LogError ("Number of source files is different than number of destination files.");
                return false;
            }

            if (this.DestinationFiles != null && this.DestinationFolder != null) {
                this.Log.LogError ("You must specify only one attribute from DestinationFiles and DestinationFolder");
                return false;
            }

            FileEncrypter encrypter = new FileEncrypter ();
            encrypter.Logger = new ExecutionLogger (this);

            Aes provider = new AesCryptoServiceProvider ();
            provider.Key = FileEncrypter.DeriveKey (this.EncryptionSeed);

            if (this.DestinationFiles != null && this.DestinationFiles.Length > 0) {
                for (int i = 0; i < this.SourceFiles.Length; i++) {
                    ITaskItem sourceItem = this.SourceFiles [i];
                    ITaskItem destinationItem = this.DestinationFiles [i];
                    String sourcePath = sourceItem.GetMetadata ("FullPath");
                    String destinationPath = destinationItem.GetMetadata ("FullPath");
                    if (!File.Exists (sourcePath)) {
                        this.Log.LogError ("Cannot encrypt {0} to {1}, as the source file doesn't exist.", new object[] {
                            sourcePath,
                            destinationPath
                        });
                    } else {
                        String parentDestinationPath = Path.GetDirectoryName(destinationPath);
                        if (!Directory.Exists(parentDestinationPath)) {
                            Directory.CreateDirectory(parentDestinationPath);
                        }
                        encrypter.Encrypt (sourcePath, destinationPath, provider);
                    }
                }
                return true;
            }

            if (this.DestinationFolder == null) {
                this.Log.LogError ("You must specify DestinationFolder attribute.");
                return false;
            }

            String destinationFolder = this.DestinationFolder.GetMetadata ("FullPath");
            if (!Directory.Exists (destinationFolder)) {
                Directory.CreateDirectory(destinationFolder);
            }

            for (int i = 0; i < this.SourceFiles.Length; i++) {
                ITaskItem sourceItem = this.SourceFiles [i];
                String sourcePath = sourceItem.GetMetadata ("FullPath");
                String path = sourceItem.GetMetadata ("Filename") + sourceItem.GetMetadata ("Extension");
                String destinationPath = Path.Combine (destinationFolder, path);
                if (!File.Exists (sourcePath)) {
                    this.Log.LogError ("Cannot encrypt {0} to {1}, as the source file doesn't exist.", new object[] {
                        sourcePath,
                        destinationPath
                    });
                } else {
                    encrypter.Encrypt (sourcePath, destinationPath, provider);
                }
            }

            return true;
        }