ReciprocalUtils.java 9.6 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
 * 
 * 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.
 */
 

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 * 
 */
package org.maxkey.crypto;

import java.io.UnsupportedEncodingException;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.logging.LogFactory;
import org.maxkey.util.StringGenerator;

/**
 * Reciprocal cipher or Symmetric-key algorithm
 * 
 * algorithm Support DES,DESede,Blowfish and AES
 * 
 * default key value use ReciprocalUtils.defaultKey
 * 
 * generateKey is generate random key for algorithm
 * 
 * @author Crystal.Sea
 * 
 */
public final class ReciprocalUtils {

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
47
    private static final String defaultKey = "l0JqT7NvIzP9oRaG4kFc1QmD_bWu3x8E5yS2h6"; //
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
48

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
49 50 51 52 53 54
    public final class Algorithm {
        public static final String DES = "DES";
        public static final String DESede = "DESede";
        public static final String Blowfish = "Blowfish";
        public static final String AES = "AES";
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
55

56 57 58 59 60 61 62 63 64 65
    static {
        if(System.getProperty("java.version").startsWith("1.8")) {
            try {
                Security.addProvider(new com.sun.crypto.provider.SunJCE());
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    public static byte[] encode(byte[] simpleBytes, SecretKey secretKey, String algorithm) {
        // Create the ciphers
        Cipher ecipher;
        byte[] byteFinal = null;
        try {
            ecipher = Cipher.getInstance(secretKey.getAlgorithm());
            // Encode the string into bytes using utf-8
            ecipher.init(Cipher.ENCRYPT_MODE, secretKey);
            // Encrypt
            byteFinal = ecipher.doFinal(simpleBytes);
            return byteFinal;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
82

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    /**
     * @param simple
     * @param secretKey must length
     * @return
     * @throws Exception
     */
    public static byte[] encode(String simple, String secretKey, String algorithm) {
        if (keyLengthCheck(secretKey, algorithm)) {
            SecretKey key = generatorKey(secretKey, algorithm);
            try {
                return encode(simple.getBytes("UTF-8"), key, algorithm);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
100

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    public static byte[] decoder(byte[] ciphersBytes, SecretKey secretKey, String algorithm) {
        Cipher cipher;
        byte[] byteFinal = null;
        try {
            cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byteFinal = cipher.doFinal(ciphersBytes);
            // String simple=new String(byteFinal, "UTF8" );
            // return simple;
            return byteFinal;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return null;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
118

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
119 120 121 122 123 124 125 126 127 128 129
    public static String decoder(byte[] ciphersBytes, String secretKey, String algorithm) {
        if (keyLengthCheck(secretKey, algorithm)) {
            SecretKey key = generatorKey(secretKey, algorithm);
            try {
                return new String(decoder(ciphersBytes, key, algorithm), "UTF8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
130

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
131 132 133
    public static byte[] encodeByDefaultKey(String simple, String algorithm) {
        SecretKey key = generatorDefaultKey(algorithm);
        return encode(simple.getBytes(), key, algorithm);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
134

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
135
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
136

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
137 138
    public static String encode2HexByDefaultKey(String simple, String algorithm) {
        byte[] byteFinal = encodeByDefaultKey(simple, algorithm);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
139

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
140 141 142
        String cipherHex = HexUtils.bytes2HexString(byteFinal);
        return cipherHex;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
143

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
144 145 146
    public static byte[] decoderByDefaultKey(byte[] byteCiphers, String algorithm) {
        SecretKey key = generatorDefaultKey(algorithm);
        return decoder(byteCiphers, key, algorithm);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
147

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
148
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
149

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
150 151
    public static String decoderHexByDefaultKey(String ciphers, String algorithm) {
        byte[] byteSimple = HexUtils.hex2Bytes(ciphers);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
152

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
153
        byte[] byteFinal = decoderByDefaultKey(byteSimple, algorithm);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
154

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
155 156 157 158 159 160 161 162
        String simple = null;
        try {
            simple = new String(byteFinal, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return simple;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
163

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
164
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
165

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    public static SecretKey generatorDefaultKey(String algorithm) {
        try {
            String secretKey = defaultKey;
            if (algorithm.equals(Algorithm.DES)) {
                secretKey = defaultKey.substring(0, 8);
            } else if (algorithm.equals(Algorithm.AES) || algorithm.equals(Algorithm.Blowfish)) {
                secretKey = defaultKey.substring(0, 16);
            } else if (algorithm.equals(Algorithm.DESede)) {
                secretKey = defaultKey.substring(0, 24);
            }
            // System.out.println("defaultKey : "+secretKey);
            SecretKey key = new SecretKeySpec(secretKey.getBytes(), algorithm);
            return key;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
184

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
185 186 187 188 189 190 191 192 193
    private static SecretKey generatorKey(String secretKey, String algorithm) {
        try {
            SecretKey key = new SecretKeySpec(secretKey.getBytes(), algorithm);
            return key;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
194

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
195 196 197 198 199 200 201 202
    public static String encode2Hex(String simple, String secretKey, String algorithm) {
        if (keyLengthCheck(secretKey, algorithm)) {
            byte[] cipher = encode(simple, secretKey, algorithm);
            // Encode bytes to HEX to get a string
            return HexUtils.bytes2HexString(cipher);
        }
        return null;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
203

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
204 205 206
    public static String decoderHex(String ciphers, String secretKey, String algorithm) {
        if (keyLengthCheck(secretKey, algorithm)) {
            byte[] byteSimple = HexUtils.hex2Bytes(ciphers);
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
207

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
208 209 210 211
            return decoder(byteSimple, secretKey, algorithm);
        }
        return null;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
212

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    private static boolean keyLengthCheck(String secretKey, String algorithm) {
        boolean lengthCheck = false;
        if (algorithm.equals(Algorithm.DES)) {
            if (secretKey.length() == 8) {
                lengthCheck = true;
            } else {
                LogFactory.getLog(ReciprocalUtils.class)
                        .debug("key length is " + secretKey.getBytes().length + " ,must lequal 8");
            }
        } else if (algorithm.equals(Algorithm.DESede)) {
            if (secretKey.length() == 24) {
                lengthCheck = true;
            } else {
                LogFactory.getLog(ReciprocalUtils.class)
                        .debug("key length is " + secretKey.getBytes().length + " ,must equal 24");
            }
        } else if (algorithm.equals(Algorithm.AES)) {
            if (secretKey.length() == 16) {
                lengthCheck = true;
            } else {
                LogFactory.getLog(ReciprocalUtils.class)
                        .debug("key length is " + secretKey.getBytes().length + " ,must equal 16");
            }
        } else if (algorithm.equals(Algorithm.Blowfish)) {
            if (secretKey.length() <= 16) {
                lengthCheck = true;
            } else {
                LogFactory.getLog(ReciprocalUtils.class)
                        .debug("key length is " + secretKey.getBytes().length + " ,must be less then 16");
            }
        }
        return lengthCheck;
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
246

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
247 248 249 250 251 252 253 254
    /**
     * @param simple
     * @param secretKey must length is 16
     * @return
     */
    public static String aesEncode(String simple, String secretKey) {
        return encode2Hex(simple, secretKey, Algorithm.AES);
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
255

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
256 257 258
    public static String aesDecoder(String ciphers, String secretKey) {
        return decoderHex(ciphers, secretKey, Algorithm.AES);
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
259

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
260 261 262 263 264 265 266 267 268
    /**
     * encode by defaultKey with Algorithm.AES
     * 
     * @param simple
     * @return Hex
     */
    public static String encode(String simple) {
        return encode2HexByDefaultKey(simple, Algorithm.AES);
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
269

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    /**
     * decoder by defaultKey with Algorithm.AES
     * 
     * @param ciphers is HEX
     * 
     * @return
     */
    public static String decoder(String ciphers) {
        return decoderHexByDefaultKey(ciphers, Algorithm.AES);
    }

    public static String generateKey(String algorithm) {
        if (algorithm.equals(Algorithm.DES)) {
            return (new StringGenerator(8)).randomGenerate();
        } else if (algorithm.equals(Algorithm.AES)) {
            return (new StringGenerator(16)).randomGenerate();
        } else if (algorithm.equals(Algorithm.Blowfish)) {
            return (new StringGenerator(16)).randomGenerate();
        } else if (algorithm.equals(Algorithm.DESede)) {
            return (new StringGenerator(24)).randomGenerate();
        } else {
            return (new StringGenerator()).uniqueGenerate();
        }
    }
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
294
}