# generate RSA key pairs
$ openssl genrsa -out private.pem 2048
# generate PKCS8 private key
$ openssl pkcs8 -topk8 -inform PEM -outform PEM -in private.pem -out private_key.pem -nocrypt
# generate public key
$ openssl rsa -in private.pem -outform PEM -pubout -out public.pem
Created
January 15, 2021 06:33
-
-
Save Coffee0127/e4be03e228a69f7cf77d04ef9e3cbbdd to your computer and use it in GitHub Desktop.
Java sign / verify with RSA key pair
This file contains hidden or 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
public class Demo { | |
public static void main(String[] args) throws Exception { | |
String plainText = "Hello World"; | |
String signature = sign(plainText, readPrivateKey(new File("/Users/bfan01/Desktop/private_key.pem"))); | |
System.out.println(signature); | |
System.out.println(verify(plainText, signature, readPublicKey(new File("/Users/bfan01/Desktop/public.pem")))); | |
} | |
public static String sign(String plainText, PrivateKey privateKey) throws Exception { | |
Signature privateSignature = Signature.getInstance("SHA256withRSA"); | |
privateSignature.initSign(privateKey); | |
privateSignature.update(plainText.getBytes(StandardCharsets.UTF_8)); | |
byte[] signature = privateSignature.sign(); | |
return Base64.encodeBase64String(signature); | |
} | |
public static boolean verify(String plainText, String signature, PublicKey publicKey) throws Exception { | |
Signature publicSignature = Signature.getInstance("SHA256withRSA"); | |
publicSignature.initVerify(publicKey); | |
publicSignature.update(plainText.getBytes(StandardCharsets.UTF_8)); | |
byte[] signatureBytes = Base64.decodeBase64(signature); | |
return publicSignature.verify(signatureBytes); | |
} | |
public static RSAPrivateKey readPrivateKey(File file) throws Exception { | |
String key = Files.readString(file.toPath(), Charset.defaultCharset()); | |
String privateKeyPEM = key | |
.replace("-----BEGIN PRIVATE KEY-----", "") | |
.replaceAll(System.lineSeparator(), "") | |
.replace("-----END PRIVATE KEY-----", ""); | |
byte[] encoded = Base64.decodeBase64(privateKeyPEM); | |
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | |
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded); | |
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec); | |
} | |
public static RSAPublicKey readPublicKey(File file) throws Exception { | |
String key = Files.readString(file.toPath(), Charset.defaultCharset()); | |
String publicKeyPEM = key | |
.replace("-----BEGIN PUBLIC KEY-----", "") | |
.replaceAll(System.lineSeparator(), "") | |
.replace("-----END PUBLIC KEY-----", ""); | |
byte[] encoded = Base64.decodeBase64(publicKeyPEM); | |
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | |
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded); | |
return (RSAPublicKey) keyFactory.generatePublic(keySpec); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment