Skip to content

Commit 3bfc835

Browse files
committed
public and private key op
1 parent a6d0c7b commit 3bfc835

File tree

5 files changed

+115
-20
lines changed

5 files changed

+115
-20
lines changed

rsa-backend/src/main/java/com/example/rsa/controller/UserController.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
package com.example.rsa.controller;
22

33
import com.example.rsa.model.User;
4+
import com.example.rsa.service.RsaEncryptionService;
45
import com.example.rsa.service.UserService;
56
import io.swagger.v3.oas.annotations.Operation;
67
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.HttpStatus;
79
import org.springframework.http.ResponseEntity;
810
import org.springframework.web.bind.annotation.*;
911

12+
import java.math.BigInteger;
13+
1014
@RestController
1115
@RequestMapping("/user")
1216
public class UserController {
1317

1418
@Autowired
1519
private UserService userService;
1620

21+
@Autowired
22+
private RsaEncryptionService rsaEncryptionService;
23+
1724
@PostMapping
1825
public ResponseEntity<String> addUser(@RequestBody User user) {
1926
try {
@@ -50,5 +57,22 @@ public void cleanUsers() {
5057
userService.cleanAllUsers();
5158
}
5259

60+
@Operation(summary = "Get user key")
61+
@GetMapping("/key/{type}/{userId}")
62+
public ResponseEntity<String> getUserKey(@PathVariable String type, @PathVariable Integer userId) {
63+
BigInteger keyValue;
64+
if (type.equals("eKey")) {
65+
keyValue = rsaEncryptionService.getE();
66+
userService.updateUserKey(userId, type, keyValue);
67+
} else if (type.equals("dKey")) {
68+
keyValue = rsaEncryptionService.getD();
69+
userService.updateUserKey(userId, type, keyValue);
70+
} else {
71+
return ResponseEntity.badRequest().body("Invalid key type");
72+
}
73+
return ResponseEntity.ok("Hazır");
74+
}
75+
76+
5377
}
5478

rsa-backend/src/main/java/com/example/rsa/service/RSAEncryptionService.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@
99
@Data
1010
@Service
1111
public class RsaEncryptionService {
12-
private final BigInteger n;
13-
private final BigInteger d;
12+
private BigInteger n;
13+
private BigInteger d;
1414
private BigInteger e;
1515

1616
public RsaEncryptionService() {
17+
initializeKeys();
18+
}
19+
20+
private void initializeKeys() {
1721
SecureRandom random = new SecureRandom();
1822

1923
int bitLength = 2048;
2024
BigInteger p = BigInteger.probablePrime(bitLength, random);
2125
BigInteger q = BigInteger.probablePrime(bitLength, random);
22-
System.out.println(p);
23-
System.out.println(q);
2426

25-
n = p.multiply(q); // n = p * q
26-
System.out.println(n);
27-
BigInteger phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)); // phi(n) = (p-1)*(q-1)
27+
n = p.multiply(q);
28+
BigInteger phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
2829

2930
// Public Key
3031
e = BigInteger.probablePrime(bitLength / 2, random);
@@ -33,10 +34,10 @@ public RsaEncryptionService() {
3334
}
3435

3536
// Private Key
36-
d = e.modInverse(phi); // d = e^(-1) mod phi
37+
d = e.modInverse(phi);
3738

38-
System.out.println(e);
39-
System.out.println(d);
39+
System.out.println("Public Key :"+e);
40+
System.out.println("Private Key :"+d);
4041
}
4142

4243
public BigInteger encrypt(BigInteger message) {

rsa-backend/src/main/java/com/example/rsa/service/UserService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.example.rsa.model.User;
44
import org.springframework.stereotype.Service;
55

6+
import java.math.BigInteger;
67
import java.util.ArrayList;
78
import java.util.List;
89

@@ -57,5 +58,21 @@ public void updateUser(Integer id, String field ,boolean newValue) {
5758
System.out.println(getAllUsers());
5859
}
5960

61+
public void updateUserKey(Integer id, String type , BigInteger key) {
62+
System.out.println(id);
63+
System.out.println(type);
64+
System.out.println(key);
65+
for (User user : users) {
66+
if (user.getId().equals(id)) {
67+
switch (type) {
68+
case "eKey" -> user.setEKey(key);
69+
case "dKey" -> user.setDKey(key);
70+
}
71+
break;
72+
}
73+
}
74+
System.out.println(getAllUsers());
75+
}
76+
6077
}
6178

rsa-frontend/src/routes/components/ScenarioArea.js

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ const UserContainer = ({ user, handleUserToggle }) => {
2626
/>
2727
<div>
2828
<p>Açık Anahtar</p>
29-
<p className={"small-text"}>{user.publicKey ? user.eKey : "Kapalı"}</p>
30-
<p></p>
29+
<p className={"x-small-text font-bold"}>{user.publicKey ? user.eKey : "Kapalı"}</p>
3130
</div>
3231
</div>
3332

@@ -39,8 +38,7 @@ const UserContainer = ({ user, handleUserToggle }) => {
3938
/>
4039
<div>
4140
<p>Kapalı Anahtar</p>
42-
<p className={"small-text"}>{user.privateKey ? user.dKey : "Kapalı"}</p>
43-
<p></p>
41+
<p className={"x-small-text font-bold"}>{user.privateKey ? user.dKey : "Kapalı"}</p>
4442
</div>
4543
</div>
4644

@@ -53,9 +51,14 @@ const UserContainer = ({ user, handleUserToggle }) => {
5351
/>
5452
<div>
5553
<p>Dosya Yükleme</p>
56-
<p className={"small-text"}>{user.fileUpload ? user.fileUpload : "Kapalı"}</p>
57-
<p></p>
54+
<p className={"x-small-text font-bold"}>
55+
{user.fileUpload ? "Açık" : "Kapalı"}
56+
</p>
57+
<div className={"user-operation-button"} style={{ backgroundColor: user.fileUpload && "rgb(0,176,176)",cursor: user.fileUpload && "pointer" }}>
58+
Dosya Yükle
59+
</div>
5860
</div>
61+
5962
</div>
6063
<div className={"custom-row"} style={{gap:0}}>
6164
<Switch
@@ -65,8 +68,10 @@ const UserContainer = ({ user, handleUserToggle }) => {
6568
/>
6669
<div>
6770
<p>Dosya Okuma</p>
68-
<p className={"small-text"}>{user.readFiles ? user.readFiles : "Kapalı"}</p>
69-
<p></p>
71+
<p className={"x-small-text font-bold"}>{user.readFiles ? "Açık" : "Kapalı"}</p>
72+
<div className={"user-operation-button"} style={{ backgroundColor: user.readFiles && "rgb(16,64,59)",cursor: user.readFiles && "pointer" }}>
73+
Dosya Görüntüle
74+
</div>
7075
</div>
7176
</div>
7277
</div>
@@ -119,10 +124,26 @@ const ScenarioArea = () => {
119124
}
120125
};
121126

122-
const handleUserToggle = (userId, field,newValue) => {
127+
const updateEKeys = (id, newKey) => {
128+
setUsers(prevUsers =>
129+
prevUsers.map(user =>
130+
user.id === id ? { ...user, eKey: newKey} : user
131+
)
132+
);
133+
};
134+
135+
const updateDKeys = (id, newKey) => {
136+
setUsers(prevUsers =>
137+
prevUsers.map(user =>
138+
user.id === id ? { ...user, dKey: newKey} : user
139+
)
140+
);
141+
};
142+
143+
const handleUserToggle = (userId, field, newValue) => {
123144
setUsers(users.map(user =>
124145
user.id === userId
125-
? { ...user, [field]: !user[field] }
146+
? {...user, [field]: !user[field]}
126147
: user
127148
));
128149

@@ -133,6 +154,28 @@ const ScenarioArea = () => {
133154
.catch(error => {
134155
console.error("Kullanıcı güncellenirken hata oluştu:", error);
135156
});
157+
158+
if (field === "publicKey") {
159+
axios.get(`http://localhost:8080/user/key/eKey/${userId}`)
160+
.then(response => {
161+
updateEKeys(userId, response.data);
162+
console.log("Kullanıcı public key alındı:", response);
163+
})
164+
.catch(error => {
165+
console.error("Kullanıcı public key alınamadı:", error);
166+
});
167+
}
168+
if (field === "privateKey") {
169+
axios.get(`http://localhost:8080/user/key/dKey/${userId}`)
170+
.then(response => {
171+
updateDKeys(userId, response.data);
172+
console.log("Kullanıcı private key alındı:", response);
173+
})
174+
.catch(error => {
175+
console.error("Kullanıcı private key alınamadı:", error);
176+
});
177+
}
178+
136179
};
137180

138181
return (

rsa-frontend/src/routes/css/index.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,13 @@ span{
257257
color: var(--black-color-1);
258258
}
259259

260+
.user-operation-button{
261+
margin-top: 4px;
262+
padding: 8px 12px;
263+
border-radius: 50px;
264+
background-color: var(--grey-color-1);
265+
color: var(--primary-color-1);
266+
text-align: center;
267+
font-size: 13px;
268+
}
269+

0 commit comments

Comments
 (0)