diff options
author | Magdalena Grodzińska <mag.grodzinska@gmail.com> | 2020-01-13 20:56:01 +0100 |
---|---|---|
committer | Magdalena Grodzińska <mag.grodzinska@gmail.com> | 2020-01-13 20:56:01 +0100 |
commit | 81d1abbbad1f1e9c7774c7162b23fc804f7989a3 (patch) | |
tree | 95c9765b58b0c782a8dfc4684406adfb5263ccee /src/main | |
parent | cc901690a8e3e41a1958ae33d95462031fba8e89 (diff) |
Add reading keys from file
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java | 59 | ||||
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java | 4 |
2 files changed, 58 insertions, 5 deletions
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 { +} |