import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import org.apache.commons.codec.binary.Base64;
public class SecurityLumo {
public static String SIG_FILE_EXTENSION = ".sig";
public static String HASH_FILE_EXTENSION = ".crc";
public static String KEY_FILE_EXTENSION = ".key";
public static String PUBLIC_KEY_FILE = "public" + KEY_FILE_EXTENSION;
public static String PRIVATE_KEY_FILE = "private" + KEY_FILE_EXTENSION;
public static String DIGEST_ALGORITHM = "SHA1";
public static String ENCRYPTION_ALGORITHM = "DSA"; // DSA would be better
public static String SIGNATURE_ALGORITHM = DIGEST_ALGORITHM + "with"
+ ENCRYPTION_ALGORITHM;
public static void main(String args[]) {
SecurityLumo sec = new SecurityLumo();
try {
String path = "D:\\JAVA\\LicenseWorkspace\\licensing\\src\\keys\\";
KeyPairGenerator keyGen = KeyPairGenerator
.getInstance(ENCRYPTION_ALGORITHM);
keyGen.initialize(1024);
KeyPair generatedKeyPair = keyGen.genKeyPair();
System.out.println("Generated Key Pair");
sec.printKeyPair(generatedKeyPair);
sec.saveKeyPair(path, generatedKeyPair);
KeyPair loadedKeyPair = sec.loadKeyPair(path, ENCRYPTION_ALGORITHM);
System.out.println("Loaded Key Pair");
sec.printKeyPair(loadedKeyPair);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
private void printKeyPair(KeyPair keyPair) {
PublicKey pub = keyPair.getPublic();
System.out.println("Public Key: " + getHexString(pub.getEncoded()));
PrivateKey priv = keyPair.getPrivate();
System.out.println("Private Key: " + getHexString(priv.getEncoded()));
}
private 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;
}
public void saveKeyPair(String path, KeyPair keyPair) throws IOException {
putPrivateKey(path, keyPair.getPrivate());
putPublicKey(path, keyPair.getPublic());
}
public void putPrivateKey(String path, PrivateKey privateKey)
throws IOException {
// Store Private Key.
EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(
Base64.encodeBase64(privateKey.getEncoded()));
FileOutputStream fos = new FileOutputStream(path + PRIVATE_KEY_FILE);
fos.write(privKeySpec.getEncoded());
fos.close();
}
public PrivateKey getPrivateKey(String fileName)
throws NoSuchAlgorithmException, IOException,
InvalidKeySpecException {
KeyFactory keyFactory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
// Read Private Key.
File filePrivateKey = new File(fileName);
FileInputStream fis = new FileInputStream(
filePrivateKey.getAbsolutePath());
byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
fis.read(encodedPrivateKey);
fis.close();
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
Base64.decodeBase64(encodedPrivateKey));
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
return privateKey;
}
public void putPublicKey(String path, PublicKey publicKey)
throws IOException {
// Store Public Key.
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
Base64.encodeBase64(publicKey.getEncoded()));
FileOutputStream fos = new FileOutputStream(path + PUBLIC_KEY_FILE);
fos.write(x509EncodedKeySpec.getEncoded());
fos.close();
}
public PublicKey getPublicKey(String fileName)
throws InvalidKeySpecException, NoSuchAlgorithmException,
IOException, NoSuchProviderException {
// Read Public Key.
File filePublicKey = new File(fileName);
FileInputStream fis = new FileInputStream(
filePublicKey.getAbsolutePath());
byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
fis.read(encodedPublicKey);
fis.close();
KeyFactory keyFactory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
Base64.decodeBase64(encodedPublicKey));
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
return publicKey;
}
public KeyPair loadKeyPair(String path, String algorithm)
throws IOException, NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchProviderException {
return new KeyPair(getPublicKey(path + PRIVATE_KEY_FILE),
getPrivateKey(path + PRIVATE_KEY_FILE));
}
}