|
@@ -15,6 +15,9 @@ import net.sf.jsqlparser.expression.LongValue;
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.SecretKey;
|
|
|
+import javax.crypto.SecretKeyFactory;
|
|
|
+import javax.crypto.spec.DESKeySpec;
|
|
|
+import javax.crypto.spec.DESedeKeySpec;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
import java.awt.image.BufferedImage;
|
|
@@ -531,16 +534,19 @@ public class ToolsUtils {
|
|
|
return new String(decryptedByteArray, StandardCharsets.UTF_8);
|
|
|
}
|
|
|
|
|
|
- public static String encryptDesNew(String source, String pass) throws Exception {
|
|
|
+ /*public static String encryptDesNew(String source, String pass) throws Exception {
|
|
|
byte[] rgbKey = pass.getBytes("UTF-8");
|
|
|
byte[] rgbIV = pass.getBytes("UTF-8");
|
|
|
- Cipher cipher = Cipher.getInstance("DES");
|
|
|
- SecretKeySpec keySpec = new SecretKeySpec(rgbKey, "DES");
|
|
|
- IvParameterSpec ivSpec = new IvParameterSpec(rgbIV); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
|
|
|
- byte[] inputByteArray = source.getBytes("UTF-8");
|
|
|
- byte[] encryptedByteArray = cipher.doFinal(inputByteArray);
|
|
|
- return Base64.getEncoder().encodeToString(encryptedByteArray);
|
|
|
- }
|
|
|
+ DESedeKeySpec desKeySpec = new DESedeKeySpec(rgbKey);
|
|
|
+ SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
|
+ SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
|
|
|
+ Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
|
|
|
+ byte[] inputByteArray = source.getBytes("UTF-8");
|
|
|
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(rgbIV);
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
|
|
|
+ byte[] encrypted = cipher.doFinal(inputByteArray);
|
|
|
+ return Base64.getEncoder().encodeToString(encrypted);}
|
|
|
+ }*/
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -548,16 +554,64 @@ public class ToolsUtils {
|
|
|
@param source 源字符串
|
|
|
@param pass 密钥,长度必须8位
|
|
|
@return 解密后的字符串 */
|
|
|
- public static String decryptDesNew(String source, String pass) throws Exception {
|
|
|
+ /*public static String decryptDesNew(String source, String pass) throws Exception {
|
|
|
byte[] rgbKey = pass.getBytes("UTF-8");
|
|
|
byte[] rgbIV = pass.getBytes("UTF-8");
|
|
|
- Cipher cipher = Cipher.getInstance("DES");
|
|
|
- SecretKeySpec keySpec = new SecretKeySpec(rgbKey, "DES");
|
|
|
- IvParameterSpec ivSpec = new IvParameterSpec(rgbIV);
|
|
|
- cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
|
|
|
+ DESedeKeySpec desKeySpec = new DESedeKeySpec(rgbKey);
|
|
|
+ SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
|
+ SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
|
|
|
+ Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding")) {
|
|
|
+ byte[] inputByteArray = Base64.getDecoder().decode(source);
|
|
|
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(rgbIV);
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
|
|
|
+ byte[] decrypted = cipher.doFinal(inputByteArray);
|
|
|
+ return new String(decrypted, "UTF-8");
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+
|
|
|
+
|
|
|
+ public static String encryptDesNew(String source, String pass) throws Exception {
|
|
|
+ byte[] rgbKey = pass.getBytes(StandardCharsets.UTF_8);
|
|
|
+ byte[] rgbIV = pass.getBytes(StandardCharsets.UTF_8);
|
|
|
+ DESKeySpec desKeySpec = new DESKeySpec(rgbKey);
|
|
|
+ SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
|
+ SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
|
|
|
+ Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
|
|
+ byte[] inputByteArray = source.getBytes(StandardCharsets.UTF_8);
|
|
|
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(rgbIV);
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
|
|
|
+ byte[] encryptedByteArray = cipher.doFinal(inputByteArray);
|
|
|
+ return Base64.getEncoder().encodeToString(encryptedByteArray);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String decryptDesNew(String source, String pass) throws Exception {
|
|
|
+ byte[] rgbKey = pass.getBytes(StandardCharsets.UTF_8);
|
|
|
+ byte[] rgbIV = pass.getBytes(StandardCharsets.UTF_8);
|
|
|
+ DESKeySpec desKeySpec = new DESKeySpec(rgbKey);
|
|
|
+ SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
|
+ SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
|
|
|
+ Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
|
|
byte[] inputByteArray = Base64.getDecoder().decode(source);
|
|
|
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(rgbIV);
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
|
|
|
byte[] decryptedByteArray = cipher.doFinal(inputByteArray);
|
|
|
- return new String(decryptedByteArray, "UTF-8");
|
|
|
+ return new String(decryptedByteArray, StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String base64Encode(byte[] bytes) throws IOException {
|
|
|
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
+ Base64.getEncoder().wrap(output).write(bytes);
|
|
|
+ return output.toString(StandardCharsets.UTF_8.name());
|
|
|
+ }
|
|
|
+
|
|
|
+ /*private static byte[] base64Decode(String str) throws IOException {
|
|
|
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
+ Base64.getDecoder().wrap(output).write(str.getBytes(StandardCharsets.UTF_8));
|
|
|
+ return output.toByteArray();
|
|
|
+ }*/
|
|
|
+ private static byte[] base64Decode(String str) throws IOException {
|
|
|
+ byte[] decodedString = Base64.getDecoder().decode(new String(str).getBytes("UTF-8"));
|
|
|
+ return decodedString;
|
|
|
}
|
|
|
|
|
|
}
|