@Test(expected=GeneralSecurityException.class) public void testEncryptBadKey() throws GeneralSecurityException { KeyGenerator kg; try { if(provider != null) { kg = KeyGenerator.getInstance("AES", provider); } else { kg = KeyGenerator.getInstance("AES"); } } catch(NoSuchAlgorithmException e) { throw new RuntimeException(e); } kg.init(128); SecretKey sk = kg.generateKey(); KeyPairGenerator kpg; try { if(provider != null) { kpg = KeyPairGenerator.getInstance("DSA", provider); } else { kpg = KeyPairGenerator.getInstance("DSA"); } } catch(NoSuchAlgorithmException e) { throw new RuntimeException(e); } KeyPair dsa = kpg.generateKeyPair(); KeyUtils.encryptKey(sk, null, dsa.getPublic()); }
public static String encryptKey(SecretKey key, Provider provider, PublicKey publicKey) throws GeneralSecurityException { Cipher cipher = null; if(provider != null) { cipher = Cipher.getInstance(TransformConstants.KEY_ENCRYPTION_TRANSFORM, provider); } else { cipher = Cipher.getInstance(TransformConstants.KEY_ENCRYPTION_TRANSFORM); } cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedKey = cipher.doFinal(key.getEncoded()); return urlSafeEncodeBase64(encryptedKey); }
KeyUtils { public static String encryptKey(SecretKey key, Provider provider, PublicKey publicKey) throws GeneralSecurityException { Cipher cipher = null; if(provider != null) { cipher = Cipher.getInstance(TransformConstants.KEY_ENCRYPTION_TRANSFORM, provider); } else { cipher = Cipher.getInstance(TransformConstants.KEY_ENCRYPTION_TRANSFORM); } cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedKey = cipher.doFinal(key.getEncoded()); return urlSafeEncodeBase64(encryptedKey); } }
KeyUtils { public static String encryptKey(SecretKey key, Provider provider, PublicKey publicKey) throws GeneralSecurityException { Cipher cipher = null; if(provider != null) { cipher = Cipher.getInstance(TransformConstants.KEY_ENCRYPTION_TRANSFORM, provider); } else { cipher = Cipher.getInstance(TransformConstants.KEY_ENCRYPTION_TRANSFORM); } cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedKey = cipher.doFinal(key.getEncoded()); return urlSafeEncodeBase64(encryptedKey); }  }
KeyUtils { public static String encryptKey(SecretKey key, Provider provider, PublicKey publicKey) throws GeneralSecurityException { Cipher cipher = null; if(provider != null) { cipher = Cipher.getInstance(TransformConstants.KEY_ENCRYPTION_TRANSFORM, provider); } else { cipher = Cipher.getInstance(TransformConstants.KEY_ENCRYPTION_TRANSFORM); } cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedKey = cipher.doFinal(key.getEncoded()); return urlSafeEncodeBase64(encryptedKey); }  static String getRsaPublicKeyFingerprint(RSAPublicKey pubKey, Provider provider); static String toHexPadded(byte[] data); static byte[] derEncodeBigInteger(BigInteger value); static byte[] derEncodeValue(byte type, byte[] bytes); static byte[] derEncodeRSAPublicKey(RSAPublicKey pubkey); static byte[] derEncodeSequence(List<byte[]> objects); static KeyPair rsaKeyPairFromBase64(String publicKey, String privateKey); static SecretKey decryptKey(String encodedKey, String algorithm, Provider provider, PrivateKey privateKey); static String encryptKey(SecretKey key, Provider provider, PublicKey publicKey); static String urlSafeEncodeBase64(byte[] data); static byte[] urlSafeDecodeBase64(String b64Data); static String signMetadata(Map<String, String> metadata,
            RSAPrivateKey privateKey, Provider provider); static byte[] extractSubjectKeyIdentifier(byte[] derSki); }
KeyUtils { public static String encryptKey(SecretKey key, Provider provider, PublicKey publicKey) throws GeneralSecurityException { Cipher cipher = null; if(provider != null) { cipher = Cipher.getInstance(TransformConstants.KEY_ENCRYPTION_TRANSFORM, provider); } else { cipher = Cipher.getInstance(TransformConstants.KEY_ENCRYPTION_TRANSFORM); } cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedKey = cipher.doFinal(key.getEncoded()); return urlSafeEncodeBase64(encryptedKey); }  static String getRsaPublicKeyFingerprint(RSAPublicKey pubKey, Provider provider); static String toHexPadded(byte[] data); static byte[] derEncodeBigInteger(BigInteger value); static byte[] derEncodeValue(byte type, byte[] bytes); static byte[] derEncodeRSAPublicKey(RSAPublicKey pubkey); static byte[] derEncodeSequence(List<byte[]> objects); static KeyPair rsaKeyPairFromBase64(String publicKey, String privateKey); static SecretKey decryptKey(String encodedKey, String algorithm, Provider provider, PrivateKey privateKey); static String encryptKey(SecretKey key, Provider provider, PublicKey publicKey); static String urlSafeEncodeBase64(byte[] data); static byte[] urlSafeDecodeBase64(String b64Data); static String signMetadata(Map<String, String> metadata,
            RSAPrivateKey privateKey, Provider provider); static byte[] extractSubjectKeyIdentifier(byte[] derSki);  }