Skip to content

Instantly share code, notes, and snippets.

@ensingerphilipp
Created September 24, 2019 11:27
Show Gist options
  • Save ensingerphilipp/202ef2f16a93a21e93ac8bfe5a6012fb to your computer and use it in GitHub Desktop.
Save ensingerphilipp/202ef2f16a93a21e93ac8bfe5a6012fb to your computer and use it in GitHub Desktop.
Secure RSA Encryption - Implementation in Java
package rsaEncryption;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import java.security.*;
import java.security.spec.MGF1ParameterSpec;
import java.util.Base64;
public class RsaEncryption {
public static byte[] encrypt(byte[] data, PublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException,
BadPaddingException, InvalidKeyException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
OAEPParameterSpec oaepParameterSpec = new OAEPParameterSpec("SHA-512", "MGF1",
MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT);
cipher.init(Cipher.ENCRYPT_MODE, publicKey, oaepParameterSpec);
byte[] encryptData = cipher.doFinal(data);
return encryptData;
}
public static byte[] decrypt(byte[] data, PrivateKey privateKey) throws BadPaddingException, IllegalBlockSizeException,
InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
OAEPParameterSpec oaepParameterSpec = new OAEPParameterSpec("SHA-512", "MGF1",
MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey, oaepParameterSpec);
byte[] decryptData = cipher.doFinal(data);
return decryptData;
}
public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException, InvalidAlgorithmParameterException {
KeyPair keyPair = RsaKeyGenerator.generateKeyPair();
String testMessage = "hallo!";
byte[] encryptedBytes = encrypt(testMessage.getBytes(), keyPair.getPublic());
String decryptedMessage = new String(decrypt(encryptedBytes, keyPair.getPrivate()));
System.out.println("testMessage: " + testMessage);
System.out.println("encryptedBytes: " + Base64.getEncoder().encodeToString(encryptedBytes));
System.out.println("decryptedMessage: "+ decryptedMessage);
}
}
package rsaEncryption;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class RsaKeyGenerator {
//Generate RSA Key with size of at least 3072 bits
//Use "SecureRandom.getInstanceStrong()" for more secure Randomness
//For Availability Oriented Implementation use the nonBlocking "SecureRandom.getInstance()"
//If executed on Windows Systems, it is strongly advised not to use "SecureRandom.getInstance()" as this will trigger
//the insecure SHA1PRNG
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(3072, SecureRandom.getInstanceStrong());
return generator.generateKeyPair();
}
public static void main(String[] args) throws NoSuchAlgorithmException {
KeyPair keyPair = generateKeyPair();
System.out.println(keyPair.getPrivate());
System.out.println(keyPair.getPublic());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment