@Test public void generateRSAKeyPairTest() { for (RSA_KEYLENGTH keyLength : RSA_KEYLENGTH.values()) { logger.debug("Testing RSA {}-bit key pair generation.", keyLength.value()); long start = System.currentTimeMillis(); KeyPair rsaKeyPair = generateRSAKeyPair(keyLength); long stop = System.currentTimeMillis(); assertNotNull(rsaKeyPair); assertNotNull(rsaKeyPair.getPrivate()); assertNotNull(rsaKeyPair.getPublic()); logger.debug("Private Key: {}.", EncryptionUtil.byteToHex(rsaKeyPair.getPrivate().getEncoded())); logger.debug("Public Key: {}.", EncryptionUtil.byteToHex(rsaKeyPair.getPublic().getEncoded())); logger.debug("Time: {} ms.", stop - start); } }
public static KeyPair generateRSAKeyPair(RSA_KEYLENGTH keyLength, String securityProvider) { try { KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", securityProvider); RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(keyLength.value(), RSA_PUBLIC_EXP); gen.initialize(params, new SecureRandom()); return gen.generateKeyPair(); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchProviderException e) { logger.error("Exception while generation of RSA key pair of length {}:", keyLength, e); } return null; }
EncryptionUtil { public static KeyPair generateRSAKeyPair(RSA_KEYLENGTH keyLength, String securityProvider) { try { KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", securityProvider); RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(keyLength.value(), RSA_PUBLIC_EXP); gen.initialize(params, new SecureRandom()); return gen.generateKeyPair(); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchProviderException e) { logger.error("Exception while generation of RSA key pair of length {}:", keyLength, e); } return null; } }
EncryptionUtil { public static KeyPair generateRSAKeyPair(RSA_KEYLENGTH keyLength, String securityProvider) { try { KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", securityProvider); RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(keyLength.value(), RSA_PUBLIC_EXP); gen.initialize(params, new SecureRandom()); return gen.generateKeyPair(); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchProviderException e) { logger.error("Exception while generation of RSA key pair of length {}:", keyLength, e); } return null; } private  EncryptionUtil(); }
EncryptionUtil { public static KeyPair generateRSAKeyPair(RSA_KEYLENGTH keyLength, String securityProvider) { try { KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", securityProvider); RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(keyLength.value(), RSA_PUBLIC_EXP); gen.initialize(params, new SecureRandom()); return gen.generateKeyPair(); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchProviderException e) { logger.error("Exception while generation of RSA key pair of length {}:", keyLength, e); } return null; } private  EncryptionUtil(); static byte[] generateIV(); static SecretKey generateAESKey(AES_KEYLENGTH keyLength, String securityProvider); static KeyPair generateRSAKeyPair(RSA_KEYLENGTH keyLength, String securityProvider); static byte[] encryptAES(byte[] data, SecretKey secretKey, byte[] initVector, String securityProvider,
			IStrongAESEncryption strongAES); static byte[] decryptAES(byte[] data, SecretKey secretKey, byte[] initVector, String securityProvider,
			IStrongAESEncryption strongAES); static byte[] encryptRSA(byte[] data, PublicKey publicKey, String securityProvider); static byte[] decryptRSA(byte[] data, PrivateKey privateKey, String securityProvider); static HybridEncryptedContent encryptHybrid(byte[] data, PublicKey publicKey, AES_KEYLENGTH aesKeyLength,
			String securityProvider, IStrongAESEncryption strongAES); static byte[] decryptHybrid(HybridEncryptedContent data, PrivateKey privateKey, String securityProvider,
			IStrongAESEncryption strongAES); static byte[] sign(byte[] data, PrivateKey privateKey, String securityProvider); static boolean verify(byte[] data, byte[] signature, PublicKey publicKey, String securityProvider); static String byteToHex(byte[] data); }
EncryptionUtil { public static KeyPair generateRSAKeyPair(RSA_KEYLENGTH keyLength, String securityProvider) { try { KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", securityProvider); RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(keyLength.value(), RSA_PUBLIC_EXP); gen.initialize(params, new SecureRandom()); return gen.generateKeyPair(); } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchProviderException e) { logger.error("Exception while generation of RSA key pair of length {}:", keyLength, e); } return null; } private  EncryptionUtil(); static byte[] generateIV(); static SecretKey generateAESKey(AES_KEYLENGTH keyLength, String securityProvider); static KeyPair generateRSAKeyPair(RSA_KEYLENGTH keyLength, String securityProvider); static byte[] encryptAES(byte[] data, SecretKey secretKey, byte[] initVector, String securityProvider,
			IStrongAESEncryption strongAES); static byte[] decryptAES(byte[] data, SecretKey secretKey, byte[] initVector, String securityProvider,
			IStrongAESEncryption strongAES); static byte[] encryptRSA(byte[] data, PublicKey publicKey, String securityProvider); static byte[] decryptRSA(byte[] data, PrivateKey privateKey, String securityProvider); static HybridEncryptedContent encryptHybrid(byte[] data, PublicKey publicKey, AES_KEYLENGTH aesKeyLength,
			String securityProvider, IStrongAESEncryption strongAES); static byte[] decryptHybrid(HybridEncryptedContent data, PrivateKey privateKey, String securityProvider,
			IStrongAESEncryption strongAES); static byte[] sign(byte[] data, PrivateKey privateKey, String securityProvider); static boolean verify(byte[] data, byte[] signature, PublicKey publicKey, String securityProvider); static String byteToHex(byte[] data);  }