Skip to content

Instantly share code, notes, and snippets.

@Slayug
Last active October 6, 2020 10:55
Show Gist options
  • Save Slayug/198d4c8f1edf18ed36d500fe8e638a99 to your computer and use it in GitHub Desktop.
Save Slayug/198d4c8f1edf18ed36d500fe8e638a99 to your computer and use it in GitHub Desktop.
This progam demonstrate the RSA keypair generation, and get the keys as String format.
import com.jcraft.jsch.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
class KeyGen {
public static void main(String[] arg) {
String email = "your@email.com";
JSch jsch = new JSch();
try (ByteArrayOutputStream privateKeyStream = new ByteArrayOutputStream();
ByteArrayOutputStream publicKeyStream = new ByteArrayOutputStream()) {
com.jcraft.jsch.KeyPair kpair = com.jcraft.jsch.KeyPair.genKeyPair(jsch, com.jcraft.jsch.KeyPair.RSA);
kpair.writePrivateKey(privateKeyStream);
kpair.writePublicKey(publicKeyStream, email);
System.out.println(privateKeyStream.toString());
System.out.println(publicKeyStream.toString());
kpair.dispose();
} catch (JSchException | IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment