0% found this document useful (0 votes)
10 views

Caesar Cipher Coding Activity

The document contains a C# source code for a Caesar Cipher application that can encrypt and decrypt text based on a user-defined shift value. It includes methods for encryption and decryption, along with event handlers for user interactions such as button clicks and text input. The encryption process shifts letters while leaving non-letter characters unchanged, and the decryption process reverses the shift to retrieve the original text.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Caesar Cipher Coding Activity

The document contains a C# source code for a Caesar Cipher application that can encrypt and decrypt text based on a user-defined shift value. It includes methods for encryption and decryption, along with event handlers for user interactions such as button clicks and text input. The encryption process shifts letters while leaving non-letter characters unchanged, and the decryption process reverses the shift to retrieve the original text.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Quimbo, Prytj Elmo G.

IT16-6727

Caesar Cipher Coding Activity


Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CeasarCipher
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

// Function to encrypt the input text using the Caesar Cipher technique
private string Encrypt(string input, int shift)
{
string result = ""; // Initialize result string

foreach (char c in input)


{
if (char.IsLetter(c)) // Check if character is a letter
{
char offset = char.IsUpper(c) ? 'A' : 'a'; // Determine if
uppercase or lowercase
result += (char)(((c + shift - offset) % 26) + offset); //
Shift the character and wrap within A-Z/a-z
}
else
{
result += c; // Keep non-letter characters unchanged
}
}
return result; // Return the encrypted text
}

// Function to decrypt the encrypted text by reversing the Caesar Cipher


shift
private string Decrypt(string input, int shift)
{
return Encrypt(input, 26 - shift); // Reverse the shift by using (26 -
shift)
}

// Event handler for the Encrypt button click


private void button1_Click(object sender, EventArgs e)
{
string inputText = textBox1.Text; // Get text from input field
int shiftKey = (int)numericUpDown1.Value; // Get shift value from
numeric input
label6.Text = Encrypt(inputText, shiftKey); // Display encrypted text
in label6
}

// Event handler for the Decrypt button click


private void button2_Click(object sender, EventArgs e)
{
string encryptedText = textBox1.Text; // Get text from input field
(encrypted text assumed)
int shiftKey = (int)numericUpDown1.Value; // Get shift value from
numeric input
label7.Text = Decrypt(encryptedText, shiftKey); // Display decrypted
text in label7
}

// Event handler for the Reset button click


private void button3_Click_1(object sender, EventArgs e)
{
textBox1.Clear(); // Clear input text field
label6.Text = "Encrypted text here..."; // Reset encrypted text label
label7.Text = "Decrypted text here..."; // Reset decrypted text label
numericUpDown1.Value = 0; // Reset shift key to default value (0)
}

// Event handler for text change in the input textbox (currently empty)
private void textBox1_TextChanged(object sender, EventArgs e)
{
// This can be used to handle real-time validation if needed
}

// Empty event handler (can be removed if unused)


private void button3_Click(object sender, EventArgs e)
{

}
}
}

Encryption Process:
• The Encrypt method takes an input string and a shift value.
• It loops through each character of the input text:
• If the character is a letter, it shifts it by the given value within the
alphabet's range using modular arithmetic.
• If the character is not a letter, it remains unchanged.
• The shifted characters are concatenated to form the encrypted result.

Decryption Process:
• The Decrypt method reverses the encryption process by shifting the
characters backward using 26 - shift, effectively undoing the original shift.
• It reuses the Encrypt function by shifting in the opposite direction.

You might also like