Skip to content

Commit 34b4c32

Browse files
committed
Cipher done
1 parent fc042cb commit 34b4c32

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
All task from list of Programming Challenges v1.4
33
![list](docs/list.png)
44

5+
If you have any questions, you can contact me by email morasiu2@gmail.com
56

67
## Table of content
78
1. [00 Name generator](#00)

challenges/04_Cipher/.Cipher.cs.swp

-12 KB
Binary file not shown.

challenges/04_Cipher/Cipher.cs

+29-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class Cipher{
55
static void Main(string[] args){
6-
Console.WriteLine("<--------------_Cipher---------------->");
6+
Console.WriteLine("<---------------Cipher---------------->");
77
Console.WriteLine("Created by Morasiu (morasiu2@gmail.com)");
88

99
string text = GetText();
@@ -27,13 +27,12 @@ static void Encrypt(string text, string key){
2727
char originLetter = text[i];
2828
int asciiOrigin = originLetter;
2929
char keyChar = key[keyIndex];
30-
int asciiKey = keyChar;
31-
int asciiEnc = asciiOrigin + asciiKey + 32;
30+
int asciiKey = keyChar - 32;
31+
int asciiEnc = asciiOrigin + asciiKey;
3232
if (asciiEnc > 126)
33-
asciiEnc -= 126;
34-
33+
asciiEnc = asciiEnc - 126 + 32;
3534

36-
char encChar = (char) (asciiOrigin + asciiKey);
35+
char encChar = (char) (asciiEnc);
3736

3837
keyIndex++;
3938
if(keyIndex > key.Length -1)
@@ -45,7 +44,30 @@ static void Encrypt(string text, string key){
4544
}
4645

4746
static void Decrypt(string text, string key){
48-
47+
string decText = "";
48+
int keyIndex = 0;
49+
if (key.Length == 0){
50+
Console.WriteLine("Empty key");
51+
} else {
52+
for(int i = 0; i < text.Length; i++){
53+
char originLetter = text[i];
54+
int asciiOrigin = originLetter;
55+
char keyChar = key[keyIndex];
56+
int asciiKey = keyChar - 32;
57+
int asciiDec = asciiOrigin - asciiKey;
58+
if (asciiDec < 32)
59+
asciiDec = 126 - (32 - asciiDec);
60+
61+
char decChar = (char) (asciiDec);
62+
63+
keyIndex++;
64+
if(keyIndex > key.Length -1)
65+
keyIndex = 0;
66+
decText += decChar.ToString();
67+
}
68+
Console.Write("Result:\n>" + decText + "<\n");
69+
}
70+
4971
}
5072

5173
static string ChooseOption(){

challenges/04_Cipher/Cipher.exe

0 Bytes
Binary file not shown.

challenges/04_Cipher/Cipher.exe.mdb

203 Bytes
Binary file not shown.

docs/04.png

21.8 KB
Loading

0 commit comments

Comments
 (0)