¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package org.springblade.core.tool.utils; |
| | | |
| | | import org.springframework.lang.Nullable; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.SecretKeyFactory; |
| | | import javax.crypto.spec.DESKeySpec; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * DESå è§£å¯å¤çå·¥å
· |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class DesUtil { |
| | | /** |
| | | * æ°åç¾åï¼å¯é¥ç®æ³ |
| | | */ |
| | | public static final String DES_ALGORITHM = "DES"; |
| | | |
| | | /** |
| | | * çæ des å¯é¥ |
| | | * |
| | | * @return å¯é¥ |
| | | */ |
| | | public static String genDesKey() { |
| | | return StringUtil.random(16); |
| | | } |
| | | |
| | | /** |
| | | * DESå å¯ |
| | | * |
| | | * @param data byte array |
| | | * @param password å¯é¥ |
| | | * @return des hex |
| | | */ |
| | | public static String encryptToHex(byte[] data, String password) { |
| | | return HexUtil.encodeToString(encrypt(data, password)); |
| | | } |
| | | |
| | | /** |
| | | * DESå å¯ |
| | | * |
| | | * @param data å符串å
容 |
| | | * @param password å¯é¥ |
| | | * @return des hex |
| | | */ |
| | | @Nullable |
| | | public static String encryptToHex(@Nullable String data, String password) { |
| | | if (StringUtil.isBlank(data)) { |
| | | return null; |
| | | } |
| | | byte[] dataBytes = data.getBytes(Charsets.UTF_8); |
| | | return encryptToHex(dataBytes, password); |
| | | } |
| | | |
| | | /** |
| | | * DESè§£å¯ |
| | | * |
| | | * @param data å符串å
容 |
| | | * @param password å¯é¥ |
| | | * @return des context |
| | | */ |
| | | @Nullable |
| | | public static String decryptFormHex(@Nullable String data, String password) { |
| | | if (StringUtil.isBlank(data)) { |
| | | return null; |
| | | } |
| | | byte[] hexBytes = HexUtil.decode(data); |
| | | return new String(decrypt(hexBytes, password), Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * DESå å¯ |
| | | * |
| | | * @param data byte array |
| | | * @param password å¯é¥ |
| | | * @return des hex |
| | | */ |
| | | public static String encryptToBase64(byte[] data, String password) { |
| | | return Base64Util.encodeToString(encrypt(data, password)); |
| | | } |
| | | |
| | | /** |
| | | * DESå å¯ |
| | | * |
| | | * @param data å符串å
容 |
| | | * @param password å¯é¥ |
| | | * @return des hex |
| | | */ |
| | | @Nullable |
| | | public static String encryptToBase64(@Nullable String data, String password) { |
| | | if (StringUtil.isBlank(data)) { |
| | | return null; |
| | | } |
| | | byte[] dataBytes = data.getBytes(Charsets.UTF_8); |
| | | return encryptToBase64(dataBytes, password); |
| | | } |
| | | |
| | | /** |
| | | * DESè§£å¯ |
| | | * |
| | | * @param data å符串å
容 |
| | | * @param password å¯é¥ |
| | | * @return des context |
| | | */ |
| | | public static byte[] decryptFormBase64(byte[] data, String password) { |
| | | byte[] dataBytes = Base64Util.decode(data); |
| | | return decrypt(dataBytes, password); |
| | | } |
| | | |
| | | /** |
| | | * DESè§£å¯ |
| | | * |
| | | * @param data å符串å
容 |
| | | * @param password å¯é¥ |
| | | * @return des context |
| | | */ |
| | | @Nullable |
| | | public static String decryptFormBase64(@Nullable String data, String password) { |
| | | if (StringUtil.isBlank(data)) { |
| | | return null; |
| | | } |
| | | byte[] dataBytes = Base64Util.decodeFromString(data); |
| | | return new String(decrypt(dataBytes, password), Charsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * DESå å¯ |
| | | * |
| | | * @param data å
容 |
| | | * @param desKey å¯é¥ |
| | | * @return byte array |
| | | */ |
| | | public static byte[] encrypt(byte[] data, byte[] desKey) { |
| | | return des(data, desKey, Cipher.ENCRYPT_MODE); |
| | | } |
| | | |
| | | /** |
| | | * DESå å¯ |
| | | * |
| | | * @param data å
容 |
| | | * @param desKey å¯é¥ |
| | | * @return byte array |
| | | */ |
| | | public static byte[] encrypt(byte[] data, String desKey) { |
| | | return encrypt(data, Objects.requireNonNull(desKey).getBytes(Charsets.UTF_8)); |
| | | } |
| | | |
| | | /** |
| | | * DESè§£å¯ |
| | | * |
| | | * @param data å
容 |
| | | * @param desKey å¯é¥ |
| | | * @return byte array |
| | | */ |
| | | public static byte[] decrypt(byte[] data, byte[] desKey) { |
| | | return des(data, desKey, Cipher.DECRYPT_MODE); |
| | | } |
| | | |
| | | /** |
| | | * DESè§£å¯ |
| | | * |
| | | * @param data å
容 |
| | | * @param desKey å¯é¥ |
| | | * @return byte array |
| | | */ |
| | | public static byte[] decrypt(byte[] data, String desKey) { |
| | | return decrypt(data, Objects.requireNonNull(desKey).getBytes(Charsets.UTF_8)); |
| | | } |
| | | |
| | | /** |
| | | * DESå å¯/è§£å¯å
Œ
±æ¹æ³ |
| | | * |
| | | * @param data byteæ°ç» |
| | | * @param desKey å¯é¥ |
| | | * @param mode å å¯ï¼{@link Cipher#ENCRYPT_MODE}ï¼è§£å¯ï¼{@link Cipher#DECRYPT_MODE} |
| | | * @return des |
| | | */ |
| | | private static byte[] des(byte[] data, byte[] desKey, int mode) { |
| | | try { |
| | | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM); |
| | | Cipher cipher = Cipher.getInstance(DES_ALGORITHM); |
| | | DESKeySpec desKeySpec = new DESKeySpec(desKey); |
| | | cipher.init(mode, keyFactory.generateSecret(desKeySpec), Holder.SECURE_RANDOM); |
| | | return cipher.doFinal(data); |
| | | } catch (Exception e) { |
| | | throw Exceptions.unchecked(e); |
| | | } |
| | | } |
| | | |
| | | } |