반응형
Java에서 암호화 및 암호 해독을 위한 프레임워크를 제공하는 javax.crypto 패키지를 사용하여 이전 예제에 암호화를 추가할 수 있습니다. 다음은 AES 알고리즘을 사용하여 데이터 암호화를 추가하는 서버 및 클라이언트의 업데이트된 예입니다
Server
import java.io.*;
import java.net.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.util.Base64;
class TCPServer {
public static void main(String argv[]) throws Exception {
ServerSocket welcomeSocket = new ServerSocket(6789);
while (true) {
Socket connectionSocket = welcomeSocket.accept();
ClientThread ct = new ClientThread(connectionSocket);
ct.start();
}
}
}
class ClientThread extends Thread {
Socket connectionSocket;
SecretKeySpec secretKey;
Cipher cipher;
public ClientThread(Socket socket) {
this.connectionSocket = socket;
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
secretKey = new SecretKeySpec(keyGenerator.generateKey().getEncoded(), "AES");
cipher = Cipher.getInstance("AES");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
System.out.println("Encryption error: " + e.getMessage());
}
}
public void run() {
try {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
while (true) {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedMessage = Base64.getDecoder().decode(inFromClient.readLine());
String clientSentence = new String(cipher.doFinal(decryptedMessage));
System.out.println("Received: " + clientSentence);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedMessage = cipher.doFinal(clientSentence.toUpperCase().getBytes());
String capitalizedSentence = Base64.getEncoder().encodeToString(encryptedMessage) + '\n';
outToClient.writeBytes(capitalizedSentence);
}
} catch (IOException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
System.out.println("Connection lost: " + e.getMessage());
}
}
}
Client
import java.io.*;
import java.net.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.util.Base64;
class TCPClient {
public static void main(String argv[]) throws Exception {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
SecretKeySpec secretKey;
Cipher cipher;
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
secretKey = new SecretKeySpec(keyGenerator.generateKey().getEncoded(), "AES");
cipher = Cipher.getInstance("AES");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
System.out.println("Encryption error: " + e.getMessage());
return;
}
while (true) {
System.out.print("Enter a sentence: ");
sentence = inFromUser.readLine();
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedMessage = cipher.doFinal(sentence.getBytes());
String encryptedSentence = Base64.getEncoder().encodeToString(encryptedMessage) + '\n';
outToServer.writeBytes(encryptedSentence);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedMessage = Base64.getDecoder().decode(inFromServer.readLine());
modifiedSentence = new String(cipher.doFinal(decryptedMessage));
System.out.println("From server: " + modifiedSentence);
}
}
}
반응형
'Spring Boot' 카테고리의 다른 글
[SpringBoot] Spring Boot에서 Naver Blog 글쓰기를 구현 (0) | 2023.02.09 |
---|---|
[JAVA] TCP/IP 데이터통신 파일암호화 전송 서버,클라이언트 (0) | 2023.02.09 |
[JAVA] TCP/IP 데이터통신 기본 서버,클라이언트 (0) | 2023.02.09 |
[SpringBoot] Apache PDFBox 이용한 PDF to Image 변환처리 (0) | 2023.02.09 |
[Svelte + Springboot] Svelte에서 파일다운로드 구현 (0) | 2023.02.09 |