2019-08-21 09:23:51 +00:00
using IWshRuntimeLibrary ;
2018-12-17 11:16:17 +00:00
using System ;
using System.ComponentModel ;
using System.Diagnostics ;
using System.Drawing ;
using System.Globalization ;
using System.IO ;
using System.Linq ;
using System.Net ;
using System.Reflection ;
using System.Security.Cryptography ;
using System.Threading ;
using System.Windows.Forms ;
using File = System . IO . File ;
namespace Crypto_Notepad
{
public partial class MainForm : Form
{
Properties . Settings ps = Properties . Settings . Default ;
string filePath = "" ;
string [ ] args = Environment . GetCommandLineArgs ( ) ;
int caretPos = 0 ;
bool shiftPresed ;
bool noExit = false ;
string argsPath = "" ;
2018-12-25 15:29:29 +00:00
2018-12-17 11:16:17 +00:00
public MainForm ( )
{
InitializeComponent ( ) ;
2018-12-25 15:29:29 +00:00
CustomRTB . DragDrop + = new DragEventHandler ( CustomRTB_DragDrop ) ;
CustomRTB . AllowDrop = true ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
/*Functions*/
private void DecryptAES ( )
2018-12-17 11:16:17 +00:00
{
EnterKeyForm f2 = new EnterKeyForm ( ) ;
f2 . ShowDialog ( ) ;
2018-12-25 15:29:29 +00:00
if ( ! PublicVar . okPressed )
2018-12-17 11:16:17 +00:00
{
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( filePath ) ;
2018-12-17 11:16:17 +00:00
return ;
}
2018-12-25 15:29:29 +00:00
if ( SearchPanel . Visible )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
FindToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
2018-12-17 11:16:17 +00:00
}
try
{
string opnfile = File . ReadAllText ( OpenFile . FileName ) ;
string NameWithotPath = Path . GetFileName ( OpenFile . FileName ) ;
2019-01-04 11:15:06 +00:00
string de ;
2019-01-04 11:16:40 +00:00
2019-01-04 11:15:06 +00:00
if ( ps . TheSalt ! = null )
{
de = AES . Decrypt ( opnfile , TypedPassword . Value , ps . TheSalt , ps . HashAlgorithm , ps . PasswordIterations , ps . KeySize ) ;
}
else
{
de = AES . Decrypt ( opnfile , TypedPassword . Value , null , ps . HashAlgorithm , ps . PasswordIterations , ps . KeySize ) ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
CustomRTB . Text = de ;
2018-12-17 11:16:17 +00:00
2019-01-07 10:09:52 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-17 11:16:17 +00:00
filePath = OpenFile . FileName ;
2018-12-25 15:29:29 +00:00
string cc2 = CustomRTB . Text . Length . ToString ( CultureInfo . InvariantCulture ) ;
CustomRTB . Select ( Convert . ToInt32 ( cc2 ) , 0 ) ;
2018-12-17 11:16:17 +00:00
PublicVar . openFileName = Path . GetFileName ( OpenFile . FileName ) ;
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
}
catch ( CryptographicException )
{
using ( new CenterWinDialog ( this ) )
{
TypedPassword . Value = null ;
2019-01-07 10:08:47 +00:00
DialogResult dialogResult = MessageBox . Show ( "Invalid key!" , PublicVar . appName , MessageBoxButtons . RetryCancel , MessageBoxIcon . Error ) ;
2018-12-17 11:16:17 +00:00
if ( dialogResult = = DialogResult . Retry )
{
DecryptAES ( ) ;
}
if ( dialogResult = = DialogResult . Cancel )
{
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( filePath ) ;
2018-12-17 11:16:17 +00:00
return ;
}
2019-01-07 10:09:52 +00:00
2018-12-17 11:16:17 +00:00
}
}
}
2018-12-25 15:29:29 +00:00
private void OpenAsotiations ( )
2018-12-17 11:16:17 +00:00
{
EnterKeyForm Form2 = new EnterKeyForm ( ) ;
Form2 . StartPosition = FormStartPosition . CenterScreen ;
string fileExtension = Path . GetExtension ( args [ 1 ] ) ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( args [ 1 ] ) ;
2018-12-17 11:16:17 +00:00
if ( fileExtension = = ".cnp" )
{
try
{
string NameWithotPath = Path . GetFileName ( args [ 1 ] ) ;
string opnfile = File . ReadAllText ( args [ 1 ] ) ;
Form2 . ShowDialog ( ) ;
2018-12-25 15:29:29 +00:00
if ( ! PublicVar . okPressed )
2018-12-17 11:16:17 +00:00
{
OpenFile . FileName = "" ;
return ;
}
PublicVar . okPressed = false ;
2018-12-19 22:56:59 +00:00
string de = AES . Decrypt ( opnfile , TypedPassword . Value , null , ps . HashAlgorithm , ps . PasswordIterations , ps . KeySize ) ;
2018-12-25 15:29:29 +00:00
CustomRTB . Text = de ;
2018-12-17 11:16:17 +00:00
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-17 11:16:17 +00:00
filePath = args [ 1 ] ;
2018-12-25 15:29:29 +00:00
string cc = CustomRTB . Text . Length . ToString ( CultureInfo . InvariantCulture ) ;
2018-12-17 11:16:17 +00:00
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
2018-12-25 15:29:29 +00:00
CustomRTB . Select ( Convert . ToInt32 ( cc ) , 0 ) ;
2018-12-17 11:16:17 +00:00
TypedPassword . Value = null ;
}
catch ( CryptographicException )
{
TypedPassword . Value = null ;
2019-01-07 10:08:47 +00:00
DialogResult dialogResult = MessageBox . Show ( "Invalid key!" , PublicVar . appName , MessageBoxButtons . RetryCancel , MessageBoxIcon . Error ) ;
2018-12-17 11:16:17 +00:00
if ( dialogResult = = DialogResult . Retry )
{
2018-12-25 15:29:29 +00:00
OpenAsotiations ( ) ;
2018-12-17 11:16:17 +00:00
}
}
}
else
{
string opnfile = File . ReadAllText ( args [ 1 ] ) ;
string NameWithotPath = Path . GetFileName ( args [ 1 ] ) ;
2018-12-25 15:29:29 +00:00
CustomRTB . Text = opnfile ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-25 15:29:29 +00:00
string cc2 = CustomRTB . Text . Length . ToString ( CultureInfo . InvariantCulture ) ;
CustomRTB . Select ( Convert . ToInt32 ( cc2 ) , 0 ) ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void SendTo ( )
2018-12-17 11:16:17 +00:00
{
EnterKeyForm f2 = new EnterKeyForm ( ) ;
f2 . StartPosition = FormStartPosition . CenterScreen ;
string fileExtension = Path . GetExtension ( argsPath ) ;
if ( fileExtension = = ".cnp" )
{
try
{
string NameWithotPath = Path . GetFileName ( argsPath ) ;
string opnfile = File . ReadAllText ( argsPath ) ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( argsPath ) ;
2018-12-17 11:16:17 +00:00
f2 . ShowDialog ( ) ;
2018-12-25 15:29:29 +00:00
if ( ! PublicVar . okPressed )
2018-12-17 11:16:17 +00:00
{
OpenFile . FileName = "" ;
return ;
}
PublicVar . okPressed = false ;
2018-12-19 22:56:59 +00:00
string de = AES . Decrypt ( opnfile , TypedPassword . Value , null , ps . HashAlgorithm , ps . PasswordIterations , ps . KeySize ) ;
2018-12-25 15:29:29 +00:00
CustomRTB . Text = de ;
2018-12-17 11:16:17 +00:00
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-17 11:16:17 +00:00
filePath = argsPath ;
2018-12-25 15:29:29 +00:00
string cc = CustomRTB . Text . Length . ToString ( CultureInfo . InvariantCulture ) ;
CustomRTB . Select ( Convert . ToInt32 ( cc ) , 0 ) ;
2018-12-17 11:16:17 +00:00
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
}
catch ( CryptographicException )
{
TypedPassword . Value = null ;
2019-01-07 10:08:47 +00:00
DialogResult dialogResult = MessageBox . Show ( "Invalid key!" , PublicVar . appName , MessageBoxButtons . RetryCancel , MessageBoxIcon . Error ) ;
2018-12-17 11:16:17 +00:00
if ( dialogResult = = DialogResult . Retry )
{
2018-12-25 15:29:29 +00:00
SendTo ( ) ;
2018-12-17 11:16:17 +00:00
}
}
}
else
{
string opnfile = File . ReadAllText ( argsPath ) ;
string NameWithotPath = Path . GetFileName ( argsPath ) ;
2018-12-25 15:29:29 +00:00
CustomRTB . Text = opnfile ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-25 15:29:29 +00:00
string cc2 = CustomRTB . Text . Length . ToString ( CultureInfo . InvariantCulture ) ;
CustomRTB . Select ( Convert . ToInt32 ( cc2 ) , 0 ) ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void SendToShortcut ( )
2018-12-17 11:16:17 +00:00
{
string shortcutPath = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) + @"\Microsoft\Windows\SendTo" ;
2019-01-07 10:08:47 +00:00
string shortcutName = PublicVar . appName + ".lnk" ;
2018-12-17 11:16:17 +00:00
string shortcutLocation = Path . Combine ( shortcutPath , shortcutName ) ;
string targetFileLocation = Assembly . GetEntryAssembly ( ) . Location ;
WshShell shell = new WshShell ( ) ;
IWshShortcut shortcut = ( IWshShortcut ) shell . CreateShortcut ( shortcutLocation ) ;
2019-01-07 10:08:47 +00:00
shortcut . Description = PublicVar . appName ;
2018-12-17 11:16:17 +00:00
shortcut . IconLocation = targetFileLocation ;
shortcut . TargetPath = targetFileLocation ;
shortcut . Arguments = "/s" ;
2018-12-25 15:29:29 +00:00
shortcut . Save ( ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void ContextMenuEncryptReplace ( )
2018-12-17 11:16:17 +00:00
{
if ( args [ 1 ] . Contains ( ".cnp" ) )
{
2019-01-07 10:08:47 +00:00
MessageBox . Show ( "Looks like this file is already encrypted" , PublicVar . appName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2018-12-17 11:16:17 +00:00
return ;
}
2019-01-07 10:08:47 +00:00
DialogResult res = MessageBox . Show ( "This action will delete the source file and replace it with encrypted version" , PublicVar . appName , MessageBoxButtons . OKCancel , MessageBoxIcon . Question ) ;
2018-12-17 11:16:17 +00:00
if ( res = = DialogResult . Cancel )
{
2018-12-17 13:03:08 +00:00
Environment . Exit ( 0 ) ;
2018-12-17 11:16:17 +00:00
}
if ( ! args [ 1 ] . Contains ( ".cnp" ) )
{
string opnfile = File . ReadAllText ( args [ 1 ] ) ;
2018-12-25 15:29:29 +00:00
CustomRTB . Text = opnfile ;
string cc2 = CustomRTB . Text . Length . ToString ( CultureInfo . InvariantCulture ) ;
CustomRTB . Select ( Convert . ToInt32 ( cc2 ) , 0 ) ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( args [ 1 ] ) ;
2018-12-17 11:16:17 +00:00
string newFile = Path . GetDirectoryName ( args [ 1 ] ) + @"\" + Path . GetFileNameWithoutExtension ( args [ 1 ] ) + ".cnp" ;
EnterKeyForm f2 = new EnterKeyForm ( ) ;
f2 . ShowDialog ( ) ;
2018-12-25 15:29:29 +00:00
if ( ! PublicVar . okPressed )
2018-12-17 11:16:17 +00:00
{
Application . Exit ( ) ;
}
PublicVar . okPressed = false ;
File . Delete ( args [ 1 ] ) ;
2018-12-25 15:29:29 +00:00
string noenc = CustomRTB . Text ;
2018-12-17 11:16:17 +00:00
string en ;
2018-12-25 15:29:29 +00:00
en = AES . Encrypt ( CustomRTB . Text , TypedPassword . Value , null , ps . HashAlgorithm , ps . PasswordIterations , ps . KeySize ) ;
CustomRTB . Text = en ;
2018-12-17 11:16:17 +00:00
StreamWriter sw = new StreamWriter ( newFile ) ;
2018-12-25 15:29:29 +00:00
int i = CustomRTB . Lines . Count ( ) ;
2018-12-17 11:16:17 +00:00
int j = 0 ;
i = i - 1 ;
while ( j < = i )
{
2018-12-25 15:29:29 +00:00
sw . WriteLine ( CustomRTB . Lines . GetValue ( j ) . ToString ( ) ) ;
2018-12-17 11:16:17 +00:00
j = j + 1 ;
}
sw . Close ( ) ;
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
filePath = newFile ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( newFile ) ;
Text = PublicVar . appName + " – " + PublicVar . openFileName ;
2018-12-25 15:29:29 +00:00
CustomRTB . Text = noenc ;
2018-12-17 11:16:17 +00:00
}
#region workaround , strange behavior with the cursor in customRTB fix
2018-12-25 15:29:29 +00:00
CustomRTB . DetectUrls = false ;
CustomRTB . DetectUrls = true ;
CustomRTB . Modified = false ;
2018-12-17 11:16:17 +00:00
#endregion
2018-12-25 15:29:29 +00:00
if ( PublicVar . okPressed )
2018-12-17 11:16:17 +00:00
{
PublicVar . okPressed = false ;
}
}
2018-12-25 15:29:29 +00:00
private void ContextMenuEncrypt ( )
2018-12-17 11:16:17 +00:00
{
if ( ! args [ 1 ] . Contains ( ".cnp" ) )
{
string opnfile = File . ReadAllText ( args [ 1 ] ) ;
string NameWithotPath = Path . GetFileName ( args [ 1 ] ) ;
2018-12-25 15:29:29 +00:00
CustomRTB . Text = opnfile ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + NameWithotPath ;
2018-12-25 15:29:29 +00:00
string cc2 = CustomRTB . Text . Length . ToString ( CultureInfo . InvariantCulture ) ;
CustomRTB . Select ( Convert . ToInt32 ( cc2 ) , 0 ) ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( args [ 1 ] ) ;
2018-12-17 11:16:17 +00:00
filePath = OpenFile . FileName ;
}
#region workaround , strange behavior with the cursor in customRTB fix
2018-12-25 15:29:29 +00:00
CustomRTB . DetectUrls = false ;
CustomRTB . DetectUrls = true ;
2018-12-17 11:16:17 +00:00
#endregion
2018-12-25 15:29:29 +00:00
CustomRTB . Modified = false ;
if ( PublicVar . okPressed )
2018-12-17 11:16:17 +00:00
{
PublicVar . okPressed = false ;
}
}
2018-12-25 15:29:29 +00:00
private void DeleteUpdateFiles ( )
2018-12-17 11:16:17 +00:00
{
string exePath = Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) . Location ) + @"\" ;
string UpdaterExe = exePath + "Updater.exe" ;
string UpdateZip = exePath + "Crypto-Notepad-Update.zip" ;
string ZipDll = exePath + "Ionic.Zip.dll" ;
if ( File . Exists ( UpdaterExe ) )
{
File . Delete ( UpdaterExe ) ;
}
if ( File . Exists ( UpdateZip ) )
{
File . Delete ( UpdateZip ) ;
}
if ( File . Exists ( ZipDll ) )
{
File . Delete ( ZipDll ) ;
}
}
2018-12-25 15:29:29 +00:00
private void SaveConfirm ( bool exit )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( ! CustomRTB . Modified )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( exit )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
Environment . Exit ( 0 ) ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
else
{
2019-01-07 10:09:52 +00:00
if ( PublicVar . openFileName = = null )
2018-12-25 15:29:29 +00:00
{
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = "Unnamed.cnp" ;
2018-12-25 15:29:29 +00:00
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( CustomRTB . Text ! = "" )
{
string messageBoxText = "" ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( ! PublicVar . keyChanged )
{
2019-01-07 10:09:52 +00:00
messageBoxText = "Save file: " + "\"" + PublicVar . openFileName + "\"" + " ? " ;
2018-12-25 15:29:29 +00:00
}
else
{
2019-01-07 10:09:52 +00:00
messageBoxText = "Save file: " + "\"" + PublicVar . openFileName + "\"" + " with a new key? " ;
2018-12-25 15:29:29 +00:00
}
2018-12-19 22:56:59 +00:00
2018-12-25 15:29:29 +00:00
using ( new CenterWinDialog ( this ) )
{
2019-01-07 10:08:47 +00:00
DialogResult res = MessageBox . Show ( messageBoxText , PublicVar . appName , MessageBoxButtons . YesNoCancel , MessageBoxIcon . Question ) ;
2018-12-25 15:29:29 +00:00
if ( res = = DialogResult . Yes )
{
SaveToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
if ( exit )
{
Environment . Exit ( 0 ) ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( res = = DialogResult . No )
{
if ( exit )
{
Environment . Exit ( 0 ) ;
}
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( res = = DialogResult . Cancel )
{
noExit = true ;
return ;
}
}
}
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void CheckForUpdates ( bool autoCheck )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
try
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
WebClient client = new WebClient ( ) ;
2019-01-02 19:10:41 +00:00
Stream stream = client . OpenRead ( "https://raw.githubusercontent.com/Crypto-Notepad/Crypto-Notepad/master/version.txt" ) ;
2018-12-25 15:29:29 +00:00
StreamReader reader = new StreamReader ( stream ) ;
string content = reader . ReadToEnd ( ) ;
string version = Application . ProductVersion ;
string exePath = Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) . Location ) + @"\" ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
int appVersion = Convert . ToInt32 ( version . Replace ( "." , "" ) ) , serverVersion = Convert . ToInt32 ( content . Replace ( "." , "" ) ) ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( serverVersion > appVersion )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
MainMenu . Invoke ( ( Action ) delegate
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
using ( new CenterWinDialog ( this ) )
{
2019-01-07 10:09:52 +00:00
DialogResult res = MessageBox . Show ( "New version is available. Install it now?" , PublicVar . appName , MessageBoxButtons . YesNo , MessageBoxIcon . Information ) ;
2018-12-25 15:29:29 +00:00
if ( res = = DialogResult . Yes )
{
File . WriteAllBytes ( exePath + "Ionic.Zip.dll" , Properties . Resources . Ionic_Zip ) ;
File . WriteAllBytes ( exePath + "Updater.exe" , Properties . Resources . Updater ) ;
var pr = new Process ( ) ;
pr . StartInfo . FileName = exePath + "Updater.exe" ;
pr . StartInfo . Arguments = "/u" ;
pr . Start ( ) ;
Application . Exit ( ) ;
}
}
} ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
if ( serverVersion < = appVersion & & autoCheck )
{
MainMenu . Invoke ( ( Action ) delegate
{
using ( new CenterWinDialog ( this ) )
{
2019-01-07 10:08:47 +00:00
MessageBox . Show ( "Crypto Notepad is up to date." , PublicVar . appName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
2018-12-25 15:29:29 +00:00
}
} ) ;
}
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
catch
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
return ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void AutoLock ( bool minimize )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
EnterKeyForm f2 = new EnterKeyForm ( ) ;
PublicVar . encryptionKey . Set ( null ) ;
caretPos = CustomRTB . SelectionStart ;
f2 . MinimizeBox = true ;
Hide ( ) ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( minimize )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
f2 . WindowState = FormWindowState . Minimized ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
f2 . ShowDialog ( ) ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( ! PublicVar . okPressed )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
PublicVar . encryptionKey . Set ( null ) ;
CustomRTB . Clear ( ) ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName ;
PublicVar . openFileName = null ;
2018-12-25 15:29:29 +00:00
filePath = "" ;
Show ( ) ;
return ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
PublicVar . okPressed = false ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
try
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
CustomRTB . Clear ( ) ;
string opnfile = File . ReadAllText ( filePath ) ;
string de = AES . Decrypt ( opnfile , TypedPassword . Value , null , ps . HashAlgorithm , ps . PasswordIterations , ps . KeySize ) ;
CustomRTB . Text = de ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + PublicVar . openFileName ;
2018-12-25 15:29:29 +00:00
string cc2 = CustomRTB . Text . Length . ToString ( CultureInfo . InvariantCulture ) ;
CustomRTB . Select ( Convert . ToInt32 ( cc2 ) , 0 ) ;
CustomRTB . SelectionStart = caretPos ;
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
Show ( ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
catch ( Exception ex )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( ex is CryptographicException )
{
TypedPassword . Value = null ;
2019-01-07 10:08:47 +00:00
DialogResult dialogResult = MessageBox . Show ( "Invalid key!" , PublicVar . appName , MessageBoxButtons . RetryCancel , MessageBoxIcon . Error ) ;
2018-12-25 15:29:29 +00:00
if ( dialogResult = = DialogResult . Retry )
{
AutoLock ( false ) ;
}
if ( dialogResult = = DialogResult . Cancel )
{
PublicVar . encryptionKey . Set ( null ) ;
CustomRTB . Clear ( ) ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName ;
2018-12-25 15:29:29 +00:00
filePath = "" ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = null ;
2018-12-25 15:29:29 +00:00
Show ( ) ;
return ;
}
}
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
protected override void WndProc ( ref Message m )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
const int WM_SYSCOMMAND = 0x112 ;
const int SC_MINIMIZE = 0xF020 ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( m . Msg = = WM_SYSCOMMAND & & m . WParam . ToInt32 ( ) = = SC_MINIMIZE & & ps . AutoLock & & PublicVar . encryptionKey . Get ( ) ! = null )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( ps . AutoSave )
{
SaveToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
else
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
SaveConfirm ( false ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
AutoLock ( true ) ;
return ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
base . WndProc ( ref m ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-29 16:26:51 +00:00
private void MenuIcons ( )
{
if ( ps . MenuIcons )
{
NewToolStripMenuItem . Image = Properties . Resources . document_plus ;
OpenToolStripMenuItem . Image = Properties . Resources . folder_open_document ;
SaveToolStripMenuItem . Image = Properties . Resources . disk_return_black ;
SaveAsToolStripMenuItem . Image = Properties . Resources . disks_black ;
OpenFileLocationToolStripMenuItem . Image = Properties . Resources . folder_horizontal ;
DeleteFileToolStripMenuItem . Image = Properties . Resources . document_minus ;
ExitToolStripMenuItem . Image = Properties . Resources . cross_button ;
UndoToolStripMenuItem . Image = Properties . Resources . arrow_left ;
RedoToolStripMenuItem . Image = Properties . Resources . arrow_right ;
CutToolStripMenuItem . Image = Properties . Resources . scissors ;
CopyToolStripMenuItem . Image = Properties . Resources . document_copy ;
PasteToolStripMenuItem . Image = Properties . Resources . clipboard ;
DeleteToolStripMenuItem . Image = Properties . Resources . minus ;
FindToolStripMenuItem . Image = Properties . Resources . magnifier ;
SelectAllToolStripMenuItem . Image = Properties . Resources . selection_input ;
WordWrapToolStripMenuItem . Image = Properties . Resources . wrap_option ;
ClearToolStripMenuItem . Image = Properties . Resources . document ;
ChangeKeyToolStripMenuItem . Image = Properties . Resources . key ;
LockToolStripMenuItem . Image = Properties . Resources . lock_warning ;
SettingsToolStripMenuItem . Image = Properties . Resources . gear ;
DocumentationToolStripMenuItem . Image = Properties . Resources . document_text ;
CheckForUpdatesToolStripMenuItem . Image = Properties . Resources . upload_cloud ;
AboutToolStripMenuItem . Image = Properties . Resources . information ;
}
else
{
foreach ( ToolStripItem item in MainMenu . Items )
{
if ( item is ToolStripDropDownItem )
foreach ( ToolStripItem dropDownItem in ( ( ToolStripDropDownItem ) item ) . DropDownItems )
{
dropDownItem . Image = null ;
}
}
}
}
2018-12-25 15:29:29 +00:00
/*Functions*/
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
/*Form Events*/
private void MainWindow_Activated ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( PublicVar . settingsChanged )
2018-12-17 11:16:17 +00:00
{
PublicVar . settingsChanged = false ;
2018-12-25 15:29:29 +00:00
CustomRTB . Font = new Font ( ps . RichTextFont , ps . RichTextSize ) ;
CustomRTB . ForeColor = ps . RichForeColor ;
CustomRTB . BackColor = ps . RichBackColor ;
BackColor = ps . RichBackColor ;
2018-12-17 11:16:17 +00:00
2019-08-21 09:23:51 +00:00
LineNumbers_For_RichTextBox . Visible = bool . Parse ( ps . LNVisible ) ;
LineNumbers_For_RichTextBox . BackColor = ps . LNBackgroundColor ;
LineNumbers_For_RichTextBox . ForeColor = ps . LNFontColorPanel ;
LineNumbers_For_RichTextBox . Font = new Font ( ps . RichTextFont , ps . RichTextSize ) ;
LineNumbers_For_RichTextBox . Show_BorderLines = bool . Parse ( ps . BLShow ) ;
LineNumbers_For_RichTextBox . BorderLines_Color = ps . BLColor ;
LineNumbers_For_RichTextBox . BorderLines_Style = ps . BLStyle ;
LineNumbers_For_RichTextBox . Show_GridLines = bool . Parse ( ps . GLShow ) ;
LineNumbers_For_RichTextBox . GridLines_Color = ps . GLColor ;
LineNumbers_For_RichTextBox . GridLines_Style = ps . GLStyle ;
2019-08-21 09:09:05 +00:00
if ( ps . InserKey = = "Disable" )
{
insertToolStripMenuItem . ShortcutKeys = Keys . Insert ;
}
else
{
insertToolStripMenuItem . ShortcutKeys = Keys . None ;
}
2019-08-21 09:23:51 +00:00
2018-12-17 11:16:17 +00:00
if ( ps . SendTo )
{
SendToShortcut ( ) ;
}
else
{
string shortcutPath = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) + @"\Microsoft\Windows\SendTo\Crypto Notepad.lnk" ;
if ( File . Exists ( shortcutPath ) )
{
File . Delete ( shortcutPath ) ;
}
}
#region workaround , unhighlight URLs fix
2018-12-25 15:29:29 +00:00
CustomRTB . DetectUrls = false ;
CustomRTB . DetectUrls = true ;
2018-12-17 11:16:17 +00:00
#endregion
2018-12-25 15:29:29 +00:00
if ( ! ps . ShowToolbar & & ToolbarPanel . Visible )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
ToolbarPanel . Visible = false ;
CustomRTB . Height + = 23 ;
CustomRTB . Location = new Point ( 0 , 24 ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
if ( ps . ShowToolbar & & ! ToolbarPanel . Visible )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
ToolbarPanel . Visible = true ;
CustomRTB . Height - = 23 ;
CustomRTB . Location = new Point ( 0 , 47 ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-29 16:26:51 +00:00
MenuIcons ( ) ;
if ( ps . ColoredToolbar )
{
ToolbarPanel . BackColor = ps . RichBackColor ;
ToolbarPanel . BorderStyle = BorderStyle . FixedSingle ;
}
else
{
ToolbarPanel . BackColor = SystemColors . ButtonFace ;
ToolbarPanel . BorderStyle = BorderStyle . None ;
}
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
if ( PublicVar . keyChanged )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
CustomRTB . Modified = true ;
2018-12-17 11:16:17 +00:00
}
if ( PublicVar . encryptionKey . Get ( ) = = null )
{
2018-12-25 15:29:29 +00:00
FileLocationToolbarButton . Enabled = false ;
DeleteFileToolbarButton . Enabled = false ;
ChangeKeyToolbarButton . Enabled = false ;
2019-01-04 12:14:12 +00:00
LockToolbarButton . Enabled = false ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
else
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
FileLocationToolbarButton . Enabled = true ;
DeleteFileToolbarButton . Enabled = true ;
ChangeKeyToolbarButton . Enabled = true ;
2019-01-04 12:14:12 +00:00
LockToolbarButton . Enabled = true ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void MainWindow_FormClosing ( object sender , FormClosingEventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( WindowState = = FormWindowState . Normal )
{
ps . WindowSize = Size ;
ps . WindowLocation = Location ;
ps . WindowState = WindowState ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( WindowState = = FormWindowState . Maximized )
{
ps . WindowState = WindowState ;
}
ps . Save ( ) ;
SaveConfirm ( true ) ;
2019-08-19 21:09:52 +00:00
if ( CustomRTB . Text = = "" )
{
noExit = false ;
}
2018-12-25 15:29:29 +00:00
if ( noExit )
{
e . Cancel = true ;
}
2018-12-17 11:16:17 +00:00
}
2018-12-29 16:26:51 +00:00
2018-12-25 15:29:29 +00:00
private void MainWindow_Load ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
string pos = ps . WindowLocation . ToString ( ) ;
CustomRTB . Font = new Font ( ps . RichTextFont , ps . RichTextSize ) ;
CustomRTB . ForeColor = ps . RichForeColor ;
CustomRTB . BackColor = ps . RichBackColor ;
BackColor = ps . RichBackColor ;
WordWrapToolStripMenuItem . Checked = ps . MenuWrap ;
CustomRTB . WordWrap = ps . RichWrap ;
ToolbarPanel . Visible = ps . ShowToolbar ;
2019-08-21 09:23:51 +00:00
LineNumbers_For_RichTextBox . Visible = bool . Parse ( ps . LNVisible ) ;
LineNumbers_For_RichTextBox . ForeColor = ps . LNFontColorPanel ;
LineNumbers_For_RichTextBox . BackColor = ps . LNBackgroundColor ;
LineNumbers_For_RichTextBox . Show_BorderLines = bool . Parse ( ps . BLShow ) ;
LineNumbers_For_RichTextBox . BorderLines_Color = ps . BLColor ;
LineNumbers_For_RichTextBox . BorderLines_Style = ps . BLStyle ;
LineNumbers_For_RichTextBox . Show_GridLines = bool . Parse ( ps . GLShow ) ;
LineNumbers_For_RichTextBox . GridLines_Color = ps . GLColor ;
LineNumbers_For_RichTextBox . GridLines_Style = ps . GLStyle ;
LineNumbers_For_RichTextBox . Font = new Font ( ps . RichTextFont , ps . RichTextSize ) ;
2018-12-25 15:29:29 +00:00
2019-08-21 09:09:05 +00:00
if ( ps . InserKey = = "Disable" )
{
insertToolStripMenuItem . ShortcutKeys = Keys . Insert ;
}
else
{
insertToolStripMenuItem . ShortcutKeys = Keys . None ;
}
2018-12-29 16:26:51 +00:00
if ( ps . ColoredToolbar )
{
ToolbarPanel . BackColor = ps . RichBackColor ;
ToolbarPanel . BorderStyle = BorderStyle . FixedSingle ;
}
2018-12-25 15:29:29 +00:00
if ( ! ps . ShowToolbar )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
ToolbarPanel . Visible = false ;
CustomRTB . Height + = 23 ;
CustomRTB . Location = new Point ( 0 , 24 ) ;
}
else
{
ToolbarPanel . Visible = true ;
}
if ( ps . AutoCheckUpdate )
{
Thread up = new Thread ( ( ) = > CheckForUpdates ( false ) ) ;
up . Start ( ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-29 16:26:51 +00:00
MenuIcons ( ) ;
2018-12-25 15:29:29 +00:00
DeleteUpdateFiles ( ) ;
2019-08-19 19:35:20 +00:00
if ( args . Length > 1 ) /*drag & drop to executable*/
{
OpenAsotiations ( ) ;
}
2018-12-25 15:29:29 +00:00
if ( args . Contains ( "/s" ) ) /*send to*/
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
foreach ( var arg in args )
{
argsPath = arg ;
}
SendTo ( ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
if ( args . Contains ( "/o" ) ) /*decrypt & open cnp*/
{
OpenAsotiations ( ) ;
}
if ( args . Contains ( "/e" ) ) /*encrypt*/
{
ContextMenuEncrypt ( ) ;
}
if ( args . Contains ( "/er" ) ) /*encrypt and replace*/
{
ContextMenuEncryptReplace ( ) ;
}
if ( pos ! = "{X=0,Y=0}" )
{
Location = ps . WindowLocation ;
}
Size = ps . WindowSize ;
WindowState = ps . WindowState ;
#if DEBUG
DebugToolStripMenuItem . Visible = true ;
#endif
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
/*Form Events*/
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
/*CustomRTB Events*/
2019-08-19 19:46:14 +00:00
private void CustomRTB_SelectionChanged ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( CustomRTB . SelectionLength ! = 0 )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
CutToolbarButton . Enabled = true ;
CopyToolbarButton . Enabled = true ;
2018-12-17 11:16:17 +00:00
}
else
{
2018-12-25 15:29:29 +00:00
CutToolbarButton . Enabled = false ;
CopyToolbarButton . Enabled = false ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void CustomRTB_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
caretPos = CustomRTB . SelectionStart ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void CustomRTB_KeyDown ( object sender , KeyEventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
caretPos = CustomRTB . SelectionStart ;
if ( e . KeyCode = = Keys . ShiftKey )
{
shiftPresed = true ;
}
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void С ustomRTB_LinkClicked ( object sender , LinkClickedEventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( shiftPresed )
{
shiftPresed = false ;
Process . Start ( e . LinkText ) ;
}
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void CustomRTB_DragDrop ( object sender , DragEventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
SaveConfirm ( false ) ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
string [ ] FileList = ( string [ ] ) e . Data . GetData ( DataFormats . FileDrop , false ) ;
foreach ( string file in FileList ) OpenFile . FileName = file ;
object fname = e . Data . GetData ( "FileDrop" ) ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( OpenFile . FileName ) ;
2018-12-25 15:29:29 +00:00
if ( fname ! = null )
{
var list = fname as string [ ] ;
if ( list ! = null & & ! string . IsNullOrWhiteSpace ( list [ 0 ] ) )
{
if ( ! OpenFile . FileName . Contains ( ".cnp" ) )
{
string opnfile = File . ReadAllText ( OpenFile . FileName ) ;
string NameWithotPath = Path . GetFileName ( OpenFile . FileName ) ;
CustomRTB . Text = opnfile ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-25 15:29:29 +00:00
string cc2 = CustomRTB . Text . Length . ToString ( CultureInfo . InvariantCulture ) ;
CustomRTB . Select ( Convert . ToInt32 ( cc2 ) , 0 ) ;
filePath = OpenFile . FileName ;
return ;
}
DecryptAES ( ) ;
if ( PublicVar . okPressed )
{
PublicVar . okPressed = false ;
}
}
}
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void CustomRTB_KeyUp ( object sender , KeyEventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( e . KeyCode = = Keys . ShiftKey )
{
shiftPresed = false ;
}
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
/*CustomRTB Events*/
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
/* Main Menu */
/*File*/
private void NewToolStripMenuItem_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
SaveConfirm ( false ) ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = "Unnamed.cnp" ;
2018-12-25 15:29:29 +00:00
EnterKeyForm f2 = new EnterKeyForm ( ) ;
f2 . ShowDialog ( ) ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( ! PublicVar . okPressed )
{
2019-01-07 10:09:52 +00:00
if ( filePath ! = "" )
{
PublicVar . openFileName = Path . GetFileName ( filePath ) ;
}
2018-12-25 15:29:29 +00:00
TypedPassword . Value = null ;
return ;
}
else
{
2019-01-07 10:09:52 +00:00
SaveFile . FileName = "Unnamed.cnp" ;
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
2018-12-25 15:29:29 +00:00
PublicVar . okPressed = false ;
if ( SaveFile . ShowDialog ( ) ! = DialogResult . OK )
{
TypedPassword . Value = null ;
return ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
CustomRTB . Clear ( ) ;
StreamWriter sw = new StreamWriter ( SaveFile . FileName ) ;
string NameWithotPath = Path . GetFileName ( SaveFile . FileName ) ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-25 15:29:29 +00:00
filePath = SaveFile . FileName ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( SaveFile . FileName ) ;
2018-12-25 15:29:29 +00:00
sw . Close ( ) ;
}
TypedPassword . Value = null ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void OpenToolStripMenuItem_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
OpenFile . FileName = "" ;
SaveConfirm ( false ) ;
if ( OpenFile . ShowDialog ( ) ! = DialogResult . OK ) return ;
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
PublicVar . openFileName = Path . GetFileName ( OpenFile . FileName ) ;
if ( ! OpenFile . FileName . Contains ( ".cnp" ) )
{
string opnfile = File . ReadAllText ( OpenFile . FileName ) ;
string NameWithotPath = Path . GetFileName ( OpenFile . FileName ) ;
CustomRTB . Text = opnfile ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + NameWithotPath ;
2018-12-25 15:29:29 +00:00
string cc2 = CustomRTB . Text . Length . ToString ( CultureInfo . InvariantCulture ) ;
CustomRTB . Select ( Convert . ToInt32 ( cc2 ) , 0 ) ;
return ;
}
DecryptAES ( ) ;
#region workaround , strange behavior with the cursor in customRTB fix
CustomRTB . DetectUrls = false ;
CustomRTB . DetectUrls = true ;
#endregion
CustomRTB . Modified = false ;
if ( PublicVar . okPressed )
{
PublicVar . okPressed = false ;
}
2018-12-29 16:26:51 +00:00
if ( PublicVar . encryptionKey . Get ( ) = = null )
{
FileLocationToolbarButton . Enabled = false ;
DeleteFileToolbarButton . Enabled = false ;
ChangeKeyToolbarButton . Enabled = false ;
2019-01-04 12:14:12 +00:00
LockToolbarButton . Enabled = false ;
2018-12-29 16:26:51 +00:00
}
else
{
FileLocationToolbarButton . Enabled = true ;
DeleteFileToolbarButton . Enabled = true ;
ChangeKeyToolbarButton . Enabled = true ;
2019-01-04 12:14:12 +00:00
LockToolbarButton . Enabled = true ;
2018-12-29 16:26:51 +00:00
}
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void SaveToolStripMenuItem_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
int saveCaret = CustomRTB . SelectionStart ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
if ( PublicVar . encryptionKey . Get ( ) = = null )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
SaveAsToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
if ( ! PublicVar . okPressed )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
return ;
}
PublicVar . okPressed = false ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
string noenc = CustomRTB . Text ;
string en ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
en = AES . Encrypt ( CustomRTB . Text , PublicVar . encryptionKey . Get ( ) , null , ps . HashAlgorithm , ps . PasswordIterations , ps . KeySize ) ;
CustomRTB . Text = en ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
StreamWriter sw = new StreamWriter ( filePath ) ;
int i = CustomRTB . Lines . Count ( ) ;
int j = 0 ;
i = i - 1 ;
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
while ( j < = i )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
sw . WriteLine ( CustomRTB . Lines . GetValue ( j ) . ToString ( ) ) ;
j = j + 1 ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
sw . Close ( ) ;
CustomRTB . Text = noenc ;
CustomRTB . Select ( Convert . ToInt32 ( saveCaret ) , 0 ) ;
PublicVar . keyChanged = false ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void SaveAsToolStripMenuItem_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
int saveCaret = CustomRTB . SelectionStart ;
string NameWithotPath = Path . GetFileName ( OpenFile . FileName ) ;
2019-01-07 10:09:52 +00:00
if ( filePath ! = "" )
{
PublicVar . openFileName = Path . GetFileName ( filePath ) ;
2019-01-07 10:32:23 +00:00
SaveFile . FileName = Path . GetFileName ( filePath ) ;
2019-01-07 10:09:52 +00:00
}
else
{
PublicVar . openFileName = "Unnamed.cnp" ;
2019-01-07 10:32:23 +00:00
SaveFile . FileName = "Unnamed.cnp" ;
2019-01-07 10:09:52 +00:00
}
2018-12-17 11:16:17 +00:00
EnterKeyForm f2 = new EnterKeyForm ( ) ;
2018-12-25 15:29:29 +00:00
if ( string . IsNullOrEmpty ( PublicVar . encryptionKey . Get ( ) ) )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
f2 . ShowDialog ( ) ;
if ( ! PublicVar . okPressed )
{
return ;
}
PublicVar . okPressed = false ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
if ( SaveFile . ShowDialog ( ) ! = DialogResult . OK )
2018-12-17 11:16:17 +00:00
{
return ;
}
2018-12-25 15:29:29 +00:00
if ( TypedPassword . Value = = null )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
TypedPassword . Value = PublicVar . encryptionKey . Get ( ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
filePath = SaveFile . FileName ;
string noenc = CustomRTB . Text ;
string en ;
en = AES . Encrypt ( CustomRTB . Text , TypedPassword . Value , null , ps . HashAlgorithm , ps . PasswordIterations , ps . KeySize ) ;
CustomRTB . Text = en ;
StreamWriter sw = new StreamWriter ( filePath ) ;
int i = CustomRTB . Lines . Count ( ) ;
int j = 0 ;
i = i - 1 ;
while ( j < = i )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
sw . WriteLine ( CustomRTB . Lines . GetValue ( j ) . ToString ( ) ) ;
j = j + 1 ;
}
sw . Close ( ) ;
CustomRTB . Text = noenc ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName + " – " + Path . GetFileName ( filePath ) ;
2018-12-25 15:29:29 +00:00
CustomRTB . Select ( Convert . ToInt32 ( saveCaret ) , 0 ) ;
PublicVar . encryptionKey . Set ( TypedPassword . Value ) ;
TypedPassword . Value = null ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = Path . GetFileName ( SaveFile . FileName ) ;
2018-12-25 15:29:29 +00:00
}
private void OpenFileLocationToolStripMenuItem_Click ( object sender , EventArgs e )
{
Process . Start ( "explorer.exe" , @"/select, " + filePath ) ;
}
private void DeleteFileToolStripMenuItem_Click ( object sender , EventArgs e )
{
if ( filePath ! = "" )
{
using ( new CenterWinDialog ( this ) )
2018-12-17 11:16:17 +00:00
{
2019-01-07 10:08:47 +00:00
if ( MessageBox . Show ( "Delete file: " + "\"" + filePath + "\"" + " ?" , PublicVar . appName , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) = = DialogResult . Yes )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
File . Delete ( filePath ) ;
CustomRTB . Clear ( ) ;
2018-12-17 11:16:17 +00:00
PublicVar . encryptionKey . Set ( null ) ;
2018-12-25 15:29:29 +00:00
FileLocationToolbarButton . Enabled = false ;
DeleteFileToolbarButton . Enabled = false ;
ChangeKeyToolbarButton . Enabled = false ;
2019-01-04 12:14:12 +00:00
LockToolbarButton . Enabled = false ;
2018-12-19 22:56:59 +00:00
filePath = "" ;
2019-01-07 10:09:52 +00:00
PublicVar . openFileName = null ;
2019-01-07 10:08:47 +00:00
Text = PublicVar . appName ;
2018-12-17 11:16:17 +00:00
return ;
}
}
}
}
2018-12-25 15:29:29 +00:00
private void ExitToolStripMenuItem_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
Application . Exit ( ) ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
private void FileToolStripMenuItem_DropDownOpened ( object sender , EventArgs e )
{
2019-01-07 10:09:52 +00:00
if ( filePath = = "" )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
OpenFileLocationToolStripMenuItem . Enabled = false ;
DeleteFileToolStripMenuItem . Enabled = false ;
}
else
{
OpenFileLocationToolStripMenuItem . Enabled = true ;
DeleteFileToolStripMenuItem . Enabled = true ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
/*File*/
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
/*Edit*/
private void UndoToolStripMenuItem_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
CustomRTB . Undo ( ) ;
}
public void RedoToolStripMenuItem_Click ( object sender , EventArgs e )
{
CustomRTB . Redo ( ) ;
}
private void CutToolStripMenuItem_Click ( object sender , EventArgs e )
{
CustomRTB . Cut ( ) ;
}
private void CopyToolStripMenuItem_Click ( object sender , EventArgs e )
{
CustomRTB . Copy ( ) ;
}
private void PasteToolStripMenuItem_Click ( object sender , EventArgs e )
{
if ( CustomRTB . Focused )
{
CustomRTB . Paste ( DataFormats . GetFormat ( DataFormats . Text ) ) ;
}
if ( SearchTextBox . Focused )
{
SearchTextBox . Paste ( ) ;
}
}
private void DeleteToolStripMenuItem_Click ( object sender , EventArgs e )
{
CustomRTB . SelectedText = "" ;
}
private void FindToolStripMenuItem_Click ( object sender , EventArgs e )
{
if ( SearchPanel . Visible )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
SearchTextBox . Text = "" ;
SearchPanel . Visible = false ;
CustomRTB . Height + = 27 ;
CustomRTB . Focus ( ) ;
CustomRTB . DeselectAll ( ) ;
CustomRTB . SelectionStart = caretPos ;
2018-12-17 11:16:17 +00:00
}
else
{
2018-12-25 15:29:29 +00:00
SearchPanel . Visible = true ;
SearchTextBox . Focus ( ) ;
CustomRTB . Height - = 27 ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void SelectAllToolStripMenuItem_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( CustomRTB . Focused )
{
CustomRTB . SelectAll ( ) ;
}
if ( SearchTextBox . Focused )
{
SearchTextBox . SelectAll ( ) ;
}
}
private void WordWrapToolStripMenuItem_Click ( object sender , EventArgs e )
{
if ( WordWrapToolStripMenuItem . Checked )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
CustomRTB . WordWrap = true ;
2018-12-17 11:16:17 +00:00
}
else
{
2018-12-25 15:29:29 +00:00
CustomRTB . WordWrap = false ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
ps . MenuWrap = WordWrapToolStripMenuItem . Checked ;
ps . RichWrap = CustomRTB . WordWrap ;
ps . Save ( ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void ClearToolStripMenuItem_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
CustomRTB . Clear ( ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void EditToolStripMenuItem_DropDownOpened ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
if ( CustomRTB . SelectionLength ! = 0 )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
CutToolStripMenuItem . Enabled = true ;
CopyToolStripMenuItem . Enabled = true ;
DeleteToolStripMenuItem . Enabled = true ;
}
else
{
CutToolStripMenuItem . Enabled = false ;
CopyToolStripMenuItem . Enabled = false ;
DeleteToolStripMenuItem . Enabled = false ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
/*Edit*/
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
/*Tools*/
private void ChangeKeyToolStripMenuItem_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
ChangeKeyForm c = new ChangeKeyForm ( ) ;
c . ShowDialog ( this ) ;
}
private void LockToolStripMenuItem_Click ( object sender , EventArgs e )
{
if ( ps . AutoSave )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
SaveToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
else
{
SaveConfirm ( false ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
AutoLock ( false ) ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void SettingsToolStripMenuItem_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
SettingsForm sf = new SettingsForm ( ) ;
sf . ShowDialog ( ) ;
}
private void ToolsToolStripMenuItem_DropDownOpened ( object sender , EventArgs e )
{
if ( PublicVar . encryptionKey . Get ( ) = = null )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
ChangeKeyToolStripMenuItem . Enabled = false ;
LockToolStripMenuItem . Enabled = false ;
}
else
{
ChangeKeyToolStripMenuItem . Enabled = true ;
LockToolStripMenuItem . Enabled = true ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
}
/*Tools*/
/*Help*/
private void DocumentationToolStripMenuItem_Click ( object sender , EventArgs e )
{
2019-01-02 19:10:41 +00:00
Process . Start ( "https://github.com/Crypto-Notepad/Crypto-Notepad/wiki/Documentation" ) ;
2018-12-25 15:29:29 +00:00
}
private void CheckForUpdatesToolStripMenuItem_Click ( object sender , EventArgs e )
{
Thread up = new Thread ( ( ) = > CheckForUpdates ( true ) ) ;
up . Start ( ) ;
}
private void AboutToolStripMenuItem_Click ( object sender , EventArgs e )
{
AboutFrom a = new AboutFrom ( ) ;
a . ShowDialog ( this ) ;
}
/*Help*/
/* Main Menu */
/* Editor Menu */
private void UndoEditorMenuStrip_Click ( object sender , EventArgs e )
{
UndoToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void RedoEditorMenuStrip_Click ( object sender , EventArgs e )
{
RedoToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void CutEditorMenuStrip_Click ( object sender , EventArgs e )
{
CutToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void CopyEditorMenuStrip_Click ( object sender , EventArgs e )
{
CopyToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void PasteEditorMenuStrip_Click ( object sender , EventArgs e )
{
PasteToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void DeleteEditorMenuStrip_Click ( object sender , EventArgs e )
{
DeleteToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void SelectAllEditorMenuStrip_Click ( object sender , EventArgs e )
{
SelectAllToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:22:59 +00:00
private void RightToLeftEditorMenuStrip_Click ( object sender , EventArgs e )
{
if ( RightToLeftEditorMenuStrip . Checked )
{
if ( ! WordWrapToolStripMenuItem . Checked )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:22:59 +00:00
string rtbTxt = CustomRTB . Text ;
CustomRTB . Clear ( ) ;
CustomRTB . RightToLeft = RightToLeft . Yes ;
Application . DoEvents ( ) ;
CustomRTB . Text = rtbTxt ;
2018-12-25 15:29:29 +00:00
}
2018-12-25 15:22:59 +00:00
else
{
CustomRTB . RightToLeft = RightToLeft . Yes ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:22:59 +00:00
else
{
CustomRTB . RightToLeft = RightToLeft . No ;
}
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
private void ClearEditorMenuStrip_Click ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
ClearToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void EditorMenuStrip_Opening ( object sender , CancelEventArgs e )
{
if ( CustomRTB . SelectionLength ! = 0 )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
CutEditorMenuStrip . Enabled = true ;
CopyEditorMenuStrip . Enabled = true ;
DeleteEditorMenuStrip . Enabled = true ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
else
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
CutEditorMenuStrip . Enabled = false ;
CopyEditorMenuStrip . Enabled = false ;
DeleteEditorMenuStrip . Enabled = false ;
}
}
/* Editor Menu */
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
/*Toolbar*/
private void NewToolbarButton_Click ( object sender , EventArgs e )
{
NewToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
private void OpenToolbarButton_Click ( object sender , EventArgs e )
{
OpenToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void SaveToolbarButton_Click ( object sender , EventArgs e )
{
SaveToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void FileLocationToolbarButton_Click ( object sender , EventArgs e )
{
OpenFileLocationToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void DeleteFileToolbarButton_Click ( object sender , EventArgs e )
{
DeleteFileToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
private void CutToolbarButton_Click ( object sender , EventArgs e )
{
CutToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void CopyToolbarButton_Click ( object sender , EventArgs e )
{
CopyToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void PasteToolbarButton_Click ( object sender , EventArgs e )
{
PasteToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void ChangeKeyToolbarButton_Click ( object sender , EventArgs e )
{
ChangeKeyToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void SettingsToolbarButton_Click ( object sender , EventArgs e )
{
SettingsToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
2019-01-04 12:14:12 +00:00
private void LockToolbarButton_Click ( object sender , EventArgs e )
2018-12-25 15:29:29 +00:00
{
2019-01-04 12:17:03 +00:00
LockToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
2018-12-25 15:29:29 +00:00
private void CloseToolbar_Click ( object sender , EventArgs e )
{
ToolbarPanel . Visible = false ;
2018-12-25 15:15:29 +00:00
CustomRTB . Height + = 23 ;
CustomRTB . Location = new Point ( 0 , 24 ) ;
2018-12-25 15:29:29 +00:00
ps . ShowToolbar = false ;
ps . Save ( ) ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
private void CloseToolbar_MouseEnter ( object sender , EventArgs e )
{
CloseToolbar . Image = Properties . Resources . close_b ;
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:29:29 +00:00
private void CloseToolbar_MouseLeave ( object sender , EventArgs e )
{
CloseToolbar . Image = Properties . Resources . close_g ;
2018-12-17 11:16:17 +00:00
}
2018-12-25 15:29:29 +00:00
/*Toolbar*/
/*Search Panel*/
private void SearchTextBox_TextChanged ( object sender , EventArgs e )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
bool isexist = CustomRTB . Highlight ( SearchTextBox . Text , ps . HighlightsColor , chkMatchCase . Checked , chkMatchWholeWord . Checked ) ;
}
private void SearchTextBox_KeyDown ( object sender , KeyEventArgs e )
{
if ( e . KeyCode = = Keys . Escape )
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:29:29 +00:00
SearchTextBox . Text = "" ;
SearchPanel . Visible = false ;
CustomRTB . Height + = 27 ;
CustomRTB . Focus ( ) ;
CustomRTB . DeselectAll ( ) ;
CustomRTB . SelectionStart = caretPos ;
e . Handled = e . SuppressKeyPress = true ;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:29:29 +00:00
private void CloseSearchPanel_Click ( object sender , EventArgs e )
{
FindToolStripMenuItem_Click ( this , new EventArgs ( ) ) ;
}
private void CloseSearchPanel_MouseHover ( object sender , EventArgs e )
{
CloseSearchPanel . Image = Properties . Resources . close_b ;
}
private void CloseSearchPanel_MouseLeave ( object sender , EventArgs e )
{
CloseSearchPanel . Image = Properties . Resources . close_g ;
}
private void ChkMatchCase_CheckedChanged ( object sender , EventArgs e )
{
bool isexist = CustomRTB . Highlight ( SearchTextBox . Text , ps . HighlightsColor , chkMatchCase . Checked , chkMatchWholeWord . Checked ) ;
}
private void ChkMatchWholeWord_CheckedChanged ( object sender , EventArgs e )
{
bool isexist = CustomRTB . Highlight ( SearchTextBox . Text , ps . HighlightsColor , chkMatchCase . Checked , chkMatchWholeWord . Checked ) ;
}
/*Search Panel*/
/*Debug Menu*/
2018-12-26 22:29:00 +00:00
private void MainVariablesToolStripMenuItem_Click ( object sender , EventArgs e )
{
2018-12-28 11:11:02 +00:00
#if DEBUG
2019-01-07 10:32:23 +00:00
string formattedTime = DateTime . Now . ToString ( "yyyy.MM.dd hh:mm:ss" ) ;
2019-01-07 10:09:14 +00:00
Debug . WriteLine ( "\nTime: " + formattedTime ) ;
Debug . WriteLine ( "PublicVar.openFileName: " + PublicVar . openFileName ) ;
Debug . WriteLine ( "filePath: " + filePath ) ;
2018-12-26 22:29:00 +00:00
Debug . WriteLine ( "encryptionKey: " + PublicVar . encryptionKey . Get ( ) ) ;
Debug . WriteLine ( "TypedPassword: " + TypedPassword . Value ) ;
Debug . WriteLine ( "noExit: " + noExit ) ;
Debug . WriteLine ( "keyChanged: " + PublicVar . keyChanged ) ;
Debug . WriteLine ( "settingsChanged: " + PublicVar . settingsChanged ) ;
Debug . WriteLine ( "okPressed: " + PublicVar . okPressed ) ;
Debug . WriteLine ( "CustomRTB.Modified: " + CustomRTB . Modified ) ;
2019-08-19 19:46:14 +00:00
Debug . WriteLine ( "EditorMenuStrip: " + EditorMenuStrip . Enabled ) ;
2019-01-07 10:09:14 +00:00
#endif
2018-12-25 15:29:29 +00:00
}
/*Debug Menu*/
2018-12-17 11:16:17 +00:00
}
2018-12-28 11:11:02 +00:00
}