3
3
4
4
class Cipher {
5
5
static void Main ( string [ ] args ) {
6
- Console . WriteLine ( "<--------------_Cipher ---------------->" ) ;
6
+ Console . WriteLine ( "<---------------Cipher ---------------->" ) ;
7
7
Console . WriteLine ( "Created by Morasiu (morasiu2@gmail.com)" ) ;
8
8
9
9
string text = GetText ( ) ;
@@ -27,13 +27,12 @@ static void Encrypt(string text, string key){
27
27
char originLetter = text [ i ] ;
28
28
int asciiOrigin = originLetter ;
29
29
char keyChar = key [ keyIndex ] ;
30
- int asciiKey = keyChar ;
31
- int asciiEnc = asciiOrigin + asciiKey + 32 ;
30
+ int asciiKey = keyChar - 32 ;
31
+ int asciiEnc = asciiOrigin + asciiKey ;
32
32
if ( asciiEnc > 126 )
33
- asciiEnc -= 126 ;
34
-
33
+ asciiEnc = asciiEnc - 126 + 32 ;
35
34
36
- char encChar = ( char ) ( asciiOrigin + asciiKey ) ;
35
+ char encChar = ( char ) ( asciiEnc ) ;
37
36
38
37
keyIndex ++ ;
39
38
if ( keyIndex > key . Length - 1 )
@@ -45,7 +44,30 @@ static void Encrypt(string text, string key){
45
44
}
46
45
47
46
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
+
49
71
}
50
72
51
73
static string ChooseOption ( ) {
0 commit comments