private String decode( String string ) throws UnsupportedEncodingException { byte[] bytes = Base64.decodeBase64( string ); EncryptionResult result = EncryptionResult.fromByteArray(bytes); byte[] clear = cryptoService.decryptForCluster(clusterName, EncryptUriDescriptor.PASSWORD_ALIAS, result.cipher, result.iv, result.salt); if (clear != null) { return new String(clear, StandardCharsets.UTF_8); } return null; } }
private String encode( String string ) throws UnsupportedEncodingException { EncryptionResult result = cryptoService.encryptForCluster(clusterName, EncryptUriDescriptor.PASSWORD_ALIAS, string.getBytes(StandardCharsets.UTF_8)); string = Base64.encodeBase64URLSafeString(result.toByteAray()); return string; }
public static EncryptionResult fromByteArray(byte[] array) { EncryptionResult result = new EncryptionResult(); ByteBuffer bb = ByteBuffer.wrap(array); int saltSize = bb.getInt(); int ivSize = bb.getInt(); int cipherSize = bb.getInt(); result.salt = new byte[saltSize]; result.iv = new byte[ivSize]; result.cipher = new byte[cipherSize]; bb.get(result.salt); bb.get(result.iv); bb.get(result.cipher); return result; } }
private String encode( String string ) throws UnsupportedEncodingException { EncryptionResult result = null; try { result = encryptor.encrypt(string); } catch (Exception e) { log.unableToEncryptValue(e); } string = Base64.encodeBase64URLSafeString(result.toByteAray()); return string; } }
String decode( String string ) throws UnsupportedEncodingException { byte[] bytes = Base64.decodeBase64( string ); EncryptionResult result = EncryptionResult.fromByteArray(bytes); byte[] clear = null; try { clear = encryptor.decrypt( result.salt, result.iv, result.cipher); } catch (Exception e) { log.unableToDecryptValue(e); } if (clear != null) { return new String(clear, StandardCharsets.UTF_8); } return null; } }
public static EncryptionResult fromByteArray(byte[] array) { EncryptionResult result = new EncryptionResult(); ByteBuffer bb = ByteBuffer.wrap(array); int saltSize = bb.getInt(); int ivSize = bb.getInt(); int cipherSize = bb.getInt(); result.salt = new byte[saltSize]; result.iv = new byte[ivSize]; result.cipher = new byte[cipherSize]; bb.get(result.salt); bb.get(result.iv); bb.get(result.cipher); return result; } }
private String encode( String string ) throws UnsupportedEncodingException { EncryptionResult result = null; try { result = encryptor.encrypt(string); } catch (Exception e) { log.unableToEncryptValue(e); } string = Base64.encodeBase64URLSafeString(result.toByteAray()); return string; } }
String decode( String string ) throws UnsupportedEncodingException { byte[] bytes = Base64.decodeBase64( string ); EncryptionResult result = EncryptionResult.fromByteArray(bytes); byte[] clear = null; try { clear = encryptor.decrypt( result.salt, result.iv, result.cipher); } catch (Exception e) { log.unableToDecryptValue(e); } if (clear != null) { return new String(clear, StandardCharsets.UTF_8); } return null; } }
public EncryptionResult encrypt(byte[] plain) throws Exception { byte[] salt = new byte[saltSize]; SecureRandom rnd = new SecureRandom(); rnd.nextBytes(salt); SecretKey tmp = getKeyFromPassword(new String(passPhrase), salt); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), alg); Cipher ecipher = Cipher.getInstance(transformation); ecipher.init(Cipher.ENCRYPT_MODE, secret); return new EncryptionResult(salt, ecipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV(), ecipher.doFinal(plain)); }
private String compressEncryptBase64(final Object o) { if (o == null || o.equals("") || (o instanceof Map<?,?> && ((Map<?,?>)o).isEmpty())) { return null; } else { byte[] bytes = javaSerializationHelper.serializeToBytes((Serializable) o); /* compress the data */ try { bytes = compress(bytes); if(bytes.length > 3000) { logger.warn("Cookie too big, it might not be properly set"); } } catch (final IOException e) { throw new TechnicalException(e); } EncryptionResult result = cryptoService.encryptForCluster(this.clusterName, PAC4J_PASSWORD, bytes); return Base64.encodeBase64String(result.toByteAray()); } }
private Serializable uncompressDecryptBase64(final String v) { if (v != null && v.length() > 0) { byte[] bytes = Base64.decodeBase64(v); EncryptionResult result = EncryptionResult.fromByteArray(bytes); byte[] clear = cryptoService.decryptForCluster(this.clusterName, PAC4J_PASSWORD, result.cipher, result.iv, result.salt); if (clear != null) { try { return javaSerializationHelper.unserializeFromBytes(unCompress(clear)); } catch (IOException e) { throw new TechnicalException(e); } } } return null; }
public EncryptionResult encrypt(byte[] plain) throws Exception { byte[] salt = new byte[saltSize]; SecureRandom rnd = new SecureRandom(); rnd.nextBytes(salt); SecretKey tmp = getKeyFromPassword(new String(passPhrase), salt); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), alg); Cipher ecipher = Cipher.getInstance(transformation); ecipher.init(Cipher.ENCRYPT_MODE, secret); EncryptionResult atom = new EncryptionResult(salt, ecipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV(), ecipher.doFinal(plain)); return atom; }
private String compressEncryptBase64(final Object o) { if (o == null || o.equals("") || (o instanceof Map<?,?> && ((Map<?,?>)o).isEmpty())) { return null; } else { byte[] bytes = javaSerializationHelper.serializeToBytes((Serializable) o); /* compress the data */ try { bytes = compress(bytes); if(bytes.length > 3000) { logger.warn("Cookie too big, it might not be properly set"); } } catch (final IOException e) { throw new TechnicalException(e); } EncryptionResult result = cryptoService.encryptForCluster(this.clusterName, PAC4J_PASSWORD, bytes); return Base64.encodeBase64String(result.toByteAray()); } }
private Serializable uncompressDecryptBase64(final String v) { if (v != null && !v.isEmpty()) { byte[] bytes = Base64.decodeBase64(v); EncryptionResult result = EncryptionResult.fromByteArray(bytes); byte[] clear = cryptoService.decryptForCluster(this.clusterName, PAC4J_PASSWORD, result.cipher, result.iv, result.salt); if (clear != null) { try { return javaSerializationHelper.unserializeFromBytes(unCompress(clear)); } catch (IOException e) { throw new TechnicalException(e); } } } return null; }