java public private key encryption
Java
package com.mkyong.keypair;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
public class GenerateKeys {
private KeyPairGenerator keyGen;
private KeyPair pair;
private PrivateKey privateKey;
private PublicKey publicKey;
public GenerateKeys(int keylength) throws NoSuchAlgorithmException, NoSuchProviderException {
this.keyGen = KeyPairGenerator.getInstance("RSA");
this.keyGen.initialize(keylength);
}
public void createKeys() {
this.pair = this.keyGen.generateKeyPair();
this.privateKey = pair.getPrivate();
this.publicKey = pair.getPublic();
}
public PrivateKey getPrivateKey() {
return this.privateKey;
}
public PublicKey getPublicKey() {
return this.publicKey;
}
public void writeToFile(String path, byte[] key) throws IOException {
File f = new File(path);
f.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(f);
fos.write(key);
fos.flush();
fos.close();
}
public static void main(String[] args) {
GenerateKeys gk;
try {
gk = new GenerateKeys(1024);
gk.createKeys();
gk.writeToFile("KeyPair/publicKey", gk.getPublicKey().getEncoded());
gk.writeToFile("KeyPair/privateKey", gk.getPrivateKey().getEncoded());
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
Also in Java:
- Title
- javadoc reference general toString
- Category
- Java
- Title
- how lock horizontal orientation android
- Category
- Java
- Title
- removeeventlistener
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- JavaFX mouse over effe
- Category
- Java
- Title
- python vs java
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- object orientation in java
- Category
- Java
- Title
- how to limit double decimal places java
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- java double format
- Category
- Java
- Title
- java getter
- Category
- Java
- Title
- java s3 images
- Category
- Java
- Title
- capcitor FERR_CLEARTEXT_NOT_PERMITTED
- Category
- Java
- Title
- iterate map in java
- Category
- Java
- Title
- java map foreach
- Category
- Java
- Title
- exponents java
- Category
- Java
- Title
- output statement java
- Category
- Java
- Title
- java http client eviction
- Category
- Java
- Title
- how to open a folder in java swing project
- Category
- Java
- Title
- number to string java
- Category
- Java
- Title
- find a substring in a string java
- Category
- Java
- Title
- prendere valore da tastiera java
- Category
- Java
- Title
- 2d array java
- Category
- Java
- Title
- initialize applet in java
- Category
- Java
- Title
- how to draw a triangle in java
- Category
- Java
- Title
- java hashmap syntax
- Category
- Java
- Title
- parsedouble java
- Category
- Java
- Title
- java how to get all threads
- Category
- Java
- Title
- how to crate a list in java script
- Category
- Java
- Title
- remove space string java
- Category
- Java
- Title
- java return new instance of generic type
- Category
- Java
- Title
- guess the number java
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- Scanner library showing element not found exception
- Category
- Java
- Title
- android java shared preferences remove key
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- java remove non numbers from string
- Category
- Java
- Title
- java hello world
- Category
- Java
- Title
- spring code in java
- Category
- Java
- Title
- how to get a user input in java
- Category
- Java
- Title
- system.arraycopy java
- Category
- Java
- Title
- left shift in java
- Category
- Java
- Title
- how to replace in java
- Category
- Java
- Title
- int java
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- java rest client response json
- Category
- Java
- Title
- rewrite file java
- Category
- Java
- Title
- how to instanciate map.entry java
- Category
- Java
- Title
- bukkit java get player count
- Category
- Java
- Title
- jsonobject to java object
- Category
- Java
- Title
- java setter
- Category
- Java
- Title
- create notification android
- Category
- Java
- Title
- java map string to list
- Category
- Java
- Title
- same method name with different arguments
- Category
- Java
- Title
- java literals w3schools
- Category
- Java
- Title
- java how to put a string sentence in a map
- Category
- Java
- Title
- file to image javafx
- Category
- Java
- Title
- gradle require java version
- Category
- Java
- Title
- bootstrap alert
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- how to create a button in java
- Category
- Java
- Title
- java observable to observer stack overflow
- Category
- Java
- Title
- java logical operator
- Category
- Java
- Title
- eclipse does not support logback with spring boot
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- hide element selenium
- Category
- Java
- Title
- get last element of array java
- Category
- Java
- Title
- jframe border
- Category
- Java
- Title
- how to detect if someone clicks on a jpanel in java
- Category
- Java
- Title
- change number into array in java
- Category
- Java
- Title
- how to create an abstract method in java
- Category
- Java
- Title
- primitive and non primitive data types in java
- Category
- Java
- Title
- how to make a minecraft hacked client drawing the hud malicious intent
- Category
- Java
- Title
- how to add strings together
- Category
- Java
- Title
- how to loop trough an object java script
- Category
- Java
- Title
- query java persistence
- Category
- Java
- Title
- abs in java
- Category
- Java
- Title
- get host from request object java
- Category
- Java
- Title
- how to skip a line in java
- Category
- Java
- Title
- java check if string is number
- Category
- Java
- Title
- java 8 string to localdate
- Category
- Java
- Title
- how to input in java
- Category
- Java
- Title
- character at index of string java
- Category
- Java
- Title
- Java array nested equals
- Category
- Java
- Title
- java thread class sleep
- Category
- Java
- Title
- add a value to a list java in java hashmap
- Category
- Java
- Title
- binary number input in int java
- Category
- Java
- Title
- find frequency of each word in a string in java
- Category
- Java
- Title
- Removing DOM nodes when traversing a NodeList
- Category
- Java
- Title
- import collections in java
- Category
- Java
- Title
- como detener un void java
- Category
- Java
- Title
- ndroid.support.v4.content.FileProvider
- Category
- Java
- Title
- how to take space separated input in java
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- java for loop add multiple declarations
- Category
- Java
- Title
- java array declaration
- Category
- Java
- Title
- inheritance setter and getter in java
- Category
- Java