1 /* 2 * This file is part of Waarp Project (named also Waarp or GG). 3 * 4 * Copyright (c) 2019, Waarp SAS, and individual contributors by the @author 5 * tags. See the COPYRIGHT.txt in the distribution for a full listing of 6 * individual contributors. 7 * 8 * All Waarp Project is free software: you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or (at your 11 * option) any later version. 12 * 13 * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY 14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 15 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along with 18 * Waarp . If not, see <http://www.gnu.org/licenses/>. 19 */ 20 package org.waarp.common.crypto; 21 22 /** 23 * This class handles methods to encrypt and unencrypt messages with any 24 * available algorithms in the JVM.<br> 25 * <b>AES is the best compromise in term of security and efficiency.</b> <br> 26 * Usage:<br> 27 * <ul> 28 * <li>Create a Key object: DynamicKey key = new DynamicKey(size,algo,instance,extension);<br> 29 * As DynamicKey(56, "DES", "DES/ECB/PKCS5Padding", "des") for DES<br> 30 * or through Enum class INSTANCES (minimal supported size) or INSTANCESMAX 31 * (maximal supported size) as 32 * DynamicKey(INSTANCESMAX.AES,"aes")</li> 33 * <li>Create a key: 34 * <ul> 35 * <li>Generate: key.generateKey();<br> 36 * The method key.getSecretKeyInBytes() allow getting the key in Bytes.</li> 37 * <li>From an external source: key.setSecretKey(arrayOfBytes);</li> 38 * </ul> 39 * </li> 40 * <li>To encrypt a String in a Base64 format: String myStringCrypt = 41 * key.cryptToString(myString);</li> 42 * <li>To unencrypt one string from Base64 format to the original String: 43 * String 44 * myStringDecrypt = 45 * key.decryptStringInString(myStringCrypte);</li> 46 * </ul> 47 */ 48 public class DynamicKeyObject extends KeyObject { 49 50 /** 51 * Minimal key size 52 */ 53 public enum INSTANCES { 54 AES(128), // Min 128 55 ARCFOUR(56), // No Max 56 Blowfish(56), // No Max 57 DES(56), // Must be 56 except if Strong Policy is used 58 DESede(112), // 112 or 168 triple DES 59 RC2(56), RC4(56); 60 61 final int size; 62 63 INSTANCES(final int size) { 64 this.size = size; 65 } 66 } 67 68 /** 69 * Recommended key size when normal JVM installed (no extension on encrypt 70 * support) 71 */ 72 public enum INSTANCESMAX { 73 AES(128), // Min 128 74 ARCFOUR(128), // No Max 75 Blowfish(128), // No Max 76 DES(56), // Must be 56 except if Strong Policy is used 77 DESede(168), // 112 or 168 triple DES 78 RC2(128), RC4(128); 79 80 final int size; 81 82 INSTANCESMAX(final int size) { 83 this.size = size; 84 } 85 } 86 87 /** 88 * This value could be between 32 and 128 due to license limitation. 89 */ 90 private final int keySize; 91 /** 92 * Short name for the algorithm 93 */ 94 private final String algo; 95 /** 96 * Could be the shortname again (default implementation in JVM) or the full 97 * name as DES/ECB/PKCS5Padding 98 */ 99 private final String instance; 100 /** 101 * The extension for the file to use when saving the key (note that an extra 102 * file as extension.inf will be 103 * also saved for the extra information) 104 */ 105 private final String extension; 106 107 /** 108 * @param kEYSIZE example DES: 56 109 * @param aLGO example DES: DES 110 * @param iNSTANCE example DES: DES/ECB/PKCS5Padding 111 * @param eXTENSION example DES: des 112 */ 113 public DynamicKeyObject(final int kEYSIZE, final String aLGO, 114 final String iNSTANCE, final String eXTENSION) { 115 keySize = kEYSIZE; 116 algo = aLGO; 117 instance = iNSTANCE; 118 extension = eXTENSION; 119 } 120 121 /** 122 * @param instance the minimal default instance 123 * @param eXTENSION to use for files 124 */ 125 public DynamicKeyObject(final INSTANCES instance, final String eXTENSION) { 126 keySize = instance.size; 127 algo = instance.name(); 128 this.instance = instance.name(); 129 extension = eXTENSION; 130 } 131 132 /** 133 * @param instance the maximal default instance 134 * @param eXTENSION to use for files 135 */ 136 public DynamicKeyObject(final INSTANCESMAX instance, final String eXTENSION) { 137 keySize = instance.size; 138 algo = instance.name(); 139 this.instance = instance.name(); 140 extension = eXTENSION; 141 } 142 143 @Override 144 public final String getAlgorithm() { 145 return algo; 146 } 147 148 @Override 149 public final String getInstance() { 150 return instance; 151 } 152 153 @Override 154 public final int getKeySize() { 155 return keySize; 156 } 157 158 @Override 159 public final String getFileExtension() { 160 return extension; 161 } 162 163 }