Ejemplo n.º 1
0
        public void TestImageEncryption()
        {
            File.Copy("Embedded/application-sidebar-list.nopng", "dummy.png", true);

            FileEncrypter encrypter = new FileEncrypter();

            encrypter.Encrypt(new String[] { "dummy.png" }, ".", KEY);
        }
Ejemplo n.º 2
0
        private async Task EncryptDataBaseAsync(string password)
        {
            FileEncrypter encrypter = new FileEncrypter(new AESEncrypter(password));

            if (File.Exists(StorageFile))
            {
                File.Delete(StorageFile);
                await Task.Delay(TimeSpan.FromMilliseconds(100)); // 等待100毫秒,确定文件处于空闲状态
            }
            //向储存库写入数据
            lock (_storageFileLocker) {
                File.Copy(RuntimeStorageFileName, StorageFile);
                encrypter.Encrypt(StorageFile);
                Thread.Sleep(TimeSpan.FromMilliseconds(100)); // 等待100毫秒,确定文件处于空闲状态
                StorageEncrypted?.Invoke(this, new StorageEncryptedEventArgs(password));
            }
        }
Ejemplo n.º 3
0
        public void TestImageDecryption()
        {
            Aes provider = FileEncrypter.GetProvider(KEY);

            {
                byte[]  clean   = File.ReadAllBytes("Embedded/application-sidebar-list.nopng");
                byte [] content = FileEncrypter.Encrypt(clean, provider);

                byte[] output = FileEncrypter.Decrypt(content, provider);
                File.WriteAllBytes("dummy2.png", output);
            }
            {
                byte [] content = File.ReadAllBytes("dummy.png");
                byte[]  output  = FileEncrypter.Decrypt(content, provider);
                File.WriteAllBytes("dummy3.png", output);
            }
        }
Ejemplo n.º 4
0
        public override void Handle(Context context)
        {
            if (!context.Arguments.TryGetValue(WellKnownCommandArguments.FILE_PATH, out var filePath))
            {
                throw new MissingFilePathException("Missing file path argument. try encrypt --help for more information");
            }

            if (!context.Arguments.TryGetValue(WellKnownCommandArguments.IMPORT_KEY, out var publicKeyPath))
            {
                throw new MissingKeyException("Missing key argument. try encrypt --help for more information");
            }

            if (!context.Arguments.TryGetValue(WellKnownCommandArguments.ENCRYPTED_FILE_OUT, out var encryptedFilePath))
            {
                throw new MissingFilePathException("Missing encrypted file path argument. try encrypt --help for more information");
            }

            if (!context.Arguments.TryGetValue(WellKnownCommandArguments.SIGNATURE_CONTAINER, out var signatureContainer))
            {
                throw new ContainerNameMissingException("Missing name of signature container. try encrypt --help for more information");
            }

            var publicKey = File.ReadAllText(publicKeyPath);

            var fileEncrypter = new FileEncrypter(HybridEncryption.CreateEncryption(publicKey, signatureContainer),
                                                  new RNGCryptoRandomBytesGenerator());

            var encryptionResult = fileEncrypter.Encrypt(filePath);

            File.WriteAllBytes(encryptedFilePath, encryptionResult.data);

            var keyPath = new FileInfo(encryptedFilePath).Directory.FullName;

            File.WriteAllBytes($"{keyPath}/decryptionkey", encryptionResult.key.ExportToBlob());

            SetEndState(context);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 创建默认数据
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static void CreateEmptyUserData(string filePath)
        {
            APMCore.Model.UserData source = new APMCore.Model.UserData()
            {
                Avatar       = UserAvatarFileName,
                UserName     = "******",
                UserPassword = HashString.SHA("000000"),
                Description  = "CatCatHead",
                ColumnSize   = 3,
                IsEditMode   = false,
                Storage      = UserStorageFileName
            };

            UserData userData = new UserData(source);

            userData.SaveToFile(filePath);

            APMCore.ViewModel.StorageBase.CreateEmptyStorage(UserStorageFileName);
            File.Copy(UserStorageFileName, RuntimeStorageFileName);
            FileEncrypter encrypter = new FileEncrypter(new AESEncrypter("000000"));

            encrypter.Encrypt(RuntimeStorageFileName);
            File.Copy(RuntimeStorageFileName, UserStorageFileName, true);
        }
Ejemplo n.º 6
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);
        }