Skip to content

Commit

Permalink
Add C# implementation of Ceasar Cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
QwerMike committed Oct 20, 2018
1 parent 0735017 commit ecc7364
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cryptography/caesar_cipher/csharp/caesar_cipher.cs
@@ -0,0 +1,35 @@
using System;
using System.Linq;

namespace CaesarCipher
{
class Program
{
static char Encrypt(char ch, int shift)
{
if (!char.IsLetter(ch)) return ch;
char offset = char.IsUpper(ch) ? 'A' : 'a';
return (char)((ch + shift - offset) % 26 + offset);
}

static string Encrypt(string input, int shift)
{
return String.Join(String.Empty, input.Select(ch => Encrypt(ch, shift)));
}

static string Decrypt(string input, int shift)
{
return Encrypt(input, 26 - shift);
}

static void Main()
{
string original = "Hey al-go-rithms! Hello from C#";
string encrypted = Encrypt(original, 5);
string decrypted = Decrypt(encrypted, 5);
Console.WriteLine($"Original: {original}");
Console.WriteLine($"Encrypted: {encrypted}");
Console.WriteLine($"Decrypted: {decrypted}");
}
}
}

0 comments on commit ecc7364

Please sign in to comment.