m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java
blob: 7a543ba296dfeb0f67bd565cf164c306678ae757 (plain)
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
package pl.edu.mimuw.cloudatlas.querysigner;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class KeyUtils {
    private final static String ENCRYPTION_ALGORITHM = "RSA";

    public static PublicKey getPublicKey(String filename){
        try {
            byte[] byteKey = Files.readAllBytes(Paths.get(filename));
            X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
            KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
            return kf.generatePublic(X509publicKey);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PrivateKey getPrivateKey(String filename){
        try {
            byte[] byteKey = Files.readAllBytes(Paths.get(filename));
            PKCS8EncodedKeySpec PKCS8privateKey = new PKCS8EncodedKeySpec(byteKey);
            KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
            return kf.generatePrivate(PKCS8privateKey);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}