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); } }
private void ShowErrorMessage(EncrypterStatus status) { switch (status) { case EncrypterStatus.unable: MessageBox.Show("Unable to complete action.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case EncrypterStatus.doesNotExist: MessageBox.Show("The file you are trying to encrypt/decrypt doesn't exist. Aborting.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case EncrypterStatus.targetExists: MessageBox.Show("The target destination already exists. Aborting.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case EncrypterStatus.incorrectKey: MessageBox.Show("Incorrect password. Aborting.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); break; default: break; } }
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); }