-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caesar.cs
66 lines (60 loc) · 2.25 KB
/
Caesar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SOUKSAMLANE_LEXTRAIT_TP2
{
public class Caesar
{
public static string Code(string inputText, bool toDecrypt)
{
// Ternary operator - Google it
return toDecrypt ? Decrypt(inputText) : Encrypt(inputText);
}
private static string Caesar_encryption(string inputText, bool encryption_decryption)
{
//For the Caesar encryption, we are using a shift of 2
char shift = (char)2;
string to_encrypt_caesar = inputText;
//We are putting all the characters of the string in an array in order to perform the encryption
char[] array_caesar = to_encrypt_caesar.ToCharArray();
for (var i = 0; i < array_caesar.Length; i++)
{
if (encryption_decryption == false)
{
if ((array_caesar[i] == 89) || (array_caesar[i] == 90) || (array_caesar[i] == 121) || (array_caesar[i] == 122))
{
array_caesar[i] = (char)(array_caesar[i] - 24);
}
else
{
array_caesar[i] = (char)(array_caesar[i] + shift);
}
}
else
{
//we have replaced the + by a - for the decryption
if ((array_caesar[i] == 65) || (array_caesar[i] == 66) || (array_caesar[i] == 97) || (array_caesar[i] == 98))
{
array_caesar[i] = (char)(array_caesar[i] + 24);
}
else
{
array_caesar[i] = (char)(array_caesar[i] - shift);
}
}
}
string encrypted_caesar = string.Join("", array_caesar);
return encrypted_caesar;
}
private static string Encrypt(string inputText)
{
return $"{Caesar_encryption(inputText, false)}";
}
private static string Decrypt(string inputText)
{
return $"{Caesar_encryption(inputText, true)}";
}
}
}