예제 #1
0
        public static EncrypterStatus Encrypt(string path, string key, bool deleteOnFinish = false, bool shreadOnFinish = false, EncryptProgressDelegate onProgressChange = null)
        {
            if (onProgressChange != null)
            {
                onProgressChange(0, EncrypterProgressState.compressing);
            }

            string directory;
            string zipName;
            string resultingName;

            if (File.Exists(path))
            {
                FileInfo file     = new FileInfo(path);
                string   fileName = Path.GetFileNameWithoutExtension(path);
                directory     = file.DirectoryName;
                zipName       = directory + @"\" + fileName + tempExtension;
                resultingName = directory + @"\" + fileName + encryptedExtension;

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

                if (CreateZipFromFile(path, zipName) == false)
                {
                    return(EncrypterStatus.unable);
                }
            }
            else if (Directory.Exists(path))
            {
                DirectoryInfo dir      = new DirectoryInfo(path);
                string        fileName = dir.Name;
                directory     = dir.Parent.FullName;
                zipName       = directory + @"\" + fileName + tempExtension;
                resultingName = directory + @"\" + fileName + encryptedExtension;

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

                CreateZipFromDirectory(path, zipName);
            }
            else
            {
                return(EncrypterStatus.doesNotExist);
            }

            EncrypterStatus encryptionResult = CryptFile(zipName, resultingName, key, EncrypterMode.Encrypt, onProgressChange);

            if (encryptionResult != EncrypterStatus.OK)
            {
                Delete(zipName);
                return(encryptionResult);
            }

            if (onProgressChange != null)
            {
                onProgressChange(0, EncrypterProgressState.cleanup);
            }

            Delete(zipName);

            if (deleteOnFinish)
            {
                Delete(path);
            }

            return(encryptionResult);
        }
예제 #2
0
        public static EncrypterStatus Decrypt(string path, string key, bool deleteOnFinish = false, bool shreadOnFinish = false, EncryptProgressDelegate onProgressChange = null)
        {
            if (File.Exists(path))
            {
                if (new FileInfo(path).Extension == encryptedExtension)
                {
                    FileInfo file      = new FileInfo(path);
                    string   directory = file.DirectoryName;
                    string   zipName   = directory + @"\" + Path.GetFileNameWithoutExtension(path) + tempExtension;

                    EncrypterStatus decryptionResult = CryptFile(path, zipName, key, EncrypterMode.Decrypt, onProgressChange);

                    if (decryptionResult != EncrypterStatus.OK)
                    {
                        Delete(zipName);
                        return(decryptionResult);
                    }

                    if (onProgressChange != null)
                    {
                        onProgressChange(0, EncrypterProgressState.extracting);
                    }

                    if (ExtractFromZip(zipName, directory) == false)
                    {
                        return(EncrypterStatus.unable);
                    }

                    if (onProgressChange != null)
                    {
                        onProgressChange(0, EncrypterProgressState.cleanup);
                    }

                    Delete(zipName);

                    if (deleteOnFinish)
                    {
                        Delete(path);
                    }

                    return(decryptionResult);
                }
                else
                {
                    return(EncrypterStatus.unable);
                }
            }
            else
            {
                return(EncrypterStatus.doesNotExist);
            }
        }
예제 #3
0
        private static EncrypterStatus CryptFile(string path, string resultPath, string key, EncrypterMode mode, EncryptProgressDelegate onProgressChange = null)
        {
            if (!File.Exists(path))
            {
                return(EncrypterStatus.doesNotExist);
            }

            if (File.Exists(resultPath))
            {
                return(EncrypterStatus.targetExists);
            }

            try
            {
                EncrypterProgressState progressState;

                byte[] keyByteArray = Encoding.ASCII.GetBytes(key);
                byte[] md5          = MD5.Create().ComputeHash(keyByteArray);
                byte[] sha256       = SHA256.Create().ComputeHash(keyByteArray);

                Aes aes = AesCryptoServiceProvider.Create();
                aes.IV  = md5;
                aes.Key = sha256;

                ICryptoTransform crypto;
                if (mode == EncrypterMode.Encrypt)
                {
                    crypto        = aes.CreateEncryptor();
                    progressState = EncrypterProgressState.encrypting;
                }
                else
                {
                    crypto        = aes.CreateDecryptor();
                    progressState = EncrypterProgressState.decrypting;
                }

                using (FileStream fsIn = new FileStream(path, FileMode.Open))
                    using (FileStream fsOut = new FileStream(resultPath, FileMode.Create))
                        using (CryptoStream cryptoStream = new CryptoStream(fsOut, crypto, CryptoStreamMode.Write))
                        {
                            long fileLength = fsIn.Length;
                            long percentMod = (long)Math.Floor((double)(fileLength / 100));

                            fsOut.SetLength(fileLength);


                            while (fsIn.Position < fileLength)
                            {
                                cryptoStream.WriteByte((byte)fsIn.ReadByte()); //ReadByte returns an int

                                if (!Delegate.Equals(onProgressChange, null))
                                {
                                    if (fsIn.Position % percentMod == 0)
                                    {
                                        onProgressChange((int)Math.Round((double)(fsIn.Position / percentMod)), progressState);
                                    }
                                }
                            }
                        }
            }
            catch (CryptographicException)
            {
                return(EncrypterStatus.incorrectKey);
            }
            catch
            {
                return(EncrypterStatus.unable);
            }
            return(EncrypterStatus.OK);
        }