Encription 3 - Gaius Julius

Encription 3 - Gaius Julius

Program for encryption and decryption of text messages. Method used for algorithm development is based on the story about the most famousemperor of Rome, Gaius Julius (100-44 B.C.) when he was sending text messages to army generals (^).

At the bottom of the page there are links for Downloading free version of the executable program or the entire project. Free software can be downloaded by clicking on the image of the program. For proper work of the program is necessary .Net framework 4.0 ( ^ ) !

/************************************************************************************************

* Program name : Encryption - Gaius Julius *

* Program version : 1.0 *

* Developed using : SharpDevelop *

* Code author : Перић Жељко *

* Code language : C# *

* Date : 10.07.2016 *

* Time : 12:00 *

* *

* Program description : *

* *

* Program for encryption and decryption text messages.Method used for algorithm development is *

* based on the story about the most famous emperor of Rome, Gaius Julius (100-44 B.C.) *

* when he was sending text messages to army generals. *

* *

* For encrypting text messages are used original letters of the alphabet which was used for *

* writing the message and its original layout. Encryption is performed so that for each letter *

* in the original message is found its original position within the alphabet, which is then *

* moved for a specific number of places and letter is replaced by letter of the alphabet, *

* which is located at a new position.Number of places for which will be made moving is called *

* ENCRYPTION KEY. Moving the position of the letters can be on the right, when the position *

* number increases,or to the left when the position number is reduced by the value of the KEY. *

* Given that the number of letters in the alphabet is limited,the number of values ​​that can be *

* assigned to the encryption key is limited. The value range is from one to the number of *

* letters in the alphabet minus one. To decrypt the message, it is necessary to know the *

* encrypted text messages, letters of the original alphabet and their arrangement, *

* the direction of moving the position of letters in the alphabet and key value. *

* *

* *

* Example of moving the letters in the alphabet when the key value is nine *

* and the displacement direction is to the right: *

* *

* A B C D E F G H I J K L M N O P Q R S T U V W X Y Z *

* | | | | | | | | | | | *

* J K L M N O P Q R S T U V W X Y Z A B C D E F G H I *

* *

* *

* *

* In this case thought that by the story the famous emperor spoke on the river Rubicon *

* " The dice is thrown " in code is " Cqn mrln rb cqaxfw ". *

* *

* This kind of encryption is one of the so-called symmetric encryption methods when encryption *

* and decryption methods are using the same key value. Although encryption of text messages *

* with this method is very simple and can be easily decrypted without knowing the essential *

* elements for the decryption, there are variants that are claimed to be invincible *

* cryptographic methods for protecting text messages. See on the Internet texts on *

* Vigenère cipher. This program uses basic, afore said encryption. The program encrypts and *

* decrypts only predefined letters of the alphabet, while other characters which are not *

* recognized, are transcribed. It is possible to directly drag text documents in the text *

* message field, when the program itself opens the document and reads the text and then *

* encrypt or decrypt, depending on the selected options. Right-click on the text entry field *

* of encrypted or decrypted text messages, opens a menu for selection of loading or writing *

* text messages. The program can open only three types of text documents * .txt, *. Rtf and *

* * .doc, which must be recorded as a UNICODE text documents. There are many variations of *

* this type of encryption, and the appropriate programs, look on the Internet, *

* or try to make Your own version. *

* *

* http://en.wikipedia.org/wiki/Cryptography *

* https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher *

* *

* All the best, *

* Author. *

************************************************************************************************/

using System;

using System.IO;

using System.Text;

using System.Windows.Forms;

namespace Encryption___Gaius_Julius

