2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 
   4  * This program and the accompanying materials are made available under the
 
   5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 
   6  * and is available at http://www.eclipse.org/legal/epl-v10.html
 
   9 package org.opendaylight.aaa.authn.mdsal.store;
 
  11 import java.security.spec.KeySpec;
 
  12 import javax.crypto.Cipher;
 
  13 import javax.crypto.SecretKey;
 
  14 import javax.crypto.SecretKeyFactory;
 
  15 import javax.crypto.spec.IvParameterSpec;
 
  16 import javax.crypto.spec.PBEKeySpec;
 
  17 import javax.crypto.spec.SecretKeySpec;
 
  18 import javax.xml.bind.DatatypeConverter;
 
  19 import org.slf4j.Logger;
 
  20 import org.slf4j.LoggerFactory;
 
  23  * @author - Sharon Aicler (saichler@cisco.com)
 
  25 public class DataEncrypter {
 
  27     final protected SecretKey k;
 
  28     private static final Logger LOG = LoggerFactory.getLogger(DataEncrypter.class);
 
  29     private static final byte[] iv = { 0, 5, 0, 0, 7, 81, 0, 3, 0, 0, 0, 0, 0, 43, 0, 1 };
 
  30     private static final IvParameterSpec ivspec = new IvParameterSpec(iv);
 
  31     public static final String ENCRYPTED_TAG = "Encrypted:";
 
  33     public DataEncrypter(final String ckey) {
 
  35         if (ckey != null && !ckey.isEmpty()) {
 
  38                 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
 
  39                 KeySpec spec = new PBEKeySpec(ckey.toCharArray(), iv, 32768, 128);
 
  40                 tmp = keyFactory.generateSecret(spec);
 
  41             } catch (Exception e) {
 
  42                 LOG.error("Couldn't initialize key factory", e);
 
  45                 k = new SecretKeySpec(tmp.getEncoded(), "AES");
 
  47                 throw new RuntimeException("Couldn't initalize encryption key");
 
  51             LOG.warn("Void crypto key passed! AuthN Store Encryption disabled");
 
  56     protected String encrypt(String token) {
 
  62         String cryptostring = null;
 
  64             Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
 
  65             c.init(Cipher.ENCRYPT_MODE, k, ivspec);
 
  66             byte[] cryptobytes = c.doFinal(token.getBytes());
 
  67             cryptostring = DatatypeConverter.printBase64Binary(cryptobytes);
 
  68             return ENCRYPTED_TAG + cryptostring;
 
  69         } catch (Exception e) {
 
  70             LOG.error("Couldn't encrypt token", e);
 
  75     protected String decrypt(String eToken) {
 
  80         if (eToken == null || eToken.length() == 0) {
 
  84         if (!eToken.startsWith(ENCRYPTED_TAG)) {
 
  89             Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
 
  90             c.init(Cipher.DECRYPT_MODE, k, ivspec);
 
  92             byte[] cryptobytes = DatatypeConverter.parseBase64Binary(eToken.substring(ENCRYPTED_TAG.length()));
 
  93             byte[] clearbytes = c.doFinal(cryptobytes);
 
  94             return DatatypeConverter.printBase64Binary(clearbytes);
 
  96         } catch (Exception e) {
 
  97             LOG.error("Couldn't decrypt token", e);