Encryption 2 - Expedition

Encryption 2 - Expedition

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

* Program name : Expedition *

* Program ver. : 1.0 *

* Created by : SharpDevelop *

* Code author : Perić Željko *

* Code language : C# *

* Date created : 25.11.2013 *

* Time created : 16:10 *

* *

* *

* Program Description : Program for telegram encryption and decryption. *

* Telegram is plain unicod text document, that contains message text. *

* To start program simply drag and drop telegram to program icon, *

* and program will start automatically. Program open's telegram, *

* and finds out weather text inside is open or encrypted. *

* Performs encryption or decryption operation on message text, and saves *

* result to new unicode text document. Key for encryption or decryption *

* is entered by user. Key is positive integer from one up to approximately *

* sixty five thousands. Key range varies on local langage settings. *

* In case of error response from program, it is usually too big key value. *

* Program accepts exclusively text from plain unicode text documents. *

* *

* *

* All the best, *

* Author *

* *

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

using System;

using System.IO;

using System.Text;

namespace Expedition

{

class Cryptographer

{

public static void Main(string[] telegram)

{

if(telegram.Length>0)

{

if(telegram[0].EndsWith(".txt"))

{

Teleprinter Т1=new Teleprinter();

Т1.Open_Telegram(telegram);

}

}

}

}

class Teleprinter

{

public void Open_Telegram(string[] телеграм)

{

string adress=string.Empty;

string text=string.Empty;

adress=телеграм[0];

text=File.ReadAllText(adress);

adress=adress.Replace(".txt","");

Console.SetWindowSize(33,5);

Console.SetBufferSize(33,5);

Console.Title=" Enemy listens !";

if(text.EndsWith("@@@"))

{

text=Decrypt_Telegram(text);

File.WriteAllText(adress.Replace("@@@","")+" .txt",text,Encoding.UTF8);

}

else if(text.Length>0)

{

text=Encrypt_Telegram(text);

File.WriteAllText(adress+" @@@.txt",text,Encoding.UTF8);

}

}

string Decrypt_Telegram(string text)

{

string decrypted=string.Empty;

string letter=string.Empty;

int key=0;

int length=0;

int counter=0;

int code=0;

Console.WriteLine("Enter decryption key :");

Console.WriteLine("------------------------");

key=EnterKey(0,2);

length=text.Length-3;

counter=0;

while(counter<length)

{

code=0;

code=text[counter];

code-=key;

decrypted+=(char)code;

counter++;

}

return decrypted;

}

string Encrypt_Telegram(string text)

{

string encrypted=string.Empty;

string letter=string.Empty;

int key=0;

int length=0;

int counter=0;

int code=0;

Console.WriteLine("Enter encryption key :");

Console.WriteLine("----------------------");

key=EnterKey(0,2);

length=text.Length;

counter=0;

while(counter<length)

{

code=0;

code=text[counter];

code+=key;

encrypted+=(char)code;

counter++;

}

encrypted+="@@@";

return encrypted;

}

int EnterKey(int position_x,int position_y)

{

const string possible_numbers="1234567890";

string word=string.Empty;

string letter=string.Empty;

int length=0;

int number=0;

Console.SetCursorPosition(position_x,position_y);

Console.Write(" ");

Console.SetCursorPosition(position_x,position_y);

ConsoleKeyInfo button=new ConsoleKeyInfo();

while(button.Key!=ConsoleKey.Enter||length==0)

{

button=Console.ReadKey();

if(button.Key==ConsoleKey.Backspace||button.Key==ConsoleKey.Delete)

{

length--;

if(length<0)

{

length=0;

}

word=word.Substring(0,length);

Console.SetCursorPosition(position_x,position_y);

Console.Write(" ");

Console.SetCursorPosition(position_x,position_y);

Console.Write(word);

}

else

{

letter=button.KeyChar.ToString();

if(possible_numbers.Contains(letter))

{

length++;

if(length<6)

{

word=word+letter;

}

else

{

length=5;

Console.SetCursorPosition(position_x,position_y);

Console.Write(" ");

Console.SetCursorPosition(position_x,position_y);

Console.Write(word);

}

}

else if(!possible_numbers.Contains(letter))

{

Console.SetCursorPosition(position_x,position_y);

Console.Write(" ");

Console.SetCursorPosition(position_x, position_y);

Console.Write(word);

}

}

}

number=int.Parse(word.ToString());

return number;

}

}

}

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

* Program Licence : *

* *

* Copyright 2013 , 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 *

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

* *

* *

* *

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