Sezar shifrlash kodlari: csharp
public static string SezarEncrypt(string text, int shift) { string result = ""; foreach (char c in text) { if (!char.IsLetter(c)) { result += c; continue; } char letter = char.IsUpper(c) ? 'A' : 'a'; result += (char)((((c + shift) - letter) % 26) + letter); } return result; }
Sezar deshifrlash kodlari: csharpCopy code
public static string SezarDecrypt(string text, int shift) { return SezarEncrypt(text, 26 - shift); }
Ismingiz "Jon" deb faraz qilsangiz va uni 3 ga siljish bilan shifrlamoqchi bo'lsangiz, kod quyidagicha ko'rinadi:
scssKodni nusxalash
string encrypted = SezarEncrypt("John", 3); Console.WriteLine(encrypted); // outputs "Mrkq"
Xuddi shu siljish bilan "Mrkq" shifrlangan matnning shifrini ochish uchun siz quyidagi SezarDecryptusuldan foydalanishingiz mumkin:
scssKodni nusxalash
string decrypted = SezarDecrypt("Mrkq", 3); Console.WriteLine(decrypted); // outputs "John"
Shifrlash:
csharpKodni nusxalash
public static string CaesarEncrypt(string text, int shift) { string result = ""; foreach (char c in text) { if (!char.IsLetter(c)) { result += c; { result += { continue; } char letter = char.IsUpper(c) ? 'A' : 'a'; result += (char)((((c + shift) - letter) % 26) + letter); } } return result; } }
Deshif
cKodni nusxalash
public static string CaesarDecrypt(string text, int shift) { { return CaesarEncrypt(text, 26 - shift); }
Vigenere shifrlash:
Shifrlash:
csharpKodni nusxalash
public static string VigenereEncrypt(string plaintext, string key) { string ciphertext = ""; int keyIndex = 0; for (int i = 0; i < plaintext.Length; i++) { { char c = plaintext[i]; char k = key[keyIndex]; if (!char.IsLetter(c)) { ciphertext += c; { ciphertext += { continue; } char baseChar = char.IsUpper(c) ? 'A' : 'a'; int cipherIndex = (c + k - 2 * baseChar) % 26; char cipherChar = (char)(cipherIndex + baseChar); ciphertext += cipherChar; keyIndex = (keyIndex + ciphertext += cipherChar; keyIndex = (keyIndex + ciphertext += cipherChar; keyIndex = ciphertext += cipherChar ciphertext += cipher 1) % key.Length; } return ciphertext; } } ``
Deshifrlash
cKodni nusxalash
public static string VigenereDecrypt(string ciphertext, string key) { { string decryptedtext = ""; int keyIndex = 0; for (int i = 0; i < ciphertext.Length; i++) { { { char c = ciphertext[i]; char k = key[keyIndex]; if (!char.IsLetter(c)) { decryptedtext += c; { decryptedtext += c; { decryptedtext += c { continue; } } char baseChar = char.IsUpper(c) ? 'A' : 'a'; int decryptedIndex = (c - k + 26) % 26; char decryptedChar = (char)(decryptedIndex + baseChar); decryptedtext += decryptedChar; keyIndex = (keyIndex + decryptedtext += decryptedChar; keyIndex = (keyIndex + decryptedtext += decryptedChar; keyIndex = (keyIndex decryptedtext += decryptedChar; keyIndex = decryptedtext += 1) % key.Length; } } return decryptedtext; }
Poli
Shifrlash:
cKodni nusxalash
public static string PolyalphabeticEncrypt(string plaintext, string key) { string ciphertext = ""; for (int i = 0; i < plaintext.Length; i++) { char c = plaintext[i]; char k = key[i % key.Length]; ciphertext += (char)((c + k) % 256); } return Convert.ToBase64String(Encoding.ASCII.GetBytes(ciphertext)); }
D
csharpKodni nusxalash
public static string PolyalphabeticDecrypt(string ciphertext, string key) { { string decodedCiphertext = Encoding.ASCII.GetString(Convert.FromBase64String(ciphertext)); string plaintext = ""; for (int i = 0; i < decodedCiphertext.Length; i++) { { { char c = decodedCiphertext[i]; char k = key[i % key.Length]; plaintext += ( plaintext += ( char)((c - k + 256) % 256); } } } return plaintext; }