Skip to content

Instantly share code, notes, and snippets.

@mmmunk mmmunk/RubberExample.cs

Last active Feb 13, 2019
Embed
What would you like to do?
C# .NET apps without Visual Studio
// https://www.c-sharpcorner.com/article/aes-encryption-in-c-sharp/
using System;
using System.IO;
using System.Security.Cryptography;
class ManagedAesSample {
public static void Main() {
#if NET40
Console.WriteLine("Target framework: .NET Framework 4.0");
#elif NET45
Console.WriteLine("Target framework: .NET Framework 4.5");
#else
Console.WriteLine("Target framework: .NET Standard 1.4");
#endif
Console.WriteLine("Enter text that needs to be encrypted..");
string data = Console.ReadLine();
EncryptAesManaged(data);
Console.ReadLine();
}
static void EncryptAesManaged(string raw) {
try {
// Create Aes that generates a new key and initialization vector (IV).
// Same key must be used in encryption and decryption
using(AesManaged aes = new AesManaged()) {
// Encrypt string
byte[] encrypted = Encrypt(raw, aes.Key, aes.IV);
// Print encrypted string
Console.WriteLine($"Encrypted data: {System.Text.Encoding.UTF8.GetString(encrypted)}");
// Decrypt the bytes to a string.
string decrypted = Decrypt(encrypted, aes.Key, aes.IV);
// Print decrypted string. It should be same as raw data
Console.WriteLine($"Decrypted data: {decrypted}");
}
} catch (Exception exp) {
Console.WriteLine(exp.Message);
}
Console.ReadKey();
}
static byte[] Encrypt(string plainText, byte[] Key, byte[] IV) {
byte[] encrypted;
// Create a new AesManaged.
using(AesManaged aes = new AesManaged()) {
// Create encryptor
ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);
// Create MemoryStream
using(MemoryStream ms = new MemoryStream()) {
// Create crypto stream using the CryptoStream class. This class is the key to encryption
// and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream
// to encrypt
using(CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) {
// Create StreamWriter and write data to a stream
using(StreamWriter sw = new StreamWriter(cs))
sw.Write(plainText);
encrypted = ms.ToArray();
}
}
}
// Return encrypted data
return encrypted;
}
static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV) {
string plaintext = null;
// Create AesManaged
using(AesManaged aes = new AesManaged()) {
// Create a decryptor
ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
// Create the streams used for decryption.
using(MemoryStream ms = new MemoryStream(cipherText)) {
// Create crypto stream
using(CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) {
// Read crypto stream
using(StreamReader reader = new StreamReader(cs))
plaintext = reader.ReadToEnd();
}
}
}
return plaintext;
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
public class RubberExample : Form {
bool mouseDown = false;
Point mouseDownPoint = Point.Empty;
Point mousePoint = Point.Empty;
protected override void OnMouseDown(MouseEventArgs e) {
base.OnMouseDown(e);
mouseDown = true;
mousePoint = mouseDownPoint = e.Location;
}
protected override void OnMouseUp(MouseEventArgs e) {
base.OnMouseUp(e);
mouseDown = false;
}
protected override void OnMouseMove(MouseEventArgs e) {
base.OnMouseMove(e);
mousePoint = e.Location;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
if (mouseDown) {
Region r = new Region(this.ClientRectangle);
Rectangle window = new Rectangle(
Math.Min(mouseDownPoint.X, mousePoint.X),
Math.Min(mouseDownPoint.Y, mousePoint.Y),
Math.Abs(mouseDownPoint.X - mousePoint.X),
Math.Abs(mouseDownPoint.Y - mousePoint.Y)
);
r.Xor(window);
e.Graphics.FillRegion(Brushes.Black, r);
//Console.WriteLine("Painted: " + window);
}
}
public RubberExample() {
DoubleBuffered = true;
}
public static void Main(String[] args) {
Application.Run(new RubberExample());
}
}
// Install command line compiler via NuGet: nuget install Microsoft.Net.Compilers
// csc -out:test1.exe -target:winexe -platform:x64 -debug- -optimize+ test1.cs
// https://stackoverflow.com/a/27093140/1168754
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CSharpGUI {
public class WinFormExample : Form {
private Button button;
public WinFormExample() {
DisplayGUI();
}
private void DisplayGUI() {
this.Name = "WinForm Example";
this.Text = "WinForm Example";
this.Size = new Size(150, 150);
this.StartPosition = FormStartPosition.CenterScreen;
button = new Button();
button.Name = "button";
button.Text = "Click Me!";
button.Size = new Size(this.Width - 50, this.Height - 100);
button.Location = new Point((this.Width - button.Width) / 3, (this.Height - button.Height) / 3);
button.Click += new System.EventHandler(this.MyButtonClick);
this.Controls.Add(button);
}
private void MyButtonClick(object source, EventArgs e) {
//MessageBox.Show("My First WinForm Application");
// Create a new instance of a form.
Form form1 = new Form();
// Create two buttons to use as the accept and cancel buttons.
Button button1 = new Button();
Button button2 = new Button();
// Set the text of button1 to "OK".
button1.Text = "OK";
// Set the position of the button on the form.
button1.Location = new Point(10, 10);
// Set the text of button2 to "Cancel".
button2.Text = "Cancel";
// Set the position of the button based on the location of button1.
button2.Location = new Point(button1.Left, button1.Height + button1.Top + 10);
// Set the caption bar text of the form.
form1.Text = "My Dialog Box";
// Display a help button on the form.
form1.HelpButton = true;
// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the MaximizeBox to false to remove the maximize box.
form1.MaximizeBox = false;
// Set the MinimizeBox to false to remove the minimize box.
form1.MinimizeBox = false;
// Set the accept button of the form to button1.
form1.AcceptButton = button1;
// Set the cancel button of the form to button2.
form1.CancelButton = button2;
// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterScreen;
// Add button1 to the form.
form1.Controls.Add(button1);
// Add button2 to the form.
form1.Controls.Add(button2);
// Display the form as a modal dialog box.
form1.ShowDialog();
}
public static void Main(String[] args) {
Application.Run(new WinFormExample());
}
}
}
// https://stackoverflow.com/q/8466753/1168754
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
public class FirstForm : Form
{
private Label howdyLabel;
public FirstForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
howdyLabel = new Label();
howdyLabel.Location = new Point (12, 116);
howdyLabel.Text = "Howdy, Partner!";
howdyLabel.Size = new Size (267, 40);
howdyLabel.AutoSize = true;
howdyLabel.Font = new Font ("Times New Roman", 26, System.Drawing.FontStyle.Bold);
//howdyLabel.TabIndex = 0;
howdyLabel.Anchor = AnchorStyles.None;
howdyLabel.TextAlign = ContentAlignment.MiddleCenter;
Text = "The First Form";
Controls.Add(howdyLabel);
}
public static void Main()
{
Application.Run(new FirstForm());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.