AbstractCipher.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.utils.security;

  21. import java.security.SecureRandom;

  22. import javax.crypto.spec.IvParameterSpec;
  23. import javax.crypto.spec.SecretKeySpec;

  24. import org.openspcoop2.utils.UtilsException;
  25. import org.openspcoop2.utils.certificate.KeyStore;
  26. import org.openspcoop2.utils.io.Base64Utilities;
  27. import org.openspcoop2.utils.io.HexBinaryUtilities;
  28. import org.openspcoop2.utils.random.RandomUtilities;

  29. /**
  30.  * Encrypt
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public abstract class AbstractCipher {

  37.     protected java.security.Key key;
  38.     protected IvParameterSpec ivParameterSpec;
  39.     protected java.security.cert.Certificate certificate;
  40.     private int mode;
  41.        
  42.     protected AbstractCipher(int mode) {
  43.         this.mode = mode;
  44.     }
  45.    
  46.     protected AbstractCipher(int mode, java.security.Key key) {
  47.         this.mode = mode;
  48.         this.key = key;
  49.     }
  50.     protected AbstractCipher(int mode, java.security.Key key, IvParameterSpec ivParameterSpec) {
  51.         this.mode = mode;
  52.         this.key = key;
  53.         this.ivParameterSpec = ivParameterSpec;
  54.     }
  55.     protected AbstractCipher(int mode, java.security.Key key, byte[] ivParameterSpec) {
  56.         this.mode = mode;
  57.         this.key = key;
  58.         if(ivParameterSpec!=null && ivParameterSpec.length>0) {
  59.             this.ivParameterSpec = new IvParameterSpec(ivParameterSpec);
  60.         }
  61.     }
  62.    
  63.     protected AbstractCipher(int mode, KeyStore keystore, String alias, String passwordPrivateKey) throws UtilsException{
  64.         this.mode = mode;
  65.         this.key = keystore.getPrivateKey(alias, passwordPrivateKey);
  66.     }
  67.    
  68.     protected AbstractCipher(int mode, byte[] secretKey, String algorithm) {
  69.         this.mode = mode;
  70.         this.key = new SecretKeySpec(secretKey, algorithm); // cifratura simmetrica
  71.     }
  72.     protected AbstractCipher(int mode, byte[] secretKey, String algorithm, IvParameterSpec ivParameterSpec) {
  73.         this.mode = mode;
  74.         this.key = new SecretKeySpec(secretKey, algorithm); // cifratura simmetrica
  75.         this.ivParameterSpec = ivParameterSpec;
  76.     }
  77.     protected AbstractCipher(int mode, byte[] secretKey, String algorithm, byte[] ivParameterSpec) {
  78.         this.mode = mode;
  79.         this.key = new SecretKeySpec(secretKey, algorithm); // cifratura simmetrica
  80.         if(ivParameterSpec!=null && ivParameterSpec.length>0) {
  81.             this.ivParameterSpec = new IvParameterSpec(ivParameterSpec);
  82.         }
  83.     }
  84.    
  85.     protected AbstractCipher(int mode, java.security.cert.Certificate certificate) {
  86.         this.mode = mode;
  87.         this.certificate = certificate;
  88.     }
  89.     protected AbstractCipher(int mode, KeyStore keystore, String alias) throws UtilsException{
  90.         this.mode = mode;
  91.         this.certificate = keystore.getCertificate(alias);
  92.     }
  93.     protected AbstractCipher(int mode, KeyStore keystore) throws UtilsException{
  94.         this.mode = mode;
  95.         this.certificate = keystore.getCertificate();
  96.     }
  97.    
  98.    
  99.     protected byte[] process(String data, String charsetName, String algorithm) throws UtilsException{
  100.         try{
  101.             return this.process(data.getBytes(charsetName), algorithm);
  102.         }catch(Exception e){
  103.             throw new UtilsException(e.getMessage(),e);
  104.         }
  105.     }
  106.    
  107.     protected byte[] process(byte[] data, String algorithm) throws UtilsException{
  108.         try{
  109.             javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(algorithm);
  110.             if(this.key!=null){
  111.                 if(this.ivParameterSpec!=null) {
  112.                     cipher.init(this.mode, this.key, this.ivParameterSpec);
  113.                 }
  114.                 else {
  115.                     cipher.init(this.mode, this.key);
  116.                 }
  117.             }
  118.             else{
  119.                 cipher.init(this.mode, this.certificate);
  120.             }
  121.             return cipher.doFinal(data);
  122.         }catch(Exception e){
  123.             throw new UtilsException(e.getMessage(),e);
  124.         }
  125.     }

  126.     public void initIV(String algorithm) throws UtilsException{
  127.         try{
  128.             javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(algorithm);
  129.             // Genera un vettore di inizializzazione (IV) casuale
  130.             byte[] ivBytes = new byte[cipher.getBlockSize()];
  131.             SecureRandom random = RandomUtilities.getSecureRandom();
  132.             random.nextBytes(ivBytes);
  133.             this.ivParameterSpec = new IvParameterSpec(ivBytes);
  134.         }catch(Exception e){
  135.             throw new UtilsException(e.getMessage(),e);
  136.         }
  137.     }
  138.    
  139.     public byte[] getIV() {
  140.         return this.ivParameterSpec.getIV();
  141.     }
  142.    
  143.     public byte[] getIVBase64() {
  144.         return Base64Utilities.encode(getIV());
  145.     }
  146.     public String getIVBase64AsString() {
  147.         return Base64Utilities.encodeAsString(getIV());
  148.     }
  149.    
  150.     public char[] getIVHexBinary() throws UtilsException{
  151.         return HexBinaryUtilities.encode(getIV());
  152.     }  
  153.     public String getIVHexBinaryAsString() throws UtilsException{
  154.         return HexBinaryUtilities.encodeAsString(getIV());
  155.     }

  156. }