-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSAService.java
More file actions
52 lines (41 loc) · 2.31 KB
/
RSAService.java
File metadata and controls
52 lines (41 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package ftn.security.minikms.service;
import ftn.security.minikms.entity.KeyMaterial;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class RSAService implements ICryptoService {
public KeyMaterial generateKey() throws NoSuchAlgorithmException {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(3072);
var pair = generator.generateKeyPair();
return KeyMaterial.of(pair);
}
public String encrypt(String input, KeyMaterial key) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException {
Cipher encryptCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey publicKey = factory.generatePublic(new X509EncodedKeySpec(key.getPublicKey()));
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] secretMessageBytes = input.getBytes(StandardCharsets.UTF_8);
byte[] encryptedMessageBytes = encryptCipher.doFinal(secretMessageBytes);
return Base64.getEncoder().encodeToString(encryptedMessageBytes);
}
public String decrypt(String encrypted, KeyMaterial key) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
Cipher decryptCipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
KeyFactory factory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = factory.generatePrivate(new PKCS8EncodedKeySpec(key.getKey()));
decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] encryptedBytes = Base64.getDecoder().decode(encrypted);
byte[] decryptedMessageBytes = decryptCipher.doFinal(encryptedBytes);
return new String(decryptedMessageBytes, StandardCharsets.UTF_8);
}
}