Prompt Example
당신은 Java 프로그래머입니다. Java로 작성된 암호화 알고리즘의 코드를 작성해주십시오.
Result
자바를 이용하여 기본적인 암호화 알고리즘을 구현하는 프로그램을 만들 수 있습니다. 예를 들어, Caesar Cipher(시저 암호) 알고리즘을 구현할 수 있습니다.
아래는 Caesar Cipher 알고리즘을 이용한 문자열 암호화/복호화 프로그램 예시입니다.
import java.util.Scanner;
public class CaesarCipher {
public static void main(String[] args) {
Scanner input = new Scanner([System.in](http://system.in/));
System.out.print("암호화할 문자열을 입력하세요: ");
String plaintext = input.nextLine();
System.out.print("암호화할 키 값을 입력하세요: ");
int key = input.nextInt();
String ciphertext = encrypt(plaintext, key);
String decryptedText = decrypt(ciphertext, key);
System.out.println("암호화된 문자열: " + ciphertext);
System.out.println("복호화된 문자열: " + decryptedText);
}
public static String encrypt(String plaintext, int key) {
String ciphertext = "";
for (int i = 0; i < plaintext.length(); i++) {
char c = (char) (plaintext.charAt(i) + key);
if (c > 'z') {
ciphertext += (char) (plaintext.charAt(i) - (26 - key));
} else {
ciphertext += c;
}
}
return ciphertext;
}
public static String decrypt(String ciphertext, int key) {
String plaintext = "";
for (int i = 0; i < ciphertext.length(); i++) {
char c = (char) (ciphertext.charAt(i) - key);
if (c < 'a') {
plaintext += (char) (ciphertext.charAt(i) + (26 - key));
} else {
plaintext += c;
}
}
return plaintext;
}
}
이 프로그램은 사용자로부터 문자열과 키 값을 입력받아, 입력받은 문자열을 Caesar Cipher 알고리즘을 이용하여 암호화하고 복호화하는 기능을 제공합니다. 이 외에도, 다양한 암호화 알고리즘을 구현할 수 있습니다.