📄 mainform.cs
字号:
cleanForm = true;
}
break;
}
case DialogResult.No:
{
SetText( "" );
currentFileName = "Untitled";
this.Text = "Untitled - RSACryptoPad";
cleanForm = true;
break;
}
case DialogResult.Cancel:
{ break; }
default:
{ break; }
}
}
else
{
SetText( "" );
currentFileName = "Untitled";
this.Text = "Untitled - RSACryptoPad";
}
}
private void openFileMenuItem_Click( object sender, EventArgs e )
{
if( !cleanForm )
{
string dialogText = "The text in the " + currentFileName + " file has changed." + Environment.NewLine + Environment.NewLine + "Do you want to save the changes?";
switch( MessageBox.Show( dialogText, "RSACryptoPad", MessageBoxButtons.YesNoCancel ) )
{
case DialogResult.Yes:
{
if( saveFile() ) { SetText( "" ); }
else
{ return; }
break;
}
case DialogResult.No:
{
SetText( "" );
break;
}
case DialogResult.Cancel:
{ return; }
default:
{ break; }
}
}
try
{
string fileString = openFile( "Open", "Text Document( *.txt )|*.txt|All Files|*.*" );
if( fileString != null )
{
SetText( fileString );
cleanForm = true;
}
}
catch( Exception Ex ) { MessageBox.Show( "ERROR: \n" + Ex.Message ); }
}
private void saveAsFileMenuItem_Click( object sender, EventArgs e )
{
if( saveFile() )
{
cleanForm = true;
this.Text = GetFileName( currentFileName ) + " - RSACryptoPad";
}
}
private void saveMenuItem_Click( object sender, EventArgs e )
{
if( currentFileName.Equals( "Untitled" ) )
{
if( saveFile() ) { cleanForm = true; }
}
else
{
try
{
StreamWriter streamWriter = new StreamWriter( currentFileName );
streamWriter.Write( inputTextBox.Text );
streamWriter.Flush();
streamWriter.Close();
cleanForm = true;
}
catch( Exception Ex )
{ MessageBox.Show( "ERROR: \n" + Ex.Message ); }
}
}
private void exitMenuItem_Click( object sender, EventArgs e )
{
if( !cleanForm )
{
string dialogText = "The text in the " + currentFileName + " file has changed." + Environment.NewLine + Environment.NewLine + "Do you want to save the changes?";
switch( MessageBox.Show( dialogText, "RSACryptoPad", MessageBoxButtons.YesNoCancel ) )
{
case DialogResult.Yes:
{
if( saveFile() ) { Dispose( true ); }
break;
}
case DialogResult.No:
{
Dispose( true );
break;
}
case DialogResult.Cancel:
{ break; }
default:
{ break; }
}
}
else
{ Dispose( true ); }
}
private void undoMenuItem_Click( object sender, EventArgs e )
{ inputTextBox.Undo(); }
private void cutMenuItem_Click( object sender, EventArgs e )
{
try { inputTextBox.Cut(); }
catch( Exception Ex )
{ Console.WriteLine( Ex.Message ); }
}
private void copyMenuItem_Click( object sender, EventArgs e )
{
try { inputTextBox.Copy(); }
catch( Exception Ex ) { Console.WriteLine( Ex.Message ); }
}
private void pasteMenuItem_Click( object sender, EventArgs e )
{
try { inputTextBox.Paste(); }
catch( Exception Ex ) { Console.WriteLine( Ex.Message ); }
}
private void deleteMenuItem_Click( object sender, EventArgs e )
{
if( ( inputTextBox.Text.Length != 0 ) & ( inputTextBox.SelectionStart != inputTextBox.Text.Length ) )
{
if( !inputTextBox.SelectedText.Equals( "" ) )
{
try
{
int cursorPosition = inputTextBox.SelectionStart;
inputTextBox.Text = inputTextBox.Text.Remove( cursorPosition, inputTextBox.SelectedText.Length );
inputTextBox.SelectionStart = cursorPosition;
}
catch( Exception Ex ) { Console.WriteLine( Ex.Message ); }
}
else
{
try
{
int cursorPosition = inputTextBox.SelectionStart;
inputTextBox.Text = inputTextBox.Text.Remove( cursorPosition, 1 );
inputTextBox.SelectionStart = cursorPosition;
}
catch( Exception Ex ) { Console.WriteLine( Ex.Message ); }
}
}
}
private void selectAllmenuItem_Click( object sender, EventArgs e )
{
try { inputTextBox.SelectAll(); }
catch( Exception Ex ) { Console.WriteLine( Ex.Message ); }
}
private void wordWrapMenuItem_Click( object sender, EventArgs e )
{
if( wordWrapMenuItem.Checked == true )
{
wordWrapMenuItem.Checked = false;
inputTextBox.WordWrap = false;
}
else
{
wordWrapMenuItem.Checked = true;
inputTextBox.WordWrap = true;
}
SaveSettings( "WRAPPING" );
}
private void fontMenuItem_Click( object sender, EventArgs e )
{
if( fontDialog.ShowDialog() == DialogResult.OK )
{
inputTextBox.Font = fontDialog.Font;
SaveSettings( "FONT" );
}
}
private void decryptMenuItem_Click( object sender, EventArgs e )
{
if( inputTextBox.Text.Length != 0 )
{
openFileDialog.FileName = "";
openFileDialog.Title = "Open Private Key File";
openFileDialog.Filter = "Private Key Document( *.kez )|*.kez";
string fileString = null;
if( openFileDialog.ShowDialog() == DialogResult.OK )
{
if( File.Exists( openFileDialog.FileName ) )
{
StreamReader streamReader = new StreamReader( openFileDialog.FileName, true );
fileString = streamReader.ReadToEnd();
streamReader.Close();
if( fileString.Length >= inputTextBox.MaxLength )
{ MessageBox.Show( "ERROR: \nThe file you are trying to open is too big for the text editor to display properly.\nPlease open a smaller document!\nOperation Aborted!" ); }
}
}
if( File.Exists( openFileDialog.FileName ) )
{
string bitStrengthString = fileString.Substring( 0, fileString.IndexOf( "</BitStrength>" ) + 14 );
fileString = fileString.Replace( bitStrengthString, "" );
int bitStrength = Convert.ToInt32( bitStrengthString.Replace( "<BitStrength>", "" ).Replace( "</BitStrength>", "" ) );
Point point = new Point( ( inputTextBox.Size.Width / 2 ) - ( panel.Size.Width / 2 ), ( inputTextBox.Size.Height / 2 ) - ( panel.Size.Height / 2 ) );
panel.Location = point;
panel.Visible = true;
this.Refresh();
fileMenuItem.Enabled = false;
editMenuItem.Enabled = false;
formatMenuItem.Enabled = false;
encryptionMenuItem.Enabled = false;
helpMenuItem.Enabled = false;
string tempStorage = inputTextBox.Text;
if( fileString != null )
{
FinishedProcessDelegate finishedProcessDelegate = new FinishedProcessDelegate( FinishedProcess );
UpdateTextDelegate updateTextDelegate = new UpdateTextDelegate( UpdateText );
try
{
EncryptionThread decryptionThread = new EncryptionThread();
Thread decryptThread = new Thread( decryptionThread.Decrypt );
decryptThread.IsBackground = true;
decryptThread.Start( new Object[] { this, finishedProcessDelegate, updateTextDelegate, inputTextBox.Text, bitStrength, fileString } );
}
catch( CryptographicException CEx )
{ MessageBox.Show( "ERROR: \nOne of the following has occured.\nThe cryptographic service provider cannot be acquired.\nThe length of the text being encrypted is greater than the maximum allowed length.\nThe OAEP padding is not supported on this computer.\n" + "Exact error: " + CEx.Message ); }
catch( Exception Ex )
{
MessageBox.Show( "ERROR:\n" + Ex.Message );
SetText( tempStorage );
}
}
}
}
else
{ MessageBox.Show( "ERROR: You Can Not Decrypt A NULL Value!!!" ); }
}
private void encryptMenuItem_Click( object sender, EventArgs e )
{
if( inputTextBox.Text.Length != 0 )
{
openFileDialog.FileName = "";
openFileDialog.Title = "Open Public Key File";
openFileDialog.Filter = "Public Key Document( *.pke )|*.pke";
string fileString = null;
if( openFileDialog.ShowDialog() == DialogResult.OK )
{
if( File.Exists( openFileDialog.FileName ) )
{
StreamReader streamReader = new StreamReader( openFileDialog.FileName, true );
fileString = streamReader.ReadToEnd();
streamReader.Close();
if( fileString.Length >= inputTextBox.MaxLength )
{ MessageBox.Show( "ERROR: \nThe file you are trying to open is too big for the text editor to display properly.\nPlease open a smaller document!\nOperation Aborted!" ); }
}
}
if( fileString != null )
{
FinishedProcessDelegate finishedProcessDelegate = new FinishedProcessDelegate( FinishedProcess );
UpdateTextDelegate updateTextDelegate = new UpdateTextDelegate( UpdateText );
string bitStrengthString = fileString.Substring( 0, fileString.IndexOf( "</BitStrength>" ) + 14 );
fileString = fileString.Replace( bitStrengthString, "" );
int bitStrength = Convert.ToInt32( bitStrengthString.Replace( "<BitStrength>", "" ).Replace( "</BitStrength>", "" ) );
Point point = new Point( ( inputTextBox.Size.Width / 2 ) - ( panel.Size.Width / 2 ), ( inputTextBox.Size.Height / 2 ) - ( panel.Size.Height / 2 ) );
panel.Location = point;
panel.Visible = true;
this.Refresh();
fileMenuItem.Enabled = false;
editMenuItem.Enabled = false;
formatMenuItem.Enabled = false;
encryptionMenuItem.Enabled = false;
helpMenuItem.Enabled = false;
if( fileString != null )
{
try
{
EncryptionThread encryptionThread = new EncryptionThread();
Thread encryptThread = new Thread( encryptionThread.Encrypt );
encryptThread.IsBackground = true;
encryptThread.Start( new Object[] { this, finishedProcessDelegate, updateTextDelegate, inputTextBox.Text, bitStrength, fileString } );
}
catch( CryptographicException CEx )
{ MessageBox.Show( "ERROR: \nOne of the following has occured.\nThe cryptographic service provider cannot be acquired.\nThe length of the text being encrypted is greater than the maximum allowed length.\nThe OAEP padding is not supported on this computer.\n" + "Exact error: " + CEx.Message ); }
catch( Exception Ex )
{ MessageBox.Show( "ERROR: \n" + Ex.Message ); }
}
}
}
else
{ MessageBox.Show( "ERROR: You Can Not Encrypt A NULL Value!!!" ); }
}
private void generateKeyPairMenuItem_Click( object sender, EventArgs e )
{
KeyPairGeneratorForm generator = new KeyPairGeneratorForm();
if( generator.ShowDialog() == DialogResult.OK )
{
RSACryptoServiceProvider RSAProvider = new RSACryptoServiceProvider( currentBitStrength );
string publicAndPrivateKeys = "<BitStrength>" + currentBitStrength.ToString() + "</BitStrength>" + RSAProvider.ToXmlString( true );
string justPublicKey = "<BitStrength>" + currentBitStrength.ToString() + "</BitStrength>" + RSAProvider.ToXmlString( false );
if( saveFile( "Save Public/Private Keys As", "Public/Private Keys Document( *.kez )|*.kez", publicAndPrivateKeys ) )
{ while( !saveFile( "Save Public Key As", "Public Key Document( *.pke )|*.pke", justPublicKey ) ) { ; } }
}
}
private void aboutMenuItem_Click( object sender, EventArgs e )
{
AboutForm aboutRSACryptoPad = new AboutForm();
aboutRSACryptoPad.ShowDialog( this );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -