@BeforeClass public void setup() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException { KeyFactory keyfactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyfactory.generatePrivate( Pems.privateKeySpec(ByteSource.wrap(PRIVATE_KEY.getBytes(Charsets.UTF_8)))); PublicKey publicKey = keyfactory.generatePublic( Pems.publicKeySpec(ByteSource.wrap(PUBLIC_KEY.getBytes(Charsets.UTF_8)))); keyPair = new KeyPair(publicKey, privateKey); openSshKey = SshKeys.encodeAsOpenSSH(RSAPublicKey.class.cast(publicKey)); }
@Override public PublicKey deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String keyText = json.getAsString().replaceAll("\\n", "\n"); try { return crypto .rsaKeyFactory() .generatePublic(Pems.publicKeySpec(ByteSource.wrap(keyText.getBytes(Charsets.UTF_8)))); } catch (UnsupportedEncodingException e) { Throwables.propagate(e); return null; } catch (InvalidKeySpecException e) { Throwables.propagate(e); return null; } catch (IOException e) { Throwables.propagate(e); return null; } }