📄 mainform.cs
字号:
{inputTextBox.Cut();}
catch( System.Exception Ex ) {System.Console.WriteLine( Ex.Message );}
}
private void copyMenuItem_Click( object sender, System.EventArgs e )
{
try
{inputTextBox.Copy();}
catch( System.Exception Ex ) {System.Console.WriteLine( Ex.Message );}
}
private void pasteMenuItem_Click( object sender, System.EventArgs e )
{
try
{inputTextBox.Paste();}
catch( System.Exception Ex ) {System.Console.WriteLine( Ex.Message );}
}
private void selectAllmenuItem_Click( object sender, System.EventArgs e )
{
try
{inputTextBox.SelectAll();}
catch( System.Exception Ex ) {System.Console.WriteLine( Ex.Message );}
}
private void deleteMenuItem_Click( object sender, System.EventArgs e )
{
if( ( inputTextBox.Text.Length != 0 ) & ( inputTextBox.SelectionStart != inputTextBox.Text.Length ) )
{
if( !inputTextBox.SelectedText.Equals( "" ) )
{
try
{
System.Int32 cursorPosition = inputTextBox.SelectionStart;
inputTextBox.Text = inputTextBox.Text.Remove( cursorPosition, inputTextBox.SelectedText.Length );
inputTextBox.SelectionStart = cursorPosition;
}
catch( System.Exception Ex ) {System.Console.WriteLine( Ex.Message );}
}
else
{
try
{
System.Int32 cursorPosition = inputTextBox.SelectionStart;
inputTextBox.Text = inputTextBox.Text.Remove( cursorPosition, 1 );
inputTextBox.SelectionStart = cursorPosition;
}
catch( System.Exception Ex ) {System.Console.WriteLine( Ex.Message );}
}
}
}
private void mainForm_Load( object sender, System.EventArgs e )
{inputTextBox.Focus();}
private void aboutMenuItem_Click( object sender, System.EventArgs e )
{
aboutForm aboutRSACryptoPad = new aboutForm();
this.AddOwnedForm( aboutRSACryptoPad );
aboutRSACryptoPad.Visible = true;
}
private void generateKeyPairMenuItem_Click( object sender, System.EventArgs e )
{
System.Security.Cryptography.RSACryptoServiceProvider RSAProvider;
RSACryptoPad.keyPairGeneratorSettingsForm generator = new keyPairGeneratorSettingsForm();
generator.ShowDialog( this );
if( currentBitStrength != 0 )
{
if( currentBitStrength == 1024 )
{RSAProvider = new System.Security.Cryptography.RSACryptoServiceProvider( 1024 );}
else if( currentBitStrength == 2048 )
{RSAProvider = new System.Security.Cryptography.RSACryptoServiceProvider( 2048 );}
else
{return;}
System.String publicAndPrivateKeys = RSAProvider.ToXmlString( true );
System.String justPublicKey = RSAProvider.ToXmlString( false );
saveFileDialog.Title = "Save Public/Private Keys As";
saveFileDialog.ValidateNames = true;
saveFileDialog.OverwritePrompt = true;
saveFileDialog.AddExtension = true;
saveFileDialog.DereferenceLinks = true;
saveFileDialog.Filter = "Public/Private Keys Document( *.kez )|*.kez";
saveFileDialog.ValidateNames = true;
saveFileDialog.FileName = "";
saveFileDialog.ShowDialog();
if( !saveFileDialog.FileName.Equals( "" ) )
{
try
{
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter( saveFileDialog.FileName, false );
streamWriter.WriteLine( "DO NOT MODIFY!!!" );
streamWriter.Write( publicAndPrivateKeys );
streamWriter.Close();
}
catch( System.Exception Ex ) {System.Console.WriteLine( Ex.Message );}
}
saveFileDialog.Title = "Save Public Key As";
saveFileDialog.ValidateNames = true;
saveFileDialog.OverwritePrompt = true;
saveFileDialog.AddExtension = true;
saveFileDialog.DereferenceLinks = true;
saveFileDialog.Filter = "Public Key Document( *.pke )|*.pke";
saveFileDialog.ValidateNames = true;
saveFileDialog.FileName = "";
saveFileDialog.ShowDialog();
if( !saveFileDialog.FileName.Equals( "" ) )
{
try
{
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter( saveFileDialog.FileName, false );
streamWriter.WriteLine( "DO NOT MODIFY!!!" );
streamWriter.Write( justPublicKey );
streamWriter.Close();
}
catch( System.Exception Ex ) {System.Console.WriteLine( Ex.Message );}
}
}
}
public static void setBitStrength( System.Int32 bitStrength )
{currentBitStrength = bitStrength;}
private void encryptMenuItem_Click( object sender, System.EventArgs e )
{
if( inputTextBox.Text.Length != 0 )
{
openFileDialog.Title = "Open Public Key File";
openFileDialog.ValidateNames = true;
openFileDialog.DereferenceLinks = true;
openFileDialog.Filter = "Public Keys Document( *.pke )|*.pke";
openFileDialog.FileName = "";
openFileDialog.ShowDialog();
System.Drawing.Point point = new System.Drawing.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( !openFileDialog.FileName.Equals( "" ) )
{
try
{
System.IO.StreamReader streamReader = new System.IO.StreamReader( openFileDialog.FileName, true );
System.String xmlString = streamReader.ReadToEnd();
streamReader.Close();
xmlString = xmlString.Substring( xmlString.IndexOf( "DO NOT MODIFY!!!" ) + 18 );
System.Security.Cryptography.RSACryptoServiceProvider RSAProvider = new System.Security.Cryptography.RSACryptoServiceProvider();
System.Security.Cryptography.RSAParameters parameters = new System.Security.Cryptography.RSAParameters();
RSAProvider.FromXmlString( xmlString );
System.Int32 numberOfBlocks =( inputTextBox.Text.Length / 32 ) + 1;
System.Char[] charArray = inputTextBox.Text.ToCharArray();
System.Byte[][] byteBlockArray = new byte[ numberOfBlocks ][];
System.Int32 incrementer = 0;
for( System.Int32 i = 1; i <= numberOfBlocks; i++ )
{
if( i == numberOfBlocks )
{byteBlockArray[ i - 1 ] = System.Text.ASCIIEncoding.ASCII.GetBytes( charArray, incrementer, charArray.Length - incrementer );}
else
{
byteBlockArray[ i - 1 ] = System.Text.ASCIIEncoding.ASCII.GetBytes( charArray, incrementer, 32 );
incrementer += 32;
}
}
System.String tempStorage = inputTextBox.Text;
inputTextBox.Text = "";
for( System.Int32 j = 0; j < byteBlockArray.Length; j++ )
{
if( inputTextBox.Text.Length >= inputTextBox.MaxLength )
{
System.Windows.Forms.MessageBox.Show( "ERROR: \nThe amount of text you are trying to encrypt is too large for the text editor to display.\nPlease encrypt a smaller portion of text!\nOperation Aborted!" );
inputTextBox.Text = tempStorage;
if( tempStorage.Equals( inputTextBox.Text ) )
{originalText = true;}
break;
}
inputTextBox.AppendText( System.Convert.ToBase64String( RSAProvider.Encrypt( byteBlockArray[ j ], true ) ) );
}
}
catch( System.Security.Cryptography.CryptographicException CEx )
{System.Windows.Forms.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( System.Exception Ex ){System.Windows.Forms.MessageBox.Show( "ERROR: \n" + Ex.Message );}
}
panel.Visible = false;
fileMenuItem.Enabled = true;
editMenuItem.Enabled = true;
formatMenuItem.Enabled = true;
encryptionMenuItem.Enabled = true;
helpMenuItem.Enabled = true;
inputTextBox.Enabled = true;
}
else
{System.Windows.Forms.MessageBox.Show( "ERROR: You Can Not Encrypt A NULL Value!!!" );}
}
private void decryptMenuItem_Click( object sender, System.EventArgs e )
{
if( inputTextBox.Text.Length != 0 )
{
openFileDialog.Title = "Open Key File Containing Private Key";
openFileDialog.ValidateNames = true;
openFileDialog.DereferenceLinks = true;
openFileDialog.Filter = "Public/Private Keys Document( *.kez )|*.kez";
openFileDialog.FileName = "";
openFileDialog.ShowDialog();
System.Drawing.Point point = new System.Drawing.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;
System.String tempStorage = inputTextBox.Text;
if( !openFileDialog.FileName.Equals( "" ) )
{
try
{
System.IO.StreamReader streamReader = new System.IO.StreamReader( openFileDialog.FileName, true );
System.String xmlString = streamReader.ReadToEnd();
streamReader.Close();
xmlString = xmlString.Substring( xmlString.IndexOf( "</KeySize>" ) + 12 );
System.Security.Cryptography.RSACryptoServiceProvider RSAProvider = new System.Security.Cryptography.RSACryptoServiceProvider();
System.Security.Cryptography.RSAParameters parameters = new System.Security.Cryptography.RSAParameters();
RSAProvider.FromXmlString( xmlString );
System.String encryptedBlock = "";
System.Collections.Queue encryptedBlocks = new System.Collections.Queue();
while( inputTextBox.Text.Length != 0 )
{
if( RSAProvider.KeySize == 1024 )
{
encryptedBlock = inputTextBox.Text.Substring( 0, inputTextBox.Text.IndexOf( "=" ) + 1 );
encryptedBlocks.Enqueue( encryptedBlock );
inputTextBox.Text = inputTextBox.Text.Remove( 0, encryptedBlock.Length );
}
else
{
encryptedBlock = inputTextBox.Text.Substring( 0, inputTextBox.Text.IndexOf( "==" ) + 2 );
encryptedBlocks.Enqueue( encryptedBlock );
inputTextBox.Text = inputTextBox.Text.Remove( 0, encryptedBlock.Length );
}
}
encryptedBlocks.TrimToSize();
System.Int32 numberOfBlocks = encryptedBlocks.Count;
for( System.Int32 i = 1; i <= numberOfBlocks; i++ )
{
encryptedBlock =( System.String )encryptedBlocks.Dequeue();
inputTextBox.AppendText( System.Text.ASCIIEncoding.ASCII.GetString( RSAProvider.Decrypt( System.Convert.FromBase64String( encryptedBlock ), true ) ) );
}
}
catch( System.Exception Ex )
{
System.Windows.Forms.MessageBox.Show( "ERROR:\n" + Ex.Message );
inputTextBox.Text = tempStorage;
}
}
panel.Visible = false;
fileMenuItem.Enabled = true;
editMenuItem.Enabled = true;
formatMenuItem.Enabled = true;
encryptionMenuItem.Enabled = true;
helpMenuItem.Enabled = true;
}
else
{System.Windows.Forms.MessageBox.Show( "ERROR: You Can Not Decrypt A NULL Value!!!" );}
}
private void helpTopicsMenuItem_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show( "This program is basically notepad with an encryption menu." );
System.Windows.Forms.MessageBox.Show( "You generate a key pair by going to Encryption->Generate Key Pair..." );
System.Windows.Forms.MessageBox.Show( "I have confidence you'll figure this out... :-)" );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -