diff options
author | Magdalena Grodzińska <mag.grodzinska@gmail.com> | 2020-01-13 21:54:45 +0100 |
---|---|---|
committer | Magdalena Grodzińska <mag.grodzinska@gmail.com> | 2020-01-13 22:11:07 +0100 |
commit | 8103eb7e174c7c0bb33316ae9499bbc0fbb62ea5 (patch) | |
tree | aaccde672427ef59188bbf92ee480c046626df02 /src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java | |
parent | 23b8714b29816b608cb3aa330b6e93610e107126 (diff) |
Fix key generation and reading
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java')
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java new file mode 100644 index 0000000..7a543ba --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/KeyUtils.java @@ -0,0 +1,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; + } +} |