Skip to content

Instantly share code, notes, and snippets.

@punchouty
Created September 19, 2014 16:44
Show Gist options
  • Save punchouty/668a2c42162683bbeafc to your computer and use it in GitHub Desktop.
Save punchouty/668a2c42162683bbeafc to your computer and use it in GitHub Desktop.
Read RSA key from file
package com.punchouty.ed;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class KeyReader {
public static final String BASE_FOLDER = "keys/";
public static final String PRIVATE_KEY_FILE_NAME = "private.key";
public static final String PUBLIC_KEY_FILE_NAME = "public.key";
public static final String PRIVATE_KEY_FILE = BASE_FOLDER + PRIVATE_KEY_FILE_NAME;
public static final String PUBLIC_KEY_FILE = BASE_FOLDER + PUBLIC_KEY_FILE_NAME;
public static final String KEY_GENERATION_ALGORITHM = "RSA";
public static final String ENCRYPTION_DECRYPTION_ALGORITHM = "RSA/ECB/PKCS1Padding";
public PrivateKey readPrivateKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException{
File keyFile = new File(PRIVATE_KEY_FILE);
boolean exists = keyFile.exists();
if(exists) {
FileInputStream fis = new FileInputStream(keyFile);
byte[] encodedPrivateKey = new byte[(int) keyFile.length()];
fis.read(encodedPrivateKey);
fis.close();
KeyFactory keyFactory = KeyFactory.getInstance(KEY_GENERATION_ALGORITHM);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
encodedPrivateKey);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
System.out.println("Read Private Key successfully: " + getHexString(privateKey.getEncoded()));
return privateKey;
}
else {
System.err.println("File " + keyFile + " does not exists. Hence exiting");
System.exit(0);
}
return null;
}
public PublicKey readPublicKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException{
File keyFile = new File(PUBLIC_KEY_FILE);
boolean exists = keyFile.exists();
if(exists) {
FileInputStream fis = new FileInputStream(keyFile);
byte[] encodedPublicKey = new byte[(int) keyFile.length()];
fis.read(encodedPublicKey);
fis.close();
KeyFactory keyFactory = KeyFactory.getInstance(KEY_GENERATION_ALGORITHM);
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
encodedPublicKey);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
System.out.println("Read Public Key successfully: " + getHexString(publicKey.getEncoded()));
return publicKey;
}
else {
System.err.println("File " + keyFile + " does not exists. Hence exiting");
System.exit(0);
}
return null;
}
public static final String getHexString(byte[] b) {
String result = "";
for (int i = 0; i < b.length; i++) {
result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment