Skip to content

Instantly share code, notes, and snippets.

@furqanbaqai
Created April 3, 2018 18:01
Show Gist options
  • Save furqanbaqai/96dfe6c25284cfcf5e33720347dcd954 to your computer and use it in GitHub Desktop.
Save furqanbaqai/96dfe6c25284cfcf5e33720347dcd954 to your computer and use it in GitHub Desktop.
//
// Generate Certificates / Keys using following URL:
// https://adangel.org/2016/08/29/openssl-rsa-java/
// NOTE: Code is compiled using IBM Java rather than Oracle
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
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;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.apache.commons.io.FileUtils;
/**
* OpenSSLTest.java
*
* Authur: Furqan Baqai
* Created On: Apr 3, 2018
* Email: [email protected]
*
* CHANGE LOG
* ----------------
* [Apr 3, 2018:Muhammad] - Initial checkin
*/
/**
* @author Muhammad
*
*/
public class OpenSSLTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
// Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
String clearText = "Sample plain text";
PublicKey publicKey = OpenSSLTest.loadPublicKey();
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encrypted = cipher.doFinal(clearText.getBytes(StandardCharsets.UTF_8));
System.out.println("Encrypted text: "+encrypted);
PrivateKey privateKey = OpenSSLTest.loadPrivateKey();
cipher.init(Cipher.DECRYPT_MODE,privateKey);
byte[] decrypted = cipher.doFinal(encrypted);
System.out.println("Decrypted output = "+ new String(decrypted,StandardCharsets.UTF_8));
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static PublicKey loadPublicKey(){
// TODO Auto-generated method stub
System.out.println("Loading puublic keys for encryption");
try {
String publicKeyPEM = FileUtils.readFileToString(new File("./sslCerts/publickey.pem"),
StandardCharsets.UTF_8);
publicKeyPEM = publicKeyPEM
.replace("-----BEGIN PUBLIC KEY-----", "")
.replace("-----END PUBLIC KEY-----", "")
.replaceAll("\\s", "");
System.out.println("Key is \n"+publicKeyPEM);
byte[] publickeyDER = Base64.getDecoder().decode(publicKeyPEM);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publickeyDER));
System.out.println("Key loaded sucesfully");
return publicKey;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static PrivateKey loadPrivateKey() throws Exception {
String privateKeyPEM = FileUtils.readFileToString(new File("./sslCerts/privatekey-pkcs8.pem"), StandardCharsets.UTF_8);
// strip of header, footer, newlines, whitespaces
privateKeyPEM = privateKeyPEM
.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replaceAll("\\s", "");
System.out.println("Private key:\n"+privateKeyPEM);
// decode to get the binary DER representation
byte[] privateKeyDER = Base64.getDecoder().decode(privateKeyPEM);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyDER));
return privateKey;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment