/** * This file is part of Graylog. * * Graylog is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Graylog is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Graylog. If not, see <http://www.gnu.org/licenses/>. */ package org.graylog2.security; import org.apache.shiro.codec.Hex; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Nullable; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AESTools { private static final Logger LOG = LoggerFactory.getLogger(AESTools.class); @Nullable public static String encrypt(String plainText, String encryptionKey, String salt) { try { @SuppressWarnings("CIPHER_INTEGRITY") Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8"))); return Hex.encodeToString(cipher.doFinal(plainText.getBytes("UTF-8"))); } catch (Exception e) { LOG.error("Could not encrypt value.", e); } return null; } @Nullable public static String decrypt(String cipherText, String encryptionKey, String salt) { try { @SuppressWarnings("CIPHER_INTEGRITY") Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8"))); return new String(cipher.doFinal(Hex.decode(cipherText)), "UTF-8"); } catch (Exception e) { LOG.error("Could not decrypt value.", e); } return null; } }