diff options
Diffstat (limited to 'src/main/java')
3 files changed, 37 insertions, 93 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java deleted file mode 100644 index 8a474ac..0000000 --- a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java +++ /dev/null @@ -1,87 +0,0 @@ -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 - 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 { - PublicKey publicKey; - PrivateKey privateKey; - - KeyPairGenerator keyGenerator = - KeyPairGenerator.getInstance(ENCRYPTION_ALGORITHM); - 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(), privateKeyFile); - publicKey = keyPair.getPublic(); - 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(String[] args) { - try { - generateKeys(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - } -} 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; + } +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java index d60a75d..735601d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QuerySigner.java @@ -1,8 +1,5 @@ package pl.edu.mimuw.cloudatlas.querysigner; -import pl.edu.mimuw.cloudatlas.KeyGenerator; -import pl.edu.mimuw.cloudatlas.agent.NewApiImplementation; -import pl.edu.mimuw.cloudatlas.api.Api; import pl.edu.mimuw.cloudatlas.querysignerapi.QuerySignerApi; import java.io.IOException; @@ -22,15 +19,14 @@ public class QuerySigner { private static QuerySignerApiImplementation initApi() throws IOException { String publicKeyFile = System.getProperty("public_key_file"); String privateKeyFile = System.getProperty("private_key_file"); - PublicKey publicKey = null; //KeyGenerator.getPublicKey(KeyGenerator.readKeyFromFile(publicKeyFile)); - PrivateKey privateKey = null; // KeyGenerator.getPrivateKey(KeyGenerator.readKeyFromFile(privateKeyFile)); + PublicKey publicKey = KeyUtils.getPublicKey(publicKeyFile); + PrivateKey privateKey = KeyUtils.getPrivateKey(privateKeyFile); return new QuerySignerApiImplementation(publicKey, privateKey); } public static void runRegistry() { try { QuerySignerApiImplementation api = initApi(); -// NewApiImplementation apii = new NewApiImplementation(null); QuerySignerApi apiStub = (QuerySignerApi) UnicastRemoteObject.exportObject(api, 0); Registry registry = LocateRegistry.getRegistry(); |