Code C#: Mã hóa cổ điển Ceasar (Ceasar Cipher)

///Ceasar Cipher/Ceasar Encription
using System;
class Program{
static string Caesar(string value, int shift) {
 char[] buffer = value.ToCharArray();
 for (int i = 0; i < buffer.Length; i++){
     // Biến letter chứa ký tự cần mã hóa
     char letter = buffer[i];
     // Add shift to all.
     letter = (char)(letter + shift);
     // Subtract 26 on overflow.
     // Add 26 on underflow.
     if (letter > 'z')  {
  letter = (char)(letter - 26);
     }
     else if (letter < 'a') {
  letter = (char)(letter + 26);
     }
     // Lưu trữ
     buffer[i] = letter;
 }
 return new string(buffer);
    }

    static void Main()
    {
 string a = "test";
 string b = Caesar(a, 18); 
 string c = Caesar(b, -18); 

 string d = Caesar(a, 1); 
 string e = Caesar(d, -1); 

 string f = "exxegoexsrgi";
 string g = Caesar(f, -4); 

 Console.WriteLine(a);
 Console.WriteLine(b);
 Console.WriteLine(c);
 Console.WriteLine(d);
 Console.WriteLine(e);
 Console.WriteLine(f);
 Console.WriteLine(g);
    }
}

Related Posts
Previous
« Prev Post