Created
September 19, 2014 16:42
-
-
Save punchouty/b88e859e333f0b00ea91 to your computer and use it in GitHub Desktop.
RSA key to file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.punchouty.ed; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.security.KeyPair; | |
import java.security.KeyPairGenerator; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.PrivateKey; | |
import java.security.PublicKey; | |
import java.security.spec.PKCS8EncodedKeySpec; | |
import java.security.spec.X509EncodedKeySpec; | |
public class KeyWriter { | |
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"; | |
private KeyPair generatedKeyPair = null; | |
public KeyWriter() { | |
try { | |
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_GENERATION_ALGORITHM); | |
keyGen.initialize(1024); | |
generatedKeyPair = keyGen.genKeyPair(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
} | |
public void savePrivateKey() throws IOException{ | |
File keyFile = new File(PRIVATE_KEY_FILE); | |
boolean exists = keyFile.exists(); | |
if(exists) { | |
System.err.println("File " + keyFile + " already exists. Please remove it and then run program again"); | |
System.exit(0); | |
} | |
else { | |
PrivateKey privateKey = generatedKeyPair.getPrivate(); | |
System.out.println("Saving Private Key: " + getHexString(privateKey.getEncoded())); | |
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec( | |
privateKey.getEncoded()); | |
FileOutputStream fos = new FileOutputStream(keyFile); | |
fos.write(pkcs8EncodedKeySpec.getEncoded()); | |
fos.close(); | |
System.out.println("Saved Private Key successfully"); | |
} | |
} | |
public void savePublicKey() throws IOException{ | |
File keyFile = new File(PUBLIC_KEY_FILE); | |
boolean exists = keyFile.exists(); | |
if(exists) { | |
System.err.println("File " + keyFile + " already exists. Please remove it and then run program again"); | |
System.exit(0); | |
} | |
else { | |
PublicKey publicKey = generatedKeyPair.getPublic(); | |
System.out.println("Saving Public Key: " + getHexString(publicKey.getEncoded())); | |
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( | |
publicKey.getEncoded()); | |
FileOutputStream fos = new FileOutputStream(keyFile); | |
fos.write(x509EncodedKeySpec.getEncoded()); | |
fos.close(); | |
System.out.println("Saved Public Key successfully"); | |
} | |
} | |
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