|
| 1 | + |
| 2 | +package com.example.rsaservice.controller; |
| 3 | + |
| 4 | +import com.example.rsaservice.util.RSAUtil; |
| 5 | +import org.springframework.http.ResponseEntity; |
| 6 | +import org.springframework.web.bind.annotation.*; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +@RestController |
| 12 | +@RequestMapping("/api") |
| 13 | +public class RSAController { |
| 14 | + |
| 15 | + @PostMapping("/encrypt") |
| 16 | + public ResponseEntity<Map<String, String>> encrypt(@RequestBody Map<String, String> request) { |
| 17 | + try { |
| 18 | + String plaintext = request.get("plaintext"); |
| 19 | + String encryptedText = RSAUtil.encrypt(plaintext); |
| 20 | + Map<String, String> response = new HashMap<>(); |
| 21 | + response.put("encryptedText", encryptedText); |
| 22 | + return ResponseEntity.ok(response); |
| 23 | + } catch (Exception e) { |
| 24 | + return ResponseEntity.status(500).body(Map.of("error", "Encryption failed: " + e.getMessage())); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + @PostMapping("/decrypt") |
| 29 | + public ResponseEntity<Map<String, String>> decrypt(@RequestBody Map<String, String> request) { |
| 30 | + try { |
| 31 | + String encryptedText = request.get("encryptedText"); |
| 32 | + String decryptedText = RSAUtil.decrypt(encryptedText); |
| 33 | + Map<String, String> response = new HashMap<>(); |
| 34 | + response.put("decryptedText", decryptedText); |
| 35 | + return ResponseEntity.ok(response); |
| 36 | + } catch (Exception e) { |
| 37 | + return ResponseEntity.status(500).body(Map.of("error", "Decryption failed: " + e.getMessage())); |
| 38 | + } |
| 39 | + } |
| 40 | +} |
0 commit comments