001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.utils; 017 018import javax.crypto.Cipher; 019import javax.crypto.SecretKey; 020import javax.crypto.SecretKeyFactory; 021import javax.crypto.spec.DESKeySpec; 022import java.security.SecureRandom; 023 024 025/** 026 * DESUtil 对称加密工具类,非对称加密请参考 RSAUtil 027 */ 028public class DESUtil { 029 030 031 /** 032 * 加密 033 * 034 * @param data 035 * @param key 036 * @return 037 * @throws Exception 038 */ 039 public static byte[] encrypt(byte[] data, byte[] key) throws Exception { 040 Cipher cipher = Cipher.getInstance("DES"); 041 DESKeySpec ds = new DESKeySpec(key); 042 SecureRandom sr = new SecureRandom(); 043 SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); 044 SecretKey skey = secretKeyFactory.generateSecret(ds); 045 cipher.init(Cipher.ENCRYPT_MODE, skey, sr); 046 return cipher.doFinal(data); 047 } 048 049 050 /** 051 * 加密 052 * 053 * @param data 054 * @param key 055 * @return 056 * @throws Exception 057 */ 058 public static String encrypt(String data, String key) throws Exception { 059 return byte2hex(encrypt(data.getBytes(), key.getBytes())); 060 } 061 062 063 /** 064 * 解密 065 * 066 * @param data 067 * @param key 068 * @return 069 * @throws Exception 070 */ 071 public static byte[] decrypt(byte[] data, byte[] key) throws Exception { 072 Cipher cipher = Cipher.getInstance("DES"); 073 DESKeySpec ds = new DESKeySpec(key); 074 SecureRandom sr = new SecureRandom(); 075 SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); 076 SecretKey skey = secretKeyFactory.generateSecret(ds); 077 cipher.init(Cipher.DECRYPT_MODE, skey, sr); 078 return cipher.doFinal(data); 079 } 080 081 082 /** 083 * 解密 084 * 085 * @param data 086 * @param key 087 * @return 088 * @throws Exception 089 */ 090 public static String decrypt(String data, String key) throws Exception { 091 return new String(decrypt(hex2byte(data.getBytes()), key.getBytes())); 092 } 093 094 095 private static String byte2hex(byte[] b) { 096 StringBuilder hs = new StringBuilder(); 097 String stmp; 098 for (int n = 0; b != null && n < b.length; n++) { 099 stmp = Integer.toHexString(b[n] & 0XFF); 100 if (stmp.length() == 1) { 101 hs.append('0'); 102 } 103 hs.append(stmp); 104 } 105 return hs.toString().toUpperCase(); 106 } 107 108 109 private static byte[] hex2byte(byte[] b) { 110 if ((b.length % 2) != 0) { 111 throw new IllegalArgumentException(); 112 } 113 byte[] b2 = new byte[b.length / 2]; 114 for (int n = 0; n < b.length; n += 2) { 115 String item = new String(b, n, 2); 116 b2[n / 2] = (byte) Integer.parseInt(item, 16); 117 } 118 return b2; 119 } 120}