/* * Copyright (C) 2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ro.pippo.session; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.Key; import java.util.Base64; /** * @author Decebal Suiu */ public class BaseEncryptor implements Encryptor { private final String algorithm; public BaseEncryptor(String algorithm) { this.algorithm = algorithm; } @Override public String encrypt(String data, String key) throws Exception { Key secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), algorithm); Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encrypted); } @Override public String decrypt(String data, String key) throws Exception { Key secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), algorithm); Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decoded = Base64.getDecoder().decode(data); return new String(cipher.doFinal(decoded), StandardCharsets.UTF_8); } }