Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

C++ Program: Implement Caesar Cipher (Shift Cipher)


Caesar Cipher (Shift Cipher) เป็น Monoalphabetic Substitution Cipher ที่ง่ายที่สุด เลื่อนตัวอักษรแต่ละตัวไปข้างหน้าจำนวนคงที่ (Key) ในช่วง 0-25

Encryption & Decryption

Encryption:  C = (P + k) mod 26
Decryption:  P = (C - k + 26) mod 26

Key = 3 เรียกว่า "Caesar Cipher" อย่างเป็นทางการ (Julius Caesar ใช้ Shift 3)

โค้ด C++ สมบูรณ์ (รองรับ Upper/Lower, Preserve Non-Alpha)

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string caesarEncrypt(const string& text, int key) {
    string result;
    key = ((key % 26) + 26) % 26; // Normalize key to [0,25]
    
    for (char ch : text) {
        if (isupper(ch)) {
            result += char('A' + (ch - 'A' + key) % 26);
        } else if (islower(ch)) {
            result += char('a' + (ch - 'a' + key) % 26);
        } else {
            result += ch; // Preserve spaces, punctuation, digits
        }
    }
    return result;
}

string caesarDecrypt(const string& text, int key) {
    // Decrypt = Encrypt with negative key (or 26-key)
    return caesarEncrypt(text, 26 - key);
}

// Alternative: Single function with direction
string caesarCipher(const string& text, int key, bool encrypt) {
    if (!encrypt) key = 26 - key;
    return caesarEncrypt(text, key);
}

int main() {
    string message;
    int key, choice;
    
    cout << "Enter message: ";
    getline(cin, message);
    
    cout << "Enter key (0-25): ";
    cin >> key;
    
    cout << "1. Encrypt\n2. Decrypt\nChoice: ";
    cin >> choice;
    
    if (choice == 1) {
        cout << "Encrypted: " << caesarEncrypt(message, key) << "\n";
    } else if (choice == 2) {
        cout << "Decrypted: " << caesarDecrypt(message, key) << "\n";
    } else {
        cout << "Invalid choice\n";
    }
    
    return 0;
}

ตัวอย่างการรัน

Enter message: tutorial
Enter key (0-25): 3
1. Encrypt
2. Decrypt
Choice: 1
Encrypted: wxwruldo

Enter message: wxwruldo
Enter key (0-25): 3
1. Encrypt
2. Decrypt
Choice: 2
Decrypted: tutorial

ROT13 (Special Case)

Key = 13 → Encrypt = Decrypt (Involution)

string rot13(const string& text) {
    return caesarEncrypt(text, 13);
}

Cryptanalysis (การวิเคราะห์รหัส)

  • Brute Force: ลอง Key 1-25 ทั้งหมด (Instant)
  • Frequency Analysis: เปรียบเทียบความถี่ตัวอักษรกับภาษาเป้าหมาย
  • Known Plaintext: หา Key ได้ทันทีจาก 1 คู่

Security

  • Key Space: 26 → Insecure
  • ไม่มี Diffusion/Confusion
  • ใช้เพื่อ Education, Obfuscation (เช่น ROT13 ใน Usenet), หรือ Puzzle เท่านั้น

Variants

  • ROT13: Shift 13, Self-inverse
  • ROT47: Shift 47 บน Printable ASCII (33-126)
  • Affine Cipher: Generalization (ax + b) mod 26
  • Vigenère: Polyalphabetic Version

Modern C++ Version (C++20)

#include <algorithm>
#include <string_view>

string caesarModern(string_view text, int key, bool encrypt = true) {
    key = ((key % 26) + 26) % 26;
    if (!encrypt) key = 26 - key;
    
    string result;
    result.reserve(text.size());
    
    for (char ch : text) {
        if (isupper(ch)) result += char('A' + (ch - 'A' + key) % 26);
        else if (islower(ch)) result += char('a' + (ch - 'a' + key) % 26);
        else result += ch;
    }
    return result;
}