From 81d1abbbad1f1e9c7774c7162b23fc804f7989a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 13 Jan 2020 20:56:01 +0100 Subject: Add reading keys from file --- .../java/pl/edu/mimuw/cloudatlas/KeyGenerator.java | 59 ++++++++++++++++++++-- .../mimuw/cloudatlas/querysigner/QueryData.java | 4 ++ 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java (limited to 'src/main') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java index 8581280..8a474ac 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java @@ -1,16 +1,30 @@ package pl.edu.mimuw.cloudatlas; +import java.io.*; import java.security.*; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; public class KeyGenerator { private final static int NUM_KEY_BITS = 1024; private final static String ENCRYPTION_ALGORITHM = "RSA"; // TODO - // TODO read key values from files - // TODO gradlew private static void printKeyToFile(byte[] keyValue, String fileName) { + System.out.println("\n BEGIN KEY"); + for (byte k : keyValue) { + System.out.print(k); + } + System.out.println("\n EOF KEY"); + String keyString = new String(keyValue); + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); + writer.write(keyString); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } private static void generateKeys() throws NoSuchAlgorithmException { @@ -22,13 +36,48 @@ public class KeyGenerator { keyGenerator.initialize(NUM_KEY_BITS); KeyPair keyPair = keyGenerator.generateKeyPair(); + String publicKeyFile = System.getProperty("public_key_file"); + String privateKeyFile = System.getProperty("private_key_file"); + privateKey = keyPair.getPrivate(); - printKeyToFile(privateKey.getEncoded(), "query_signer"); + printKeyToFile(privateKey.getEncoded(), privateKeyFile); publicKey = keyPair.getPublic(); - printKeyToFile(publicKey.getEncoded(), "query_signer.pub"); + printKeyToFile(publicKey.getEncoded(), publicKeyFile); + } + + public static PublicKey getPublicKey(String key){ + try { + byte[] byteKey = Base64.getDecoder().decode(key.getBytes()); + 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 key){ + try { + byte[] byteKey = Base64.getDecoder().decode(key.getBytes()); + X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey); + KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM); + return kf.generatePrivate(X509publicKey); + } catch(Exception e) { + e.printStackTrace(); + } + return null; + } + + public static String readKeyFromFile(String filename) throws IOException { + String key; + BufferedReader reader = new BufferedReader(new FileReader(filename)); + key = reader.readLine(); + reader.close(); + return key; } - public static void main() { + public static void main(String[] args) { try { generateKeys(); } catch (NoSuchAlgorithmException e) { diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java new file mode 100644 index 0000000..a09c823 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java @@ -0,0 +1,4 @@ +package pl.edu.mimuw.cloudatlas.querysigner; + +public class QueryData { +} -- cgit v1.2.3