-> 블로그 이전

[정보보호개론] Week_1 : Caesar's Cipher

2022. 3. 3. 20:15Major`/정보보호개론

728x90
반응형

Caesar's Cipher

- 최초의 암호 알고리즘

- 단순치환암호의 일종이다

  • 알파벳별로 일정한 거리만큼 밀어서 다른 알파벳으로 치환하는 방식

Key

- Shift by n

 

 

Key Space

- 암호 알고리즘에서 key의 경우의 수

>> Caesar's Cipher의 경우 Key Space는 0 ~ 25인 26이다

 

Exhaustive Key Search

- key의 가능한 모든 경우의 수를 통해서 암호를 해석하는 방법

~~ Brute-Froce / 완전 탐색

 


Caesar's Cipher Encryption

static void encryption_removeSpace(){
    System.out.print("\nPlain Text : ");
    String text = sc.nextLine();

    System.out.print("Key : ");
    int key = sc.nextInt(); // 0 <= key < 26
    sc.nextLine();

    for(int i=65; i<=90; i++){ // Shift Alphabet (by key)
        int v = (i - 'A' + key) % 26;
        System.out.println(((char)(i + 32)) + " -> " + ((char)(v + 'A')));
    }

    System.out.print("Cipher Text : ");

    // (1) remove space + toUpper
    text = text.replaceAll(" ", "").toUpperCase();

    // (2) Shift by n(key)
    for(char c : text.toCharArray()){
        int v = (c - 'A' + key) % 26;
        System.out.print((char)(v + 'A'));
    }

    System.out.println("\n");
}

 

Caesar's Cipher Decryption (Brute-Force)

static void decryption(){
    // Brute-Force
    System.out.print("\nCipher Text : ");
    String text = sc.nextLine();

    for(int i=0; i<=25; i++){
        System.out.print("# Key " + i + " : ");

        text = text.toLowerCase();

        for(char c : text.toCharArray()){
            int v = (c - 'a' - i + 26) % 26;
            System.out.print((char)(v + 'a'));
        }
        System.out.println();
    }

    System.out.println();
}

728x90
반응형