Listing 1. RSA Key Generator
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
public class RSAKeyGenerator {
private static final int KEYSIZE = 8192;
public static void main(String[] args) {
generateKey("RSA_private.key","RSA_public.key");
}
public static void generateKey(String privateKey, String publicKey) {
try {
KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
pairgen.initialize(KEYSIZE, random);
KeyPair keyPair = pairgen.generateKeyPair();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(publicKey));
out.writeObject(keyPair.getPublic());
out.close();
out = new ObjectOutputStream(new FileOutputStream(privateKey));
out.writeObject(keyPair.getPrivate());
out.close();
} catch (IOException e) {
System.err.println(e);
} catch (GeneralSecurityException e) {
System.err.println(e);
}
}
}
Listing 2. Encryption Method
public void encryptToOutputFile(String publicKeyFile, String inputFile, String outputFile) throws FileNotFoundException,
IOException, ClassNotFoundException, GeneralSecurityException {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
// Wrap with public key
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(publicKeyFile));
Key publicKey = (Key) keyIn.readObject();
keyIn.close();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.WRAP_MODE, publicKey);
byte[] wrappedKey = cipher.wrap(key);
DataOutputStream out = new DataOutputStream(new FileOutputStream(outputFile));
out.writeInt(wrappedKey.length);
out.write(wrappedKey);
InputStream in = new FileInputStream(inputFile);
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in, out, cipher);
in.close();
out.close();
}
Listing 3. Decryption Method
public void decryptFromOutputFile(String privatecKeyFile, String inputFile, String
outputFile) throws IOException, ClassNotFoundException,
GeneralSecurityException {
DataInputStream in = new DataInputStream(new FileInputStream(inputFile));
int length = in.readInt();
byte[] wrappedKey = new byte[length];
in.read(wrappedKey, 0, length);
// Open with private key
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(privatec
KeyFile));
Key privateKey = (Key) keyIn.readObject();
keyIn.close();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.UNWRAP_MODE, privateKey);
Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
OutputStream out = new FileOutputStream(outputFile);
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
crypt(in, out, cipher);
in.close();
out.close();
}
Listing 4. Key File Transformer
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.security.GeneralSecurityException;
import java.security.Key;
/*
* Private/Public Key File to Encoded Key Byte[]
*/
public class KeyToByteArray {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException,
GeneralSecurityException {
/*
* Define Arguments
*/
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream("RSA_private.key"));
Key privateKey = (Key) keyIn.readObject();
keyIn.close();
byte[] k = privateKey.getEncoded();
System.out.println(privateKey.getFormat());
System.out.println(k.length);
for(int i = 0; i < k.length; i++) {
System.out.print(k[i]);
}
System.out.println();
System.out.println("Created byte[] of length : " + k.length);
System.out.println("Convert byte[] to String : " + bytesToHex(k));
System.out.println("---------------------------------");
System.out.println();
System.out.print("byte[] encPKe = { ");
int j = 0;
for (int i = 0; i < k.length; i++) {
if(i == k.length-1)
System.out.print("(byte)0x" + byteToHex(k[i]) + " ");
else
System.out.print("(byte)0x" + byteToHex(k[i]) + ", ");
j++;
if(j == 6) {
System.out.println();
j = 0;
}
}
System.out.println("};");
System.out.println();
}
public static String bytesToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
buf.append(byteToHex(data[i]).toUpperCase());
}
return (buf.toString());
}
public static String byteToHex(byte data) {
StringBuffer buf = new StringBuffer();
buf.append(toHexChar((data >>> 4) & 0x0F));
buf.append(toHexChar(data & 0x0F));
return buf.toString();
}
public static char toHexChar(int i) {
if ((0 <= i) && (i <= 9)) {
return (char) ('0' + i);
} else {
return (char) ('a' + (i – 10));
}
}
}
Listing 5. Modified Encryption Method
public void encryptWKf(byte[] encPk, String inputFile, String outputFile) throws FileNotFoundException, IOException,
ClassNotFoundException, GeneralSecurityException { …
Listing 6. Modified Decryption Method
public String decryptWKf(byte[] encPk, String inputFile) throws IOException, ClassNotFoundException, GeneralSecurityException { …
Listing 7. Modified Encryption Method 2
public void encryptWKf(byte[] encPk, String in, String outputFile) throws FileNotFoundException, IOException,
ClassNotFoundException, GeneralSecurityException { …
Listing 8. PKCS8 Key Specifications
// make key out of encrypted private key byte[]
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encPk);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
Listing 9. X509 Key Specifications
// make key out of encrypted public key byte[]
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encPk);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
Source : Hacking Magazine