{

public partial class MainForm : Form

{

// Global variables

//

// Variable Alphabet contains all letters of Alphabet

// with original layout that program will recognize in message text

//

string Alphabet ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

//

// variable Direction contains value for rotation direction

//

string Direction = "Right";

//

// Variable Message_Text contains

// text that shall be Encrypted

//

string Message_Text = "Enter Message Text";

//

// Variable Key contains positive integer value

// of encryption key

//

int Key = 1;

//

// Variable Encrypt contains bool value that defines

// what operation is going to be executed

// true - Encrypt

// false - Decrypt

//

bool Encrypt = true;

//

//

//

//

public MainForm()

{

InitializeComponent();

Move_And_Show_Letters_Layout();

}

// Move letters based on key value and direction

// and show new letter layout

void Move_And_Show_Letters_Layout()

{

// Local variables

int Counter = 0;

int Letter_Position = 0;

int Namber_Of_Letters_In_Alphabet = 0;

Key = (int) keyValueField.Value;

if(Direction == "Left")

{

Key *= - 1;

}

NewLetterLayout.Text = string.Empty;

Namber_Of_Letters_In_Alphabet = Alphabet.Length;

for(Counter=0;Counter<Namber_Of_Letters_In_Alphabet;Counter++)

{

Letter_Position = Counter + Key;

if(Letter_Position > Namber_Of_Letters_In_Alphabet-1)

{

Letter_Position -= Namber_Of_Letters_In_Alphabet;

}

if(Letter_Position < 0)

{

Letter_Position += Namber_Of_Letters_In_Alphabet;

}

NewLetterLayout.Text += Alphabet[Letter_Position] + " ";

}

}

// Encryption - Decryption of message text

void Encrypt_Decrypt_Message_Text()

{

// Local variables

string Letter = "";

string Letter_Casing = "";

int Counter = 0;

int Letter_Position = 0;

int Number_Of_Letters_In_Alphabet = 0;

int Number_Of_Letters_In_Message = 0;

Key = (int) keyValueField.Value;

if(Encrypt)

{

if(Direction == "Left")

{

Key *= - 1;

}

}

else

{

if(Direction == "Right")

{

Key *= - 1;

}

}

Number_Of_Letters_In_Alphabet = Alphabet.Length;

Message_Text = MessageTextField.Text;

Number_Of_Letters_In_Message = Message_Text.Length;

EncryptedDecryptedMessageField.Text = "";

for(Counter=0;Counter<Number_Of_Letters_In_Message;Counter++)

{

Letter = Message_Text.Substring(Counter,1);

if(char.IsUpper(Letter[0]))

Letter_Casing = "Upper case";

else

Letter_Casing = "Lower case";

Letter = Letter.ToUpper();

Letter_Position = Find_Letter_Position(Letter);

if(Letter_Position != -1)

{

Letter_Position += Key;

if(Letter_Position > Number_Of_Letters_In_Alphabet-1)

{

Letter_Position -= Number_Of_Letters_In_Alphabet;

}

if(Letter_Position < 0)

{

Letter_Position += Number_Of_Letters_In_Alphabet;

}

Letter = Alphabet.Substring(Letter_Position,1);

if(Letter_Casing == "Upper case")

Letter = Letter.ToUpper();

else

Letter = Letter.ToLower();

EncryptedDecryptedMessageField.Text += Letter;

}

else

{

if(Letter_Casing == "Upper case")

Letter = Letter.ToUpper();

else

Letter = Letter.ToLower();

EncryptedDecryptedMessageField.Text += Letter;

}

}

}

// Find letter position index inside Alphabet

int Find_Letter_Position(string Letter)

{

// Local variables

int Letter_Index = -1;

int Counter = 0;

int Number_Of_Letters_In_Alphabet = 0;

Number_Of_Letters_In_Alphabet = Alphabet.Length;

for(Counter=0;Counter<Number_Of_Letters_In_Alphabet;Counter++)

{

if(Alphabet.Substring(Counter,1) == Letter)

{

Letter_Index = Counter;

break;

}

}

return Letter_Index;

}

// Rotation to the left selected

void MoveLeftChoiceCheckedChanged(object sender, EventArgs e)

{

if(MoveLeftChoice.Checked)

{

Direction = "Left";

Move_And_Show_Letters_Layout();

Encrypt_Decrypt_Message_Text();

}

}

// Rotation to the right selected

void MoveRightChoiceCheckedChanged(object sender, EventArgs e)

{

if(MoveRightChoice.Checked)

{

Direction = "Right";

Move_And_Show_Letters_Layout();

Encrypt_Decrypt_Message_Text();

}

}

// Key value is changed

void KeyValueChanged(object sender, EventArgs e)

{

Move_And_Show_Letters_Layout();

Encrypt_Decrypt_Message_Text();

}

// Finished entry of the plaintext message

void MessageTextFieldKeyDown(object sender, KeyEventArgs e)

{

if(e.KeyData == Keys.Enter)

{

Encrypt_Decrypt_Message_Text();

}

}

// Finished entry of the plaintext message

void MessageTextFieldLeave(object sender, EventArgs e)

{

Encrypt_Decrypt_Message_Text();

}

// Selected option is Encryption

void EncryptMessageTextChoiceCheckedChanged(object sender, EventArgs e)

{

if(EncryptTextChoice.Checked)

{

EncryptedMessageDescription.Text = " Encrypted message ";

Encrypt = true;

Encrypt_Decrypt_Message_Text();

}

}

// Selected option is Decryption

void DecryptMessageTextChoiceCheckedChanged(object sender, EventArgs e)

{

if(DecriptTextChoice.Checked)

{

EncryptedMessageDescription.Text = " Decrypted message ";

Encrypt = false;

Encrypt_Decrypt_Message_Text();

}

}

//

// Enables drag and drop operaton of textual documents

// or just part of the document text in message text field

// program recognizes only three typs of documents *.txt, *.rtf и *.doc,

// that must be saved as UNICODE text documents !!!

void MessageTextFieldDragDrop(object sender, System.Windows.Forms.DragEventArgs e)

{

if (e.Data.GetDataPresent(DataFormats.FileDrop))

{

// If user selects more than one document

// all document names and paths shall be saved to table Documents

string [] Documents = (string[]) (e.Data.GetData(DataFormats.FileDrop));

// Open all selected documents and read text

foreach(string Document_Path in Documents)

{

if(Document_Path.EndsWith(".txt") ||

Document_Path.EndsWith(".doc") ||

Document_Path.EndsWith(".rtf"))

MessageTextField.Text += "\n" + File.ReadAllText(Document_Path);

}

Encrypt_Decrypt_Message_Text();

}

else if (e.Data.GetDataPresent(DataFormats.Text))

{

// Copy text

MessageTextField.AppendText("\n" +(string) (e.Data.GetData(DataFormats.Text)));

Encrypt_Decrypt_Message_Text();

}

e.Effect = DragDropEffects.None;

}

// Show menu option

void MessageTextFieldMouseDown(object sender, MouseEventArgs e)

{

if(e.Button == MouseButtons.Right)

{

MenuLoad.Text = " Load message text";

MenuSave.Text = " Save message text";

Мени.Show(MousePosition);

}

}

// Show menu option

void EncryptedDecryptedMessageFieldMouseDown(object sender, MouseEventArgs e)

{

if(e.Button == MouseButtons.Right)

{

MenuLoad.Text = " Load encrypted message";

MenuSave.Text = " Save encrypted message";

Мени.Show(MousePosition);

}

}

void MenuLoadClick(object sender, EventArgs e)

{

if(MenuLoad.Text == " Load message text")

{

// Load message text from document

LoadDocument.Title = " Load message text";

}

else

{

// Load encrypted message text from document

LoadDocument.Title = " Load encrypted message text";

}

DialogResult Choice;

Choice = LoadDocument.ShowDialog();

if(Choice == DialogResult.OK)

{

StreamReader Read = new StreamReader(LoadDocument.FileName,Encoding.Unicode);

MessageTextField.Text += "\n" + Read.ReadToEnd();

Read.Close();

Read.Dispose();

}

Encrypt_Decrypt_Message_Text();

}

void MenuSaveClick(object sender, EventArgs e)

{

if(MenuSave.Text == " Save message text")

{

// Saves message text

SaveDocument.Title = " Save message text";

Message_Text = MessageTextField.Text;

}

else

{

// Saves encrypted message text

SaveDocument.Title = " Save encrypted message";

Message_Text = EncryptedDecryptedMessageField.Text;

}

DialogResult Choice;

Choice = SaveDocument.ShowDialog();

if(Choice == DialogResult.OK)

{

StreamWriter Write = new StreamWriter(SaveDocument.FileName,false,Encoding.Unicode);

Write.WriteLine(Message_Text);

Write.Close();

Write.Dispose();

}

}

}

}

/************************************************************************

* Program Licence : *

* *

* Copyright 2016 , Perić Željko *

* (periczeljkosmederevo@yahoo.com) *

* *

* According to it's main purpose , this program is licensed *

* under the therms of 'Free Software' licence agreement. *

* *

* If You do not know what those therms applies to *

* please read explanation at the following link : *

* (http://www.gnu.org/philosophy/free-sw.html.en) *

* *

* Since it is Free Software this program has no warranty of any kind. *

************************************************************************

* Ethical Notice : *

* *

* It is not ethical to change program code signed by it's author *

* and then to redistribute it under the same author name , *

* especially if it is incorrect. *

* *

* It is recommended that if You make improvement in program code , *

* to make remarks of it and then to sign it with Your own name , *

* for further redistribution as new major version of program. *

* *

* Author name and references of old program code version should be *

* kept , for tracking history of program development. *

* *

* For any further information please contact code author at his email. *

************************************************************************/

/************************************

* List Of Revisions *

************************************

* *

* *

* *

************************************/