Skip to content

Instantly share code, notes, and snippets.

@ozansulukpinar
Last active March 28, 2024 12:18
Show Gist options
  • Save ozansulukpinar/0b8947db620d46da16e3086b2d35d1d5 to your computer and use it in GitHub Desktop.
Save ozansulukpinar/0b8947db620d46da16e3086b2d35d1d5 to your computer and use it in GitHub Desktop.
An example for the usage of Caesar Cipher
/*
Hızlı tüketim sektöründe faaliyet gösteren bir gıda firması, ürün ambalajları içerisine kod yerleştirerek, bu kodlar aracılığı ile çeşitli kampanyalar düzenlemek istemektedir. Proje aşağıda kısaca özetlenmiştir.
1. Firma aşağıdaki özelliklere sahip kodlar üretilmesini talep etmektedir.
• Kodlar 8 hane uzunluğunda ve unique olmalıdır.
• Kodlar ACDEFGHKLMNPRTXYZ234579 karakter kümesini içermelidir.
2. Kullanıcılar kampanya dönemi içerisinde çeşitli kanallar üzerinden ellerindeki kodları kullanarak kampanyalara katılabilecektir.
Beklenenler
• Kod üretimi belirlediğiniz bir algoritmaya uygun olarak yapılmalıdır.
• Kod geçerliliği veritabanı, array, hash table, file, redis, elastic gibi bir saklama ortamından kontrol edilmemeli sadece algoritmik olarak yapılmalıdır.
• Kodların tahmin edilerek sistemin manipüle edilme olasılığı yeterince düşük olmalıdır.
*/
using System;
using System.Text;
public class Program
{
static char[] upperCaseLetters = new char[]{'A','C','D','E','F','G','H','K','L','M','N','P','R','T','X','Y','Z'};
static byte[] numbers = new byte[]{2, 3, 4, 5, 7, 9};
static object[] alphanumerics = new object[]{'A','C','D','E','F','G','H','K','L','M','N','P','R','T','X','Y','Z', 2, 3, 4, 5, 7, 9};
public static void Main()
{
CreateCode();
// In here, you can also check the validation of your code with the helping of IsItValid method
//IsItValid(mycode)
}
private static string CreateCode(){
string firstPart = GetRandomString();
byte shiftKey = GetRandomNumber();
string secondPart = CaesarCipher(firstPart, shiftKey, true);
object lastPart = GetRandomAlphanumeric();
string code = firstPart + shiftKey + secondPart + lastPart;
if(!IsItValid(code))
CreateCode();
return code;
}
private static bool IsItValid(string code){
bool status = false;
string firstPart = code.Substring(0, 3);
string key = code.Substring(3,1);
int keyNumber = Int32.Parse(key);
byte shiftKey = (byte)keyNumber;
string secondPart = code.Substring(4,3);
string lastPart = code.Substring(7,1);
int letterIndex = 0;
int numberIndex = 0;
int lastPartAsNumber = 0;
char lastPartAsChar = new char();
if(int.TryParse(lastPart, out lastPartAsNumber)){
numberIndex = Array.IndexOf(numbers, (byte)lastPartAsNumber);
}
else{
lastPartAsChar = char.Parse(lastPart);
letterIndex = Array.IndexOf(upperCaseLetters, lastPartAsChar);
}
string decodedSecondPart = CaesarCipher(secondPart, shiftKey, false);
int shiftKeyIndex = Array.IndexOf(numbers, (byte)shiftKey);
if(firstPart == decodedSecondPart && shiftKeyIndex > 0 && (letterIndex > 0 || numberIndex > 0))
status = true;
return status;
}
private static char GetRandomLetter(){
char newChar = new char();
Random random = new Random();
int randomNumber = random.Next(0, upperCaseLetters.Length);
newChar = upperCaseLetters[randomNumber];
return newChar;
}
private static string GetRandomString(){
string word = "";
for(byte i = 0; i < 3; i++){
word += GetRandomLetter();
}
return word;
}
private static byte GetRandomNumber(){
Random random = new Random();
int randomNumber = random.Next(0, numbers.Length);
byte number = numbers[randomNumber];
return number;
}
private static object GetRandomAlphanumeric(){
object newObject = new object();
Random random = new Random();
int randomNumber = random.Next(0, alphanumerics.Length);
newObject = alphanumerics[randomNumber];
return newObject;
}
private static char FindLetter(int index, int shiftKey, bool isItEncryption) {
char letter = new char();
int newIndex = -1;
// Focus only the positive shift key
while (shiftKey < 0) {
shiftKey += 17;
}
if (!isItEncryption)
shiftKey *= -1;
newIndex = index + shiftKey;
if (isItEncryption) {
while (newIndex > 16) {
newIndex -= 17;
}
} else {
while (newIndex < 0) {
newIndex += 17;
}
}
letter = upperCaseLetters[newIndex];
return letter;
}
private static string CaesarCipher(string oldText, int shiftKey, bool isItEncryption) {
string newText = "";
char newLetter = new char();
char[] letters = oldText.ToCharArray();
foreach(char letter in letters) {
int firstIndex = Array.IndexOf(upperCaseLetters, letter);
if (firstIndex < 0) {
// This character is not contained in arrays of letters in the alphabet
// That means this is not a valid code
newLetter = letter;
} else {
if (firstIndex > -1)
newLetter = FindLetter(firstIndex, shiftKey, isItEncryption);
}
newText += newLetter;
}
return newText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment