@Test(expected=GeneralSecurityException.class) public void testDecodeBadKey() throws Exception { KeyUtils.rsaKeyPairFromBase64("aaaAAAaa", "bbBBBBbbb"); }
public static KeyPair rsaKeyPairFromBase64(String publicKey, String privateKey) throws GeneralSecurityException { try { byte[] pubKeyBytes = Base64.decodeBase64(publicKey.getBytes("US-ASCII")); byte[] privKeyBytes = Base64.decodeBase64(privateKey.getBytes("US-ASCII")); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyBytes); PublicKey pubKey; PrivateKey privKey; KeyFactory keyFactory = KeyFactory.getInstance("RSA"); pubKey = keyFactory.generatePublic(pubKeySpec); privKey = keyFactory.generatePrivate(privKeySpec); return new KeyPair(pubKey, privKey); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Could not load key pair: " + e, e); } }
KeyUtils { public static KeyPair rsaKeyPairFromBase64(String publicKey, String privateKey) throws GeneralSecurityException { try { byte[] pubKeyBytes = Base64.decodeBase64(publicKey.getBytes("US-ASCII")); byte[] privKeyBytes = Base64.decodeBase64(privateKey.getBytes("US-ASCII")); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyBytes); PublicKey pubKey; PrivateKey privKey; KeyFactory keyFactory = KeyFactory.getInstance("RSA"); pubKey = keyFactory.generatePublic(pubKeySpec); privKey = keyFactory.generatePrivate(privKeySpec); return new KeyPair(pubKey, privKey); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Could not load key pair: " + e, e); } } }
KeyUtils { public static KeyPair rsaKeyPairFromBase64(String publicKey, String privateKey) throws GeneralSecurityException { try { byte[] pubKeyBytes = Base64.decodeBase64(publicKey.getBytes("US-ASCII")); byte[] privKeyBytes = Base64.decodeBase64(privateKey.getBytes("US-ASCII")); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyBytes); PublicKey pubKey; PrivateKey privKey; KeyFactory keyFactory = KeyFactory.getInstance("RSA"); pubKey = keyFactory.generatePublic(pubKeySpec); privKey = keyFactory.generatePrivate(privKeySpec); return new KeyPair(pubKey, privKey); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Could not load key pair: " + e, e); } }  }
KeyUtils { public static KeyPair rsaKeyPairFromBase64(String publicKey, String privateKey) throws GeneralSecurityException { try { byte[] pubKeyBytes = Base64.decodeBase64(publicKey.getBytes("US-ASCII")); byte[] privKeyBytes = Base64.decodeBase64(privateKey.getBytes("US-ASCII")); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyBytes); PublicKey pubKey; PrivateKey privKey; KeyFactory keyFactory = KeyFactory.getInstance("RSA"); pubKey = keyFactory.generatePublic(pubKeySpec); privKey = keyFactory.generatePrivate(privKeySpec); return new KeyPair(pubKey, privKey); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Could not load key pair: " + e, e); } }  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 KeyPair rsaKeyPairFromBase64(String publicKey, String privateKey) throws GeneralSecurityException { try { byte[] pubKeyBytes = Base64.decodeBase64(publicKey.getBytes("US-ASCII")); byte[] privKeyBytes = Base64.decodeBase64(privateKey.getBytes("US-ASCII")); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyBytes); PublicKey pubKey; PrivateKey privKey; KeyFactory keyFactory = KeyFactory.getInstance("RSA"); pubKey = keyFactory.generatePublic(pubKeySpec); privKey = keyFactory.generatePrivate(privKeySpec); return new KeyPair(pubKey, privKey); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Could not load key pair: " + e, e); } }  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);  }