@Test public void testDecodeBadKey() { String data = sut.decode("X8xF5hXq+v3HsPnb0F5wXw==", "2short".getBytes()); assertNull(data); }
public String decode(final String token, final byte[] keyBytes) { byte[] encrypted = javax.xml.bind.DatatypeConverter.parseBase64Binary(token); if (encrypted.length == 0) { return null; } Key key = new SecretKeySpec(keyBytes, algorithm); byte[] decrypted; try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, key); decrypted = cipher.doFinal(encrypted); } catch (GeneralSecurityException ex) { log.error("Error decrypting data from token", ex); return null; } return new String(decrypted); }
TokenEncoder { public String decode(final String token, final byte[] keyBytes) { byte[] encrypted = javax.xml.bind.DatatypeConverter.parseBase64Binary(token); if (encrypted.length == 0) { return null; } Key key = new SecretKeySpec(keyBytes, algorithm); byte[] decrypted; try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, key); decrypted = cipher.doFinal(encrypted); } catch (GeneralSecurityException ex) { log.error("Error decrypting data from token", ex); return null; } return new String(decrypted); } }
TokenEncoder { public String decode(final String token, final byte[] keyBytes) { byte[] encrypted = javax.xml.bind.DatatypeConverter.parseBase64Binary(token); if (encrypted.length == 0) { return null; } Key key = new SecretKeySpec(keyBytes, algorithm); byte[] decrypted; try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, key); decrypted = cipher.doFinal(encrypted); } catch (GeneralSecurityException ex) { log.error("Error decrypting data from token", ex); return null; } return new String(decrypted); } TokenEncoder(final String inAlgorithm); }
TokenEncoder { public String decode(final String token, final byte[] keyBytes) { byte[] encrypted = javax.xml.bind.DatatypeConverter.parseBase64Binary(token); if (encrypted.length == 0) { return null; } Key key = new SecretKeySpec(keyBytes, algorithm); byte[] decrypted; try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, key); decrypted = cipher.doFinal(encrypted); } catch (GeneralSecurityException ex) { log.error("Error decrypting data from token", ex); return null; } return new String(decrypted); } TokenEncoder(final String inAlgorithm); String encode(final String content, final byte[] keyBytes); boolean couldBeToken(final String potentialToken); String decode(final String token, final byte[] keyBytes); }
TokenEncoder { public String decode(final String token, final byte[] keyBytes) { byte[] encrypted = javax.xml.bind.DatatypeConverter.parseBase64Binary(token); if (encrypted.length == 0) { return null; } Key key = new SecretKeySpec(keyBytes, algorithm); byte[] decrypted; try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, key); decrypted = cipher.doFinal(encrypted); } catch (GeneralSecurityException ex) { log.error("Error decrypting data from token", ex); return null; } return new String(decrypted); } TokenEncoder(final String inAlgorithm); String encode(final String content, final byte[] keyBytes); boolean couldBeToken(final String potentialToken); String decode(final String token, final byte[] keyBytes);  }