@Test public void testEncodeBadKey() { String token = sut.encode("p4507s888", "2short".getBytes()); assertNull(token); }
public String encode(final String content, final byte[] keyBytes) { Key key = new SecretKeySpec(keyBytes, algorithm); byte[] encrypted; try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, key); encrypted = cipher.doFinal(content.getBytes()); } catch (GeneralSecurityException ex) { log.error("Error encrypting data into token (data: '{}')", content, ex); return null; } String token = javax.xml.bind.DatatypeConverter.printBase64Binary(encrypted); return token; }
TokenEncoder { public String encode(final String content, final byte[] keyBytes) { Key key = new SecretKeySpec(keyBytes, algorithm); byte[] encrypted; try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, key); encrypted = cipher.doFinal(content.getBytes()); } catch (GeneralSecurityException ex) { log.error("Error encrypting data into token (data: '{}')", content, ex); return null; } String token = javax.xml.bind.DatatypeConverter.printBase64Binary(encrypted); return token; } }
TokenEncoder { public String encode(final String content, final byte[] keyBytes) { Key key = new SecretKeySpec(keyBytes, algorithm); byte[] encrypted; try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, key); encrypted = cipher.doFinal(content.getBytes()); } catch (GeneralSecurityException ex) { log.error("Error encrypting data into token (data: '{}')", content, ex); return null; } String token = javax.xml.bind.DatatypeConverter.printBase64Binary(encrypted); return token; } TokenEncoder(final String inAlgorithm); }
TokenEncoder { public String encode(final String content, final byte[] keyBytes) { Key key = new SecretKeySpec(keyBytes, algorithm); byte[] encrypted; try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, key); encrypted = cipher.doFinal(content.getBytes()); } catch (GeneralSecurityException ex) { log.error("Error encrypting data into token (data: '{}')", content, ex); return null; } String token = javax.xml.bind.DatatypeConverter.printBase64Binary(encrypted); return token; } 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 encode(final String content, final byte[] keyBytes) { Key key = new SecretKeySpec(keyBytes, algorithm); byte[] encrypted; try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, key); encrypted = cipher.doFinal(content.getBytes()); } catch (GeneralSecurityException ex) { log.error("Error encrypting data into token (data: '{}')", content, ex); return null; } String token = javax.xml.bind.DatatypeConverter.printBase64Binary(encrypted); return token; } 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);  }