Skip to content

Instantly share code, notes, and snippets.

@nielsutrecht
Created December 21, 2016 09:55
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nielsutrecht/0c1538b22e67c61b890a1b435a22fc99 to your computer and use it in GitHub Desktop.
Save nielsutrecht/0c1538b22e67c61b890a1b435a22fc99 to your computer and use it in GitHub Desktop.
Example of using RSA in Java to sign/verify and encrypt/decryt
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Signature;
public class RsaExample {
public static void main(String... argv) throws Exception {
//First generate a public/private key pair
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048, new SecureRandom());
KeyPair pair = generator.generateKeyPair();
//The private key can be used to sign (not encrypt!) a message. The public key holder can then verify the message.
String message = "To Be or not To Be";
//Let's sign our message
Signature privateSignature = Signature.getInstance("SHA256withRSA");
privateSignature.initSign(pair.getPrivate());
privateSignature.update(message.getBytes(StandardCharsets.UTF_8));
byte[] signature = privateSignature.sign();
//Let's check the signature
Signature publicSignature = Signature.getInstance("SHA256withRSA");
publicSignature.initVerify(pair.getPublic());
publicSignature.update(message.getBytes(StandardCharsets.UTF_8));
boolean isCorrect = publicSignature.verify(signature);
System.out.println("Signature correct: " + isCorrect);
//The public key can be used to encrypt a message, the private key can be used to decrypt it.
//Encrypt the message
Cipher encryptCipher = Cipher.getInstance("RSA");
encryptCipher.init(Cipher.ENCRYPT_MODE, pair.getPublic());
byte[] cipherText = encryptCipher.doFinal(message.getBytes());
//Now decrypt it
Cipher decriptCipher = Cipher.getInstance("RSA");
decriptCipher.init(Cipher.DECRYPT_MODE, pair.getPrivate());
String decipheredMessage = new String(decriptCipher.doFinal(cipherText), StandardCharsets.UTF_8);
System.out.println(decipheredMessage);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment