월레스와 그로밋: 코딩의 날

Ceasor Code(시저 암호_해독 퀴즈) 본문

Etc/Java

Ceasor Code(시저 암호_해독 퀴즈)

구운 감자 2025. 1. 26. 23:46

CeasorCode.java

public class CeaserCode {
	
	public String inputPlainText;
	public StringBuilder plainText;
	private String cipherText;
	private int decryptionKey;
	private final int COUNT = 4;
	
	//	public String getPlainText() {
//		return plainText;
//	}
//	public void setPlainText(String plainText) {
//		this.plainText = plainText;
//	}
	public String getCipherText() {
		return cipherText;
	}
	public void setCipherText(String cipherText) {
		this.cipherText = cipherText;
	}
	public int getDecryptionKey() {
		return decryptionKey;
	}
	public void setDecryptionKey(int decryptionKey) {
		this.decryptionKey = decryptionKey;
	}
	public int getCOUNT() {
		return COUNT;
	}
}

CeasorCodeSource.java

import java.util.Random;
import java.util.Scanner;

public class CeaserCodeSource {
	
	CeaserCode cc = new CeaserCode();
	Scanner input = new Scanner(System.in);
	
	// 암호문 주고 해독하는 문제내기 (mainSystem)
	public void mainSystem() {
		getRandomCipherText();
		getRandomDecryptionKey();
		
		getPlainText(getCipherList());
		
		System.out.println(cc.getCipherText() + " 를 해독하시오!");
		
		int i = 0, count = 1;
		
		while(i < cc.getCOUNT()) {
			getInputPlainText();
			boolean check = checkPlainText();
			
			if(check) {
				System.out.println(count + "번 만에 맞췄습니다. 굿굿\n" + "암호키는 " + cc.getDecryptionKey() + " 였습니다.");
				break;
			}
			
			if(count == 4)
				System.out.println("틀렸습니다.\n암호키 : " + cc.getDecryptionKey() + " / 평문 : " + cc.plainText);
			else
			{
				System.out.println("틀렸습니다. 다시 해독하세요!");
				System.out.println("암호키 힌트 : " + decryptionKeyHint(count) + "\n");
			}	
			count++;
			i++;
		}
		
		input.close();
	}
	
	// 랜덤 문자열(암호문) 생성
	public void getRandomCipherText() {
		int leftLimit = 65;
	    int rightLimit = 122;
	    int targetStringLength = 5;
	    Random random = new Random();
	    String generatedString = random.ints(leftLimit, rightLimit + 1)
	                                   .filter(i -> i <= 90 || i >= 97)
	                                   .limit(targetStringLength)
	                                   .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
	                                   .toString();
	    cc.setCipherText(generatedString);
	}
	
	// 랜덤 암호키값 생성 (1 ~ 1000)
	public void getRandomDecryptionKey() {
		cc.setDecryptionKey((int)(Math.random() * 100) + 1);
	}
	
	// 평문 입력 구간
	public void getInputPlainText() {
		System.out.print("해독한 평문 입력 >> ");
		cc.inputPlainText = input.next();
	}
	
	// 대문자 '65 ~ 90' 소문자 '97 ~ 122'	
	
	// 암호문을 배열로 변환
	public char[] getCipherList() {
		char[] cipherList = cc.getCipherText().toCharArray();
		
		return cipherList;
	}
	
	// 평문 구하는 메소드
	public void getPlainText(char[] cipherList) {
		for(int i = 0; i < cipherList.length; i++) {
			// 대문자
			if(cipherList[i] >= 65 && cipherList[i] <= 90) {
				if(cipherList[i] - 65 - (cc.getDecryptionKey() % 26) < 0) {
					cipherList[i] = (char)((26 - (cc.getDecryptionKey() % 26 - (cipherList[i] - 65))) + 65);
				}
				else {
					cipherList[i] = (char)((cipherList[i] - 65 - cc.getDecryptionKey() % 26) + 65);
				}
			}
			// 소문자
			else if(cipherList[i] >= 97 && cipherList[i] <= 122)
			{
				if(cipherList[i] - 97 - (cc.getDecryptionKey() % 26) < 0) {
					cipherList[i] = (char)((26 - (cc.getDecryptionKey() % 26 - (cipherList[i] - 97))) + 97);
				}
				else {
					cipherList[i] = (char)((cipherList[i] - 97 - (cc.getDecryptionKey() % 26)) + 97);
				}
			}
		}
		
		cc.plainText = new StringBuilder();
		
		for(int i=0; i<5; i++) {	// 문자열 길이만큼
			cc.plainText.append(cipherList[i]); 
		}
	}
	
	// 입력한 평문과 시스템이 출력한 평문이 같은지 확인
	public boolean checkPlainText() {
		if(cc.inputPlainText.equals(cc.plainText.toString()))
			return true;
		else
			return false;
	}
	
	// 암호 키 힌트 가르쳐주기
	public String decryptionKeyHint(int count) {
		String hint="";
		switch(count) {
		case 1:
			if(cc.getDecryptionKey() % 2 == 0) {
				hint = "짝수";
				break;
			}
			else {
				hint = "홀수";
				break;
			}
		case 2:
			hint = "10의 자리 숫자 -> " + (cc.getDecryptionKey() / 10);
			break;
		case 3:
			hint = cc.getDecryptionKey() + " ";
		}
		
		return hint;
	}
}

MainSystem.java

public class MainSystem {

	public static void main(String[] args) {
		CeaserCodeSource ccs = new CeaserCodeSource();
		ccs.mainSystem();
	}

}

output

ZjYNm 를 해독하시오!
해독한 평문 입력 >> a
틀렸습니다. 다시 해독하세요!
암호키 힌트 : 짝수

해독한 평문 입력 >> b
틀렸습니다. 다시 해독하세요!
암호키 힌트 : 10의 자리 숫자 -> 7

해독한 평문 입력 >> c
틀렸습니다. 다시 해독하세요!
암호키 힌트 : 72 

해독한 평문 입력 >> d
틀렸습니다.
암호키 : 72 / 평문 : FpETs

'Etc > Java' 카테고리의 다른 글

오목게임 앱 제작(Android Studio HedgeHog)  (0) 2025.02.06
Bank Account(간단한 계좌 관리)  (0) 2025.01.26