├── src ├── config.properties ├── META-INF │ └── MANIFEST.MF └── com │ └── justin │ ├── constants │ └── Global.java │ ├── service │ └── pass │ │ ├── IEncrypt.java │ │ ├── IDecrypt.java │ │ └── impl │ │ ├── Encrypt.java │ │ └── Decrypt.java │ ├── utils │ ├── PwdUtil.java │ └── FileUtil.java │ └── Main.java ├── pwd.jar ├── MANIFEST.MF ├── .gitignore ├── test └── com │ └── justin │ ├── DecryptTest.java │ └── MainTest.java └── README.md /src/config.properties: -------------------------------------------------------------------------------- 1 | filepath=./encrypt.pass -------------------------------------------------------------------------------- /pwd.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JustinJava/pwd-encrypt-decrypt/HEAD/pwd.jar -------------------------------------------------------------------------------- /src/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Main-Class: com.justin.Main 3 | 4 | -------------------------------------------------------------------------------- /MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: . 3 | Main-Class: com.justin.Main 4 | 5 | -------------------------------------------------------------------------------- /src/com/justin/constants/Global.java: -------------------------------------------------------------------------------- 1 | package com.justin.constants; 2 | 3 | public class Global{ 4 | 5 | /** DES MD5 initial key **/ 6 | public final static String DES_KEY = "F1D3EBA1384E84A1510923606DBAE2F5"; 7 | } 8 | -------------------------------------------------------------------------------- /src/com/justin/service/pass/IEncrypt.java: -------------------------------------------------------------------------------- 1 | package com.justin.service.pass; 2 | 3 | import java.io.IOException; 4 | 5 | public interface IEncrypt { 6 | 7 | /** 8 | * 9 | * @param host 10 | * @param user 11 | * @param password 12 | * @throws IOException 13 | */ 14 | public abstract void createPassfile(String host,String user,String password) throws IOException; 15 | 16 | /** 17 | * 18 | * @param password 19 | * @return 20 | */ 21 | public abstract String encryptStr(String password); 22 | } 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gitignore 2 | #README.md 3 | 4 | ### app ### 5 | build/ 6 | target/ 7 | bin/ 8 | out/ 9 | !**/src/main/**/out/ 10 | !**/src/test/**/out/ 11 | !**/src/main/**/build/ 12 | !**/src/test/**/build/ 13 | 14 | ### Gradle ### 15 | .gradle/ 16 | !gradle/wrapper/gradle-wrapper.jar 17 | 18 | ### IntelliJ IDEA ### 19 | .idea 20 | *.iws 21 | *.iml 22 | *.ipr 23 | 24 | ### Eclipse ### 25 | .classpath 26 | .project 27 | .settings/ 28 | 29 | # Operating System Files 30 | *.DS_Store 31 | Thumbs.db 32 | *.sw? 33 | .#* 34 | *# 35 | *~ 36 | *.sublime-* 37 | 38 | ### NetBeans ### 39 | /nbproject/private/ 40 | /nbbuild/ 41 | /dist/ 42 | /nbdist/ 43 | /.nb-gradle/ 44 | 45 | ### VS Code ### 46 | .vscode/ 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/com/justin/service/pass/IDecrypt.java: -------------------------------------------------------------------------------- 1 | package com.justin.service.pass; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.IOException; 5 | 6 | public interface IDecrypt { 7 | 8 | /** 9 | * 10 | * @param host 11 | * @param user 12 | * @return 13 | * @throws FileNotFoundException 14 | * @throws IOException 15 | * @throws Exception 16 | */ 17 | public abstract String getDepass(String host,String user) throws FileNotFoundException, IOException, Exception; 18 | 19 | /** 20 | * 21 | * @param enpassfileContent 22 | * @return 23 | * @throws Exception 24 | */ 25 | public abstract String getDepass(String enpassfileContent) throws Exception; 26 | 27 | /** 28 | * 29 | * @param paramString 30 | * @return 31 | */ 32 | public abstract String decryptStr(String paramString); 33 | } 34 | -------------------------------------------------------------------------------- /test/com/justin/DecryptTest.java: -------------------------------------------------------------------------------- 1 | package com.justin; 2 | 3 | import com.justin.service.pass.IDecrypt; 4 | import com.justin.service.pass.impl.Decrypt; 5 | 6 | /** 7 | * @program: DataStructures 8 | * @description: 数据库密文解密测试类 9 | * @author: JustinQin 10 | * @create: 2021/7/18 20:07 11 | * @version: v1.0.0 12 | **/ 13 | public class DecryptTest { 14 | public static void main(String[] args) { 15 | String dePass = "127.0.0.1:root:C33D583B7575AF82FFDCE895C9F5E8FA:E5DBAC8F8FFF3EA5DC670221DAF820B3:DC9AB9C2382A04F2A479891CF9B411C9"; 16 | IDecrypt decrypt = new Decrypt(); 17 | String password = null; 18 | try { 19 | password = decrypt.getDepass(dePass); 20 | } catch (Exception e) { 21 | throw new RuntimeException("数据库密文解密失败:" + e.getMessage()); 22 | } 23 | System.out.print(password); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/com/justin/utils/PwdUtil.java: -------------------------------------------------------------------------------- 1 | package com.justin.utils; 2 | 3 | import java.io.File; 4 | import java.util.ResourceBundle; 5 | 6 | public class PwdUtil { 7 | 8 | public final static String FILE = "config"; 9 | public final static String FILE_PATH = "filepath"; 10 | public final static String FILE_NAME = "encrypt.pass"; 11 | 12 | /** 13 | * Get encrypt.pass absolute path by config.properties 14 | * @return 15 | */ 16 | public static String getPassFile(){ 17 | ResourceBundle bundle = ResourceBundle.getBundle(FILE); 18 | String passFile = bundle.getString(FILE_PATH); 19 | if (passFile.startsWith(".")) { 20 | passFile = passFile.replace(".", FileUtil.getJarDir()); 21 | } 22 | 23 | File pass = new File(passFile); 24 | if (!pass.getParentFile().exists()) { 25 | passFile = FileUtil.getJarDir() + File.separator + FILE_NAME; 26 | pass= new File(passFile); 27 | } 28 | 29 | return pass.getAbsolutePath(); 30 | } 31 | 32 | public static void main(String[] args) { 33 | System.out.println(getPassFile()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/com/justin/MainTest.java: -------------------------------------------------------------------------------- 1 | package com.justin; 2 | 3 | /** 4 | * @program: DataStructures 5 | * @description: 密码加解密测试类 6 | * @author: JustinQin 7 | * @create: 2021/7/18 11:11 8 | * @version: v1.0.0 9 | **/ 10 | public class MainTest { 11 | private final static String HOST="127.0.0.1"; //数据库IP 12 | private final static String USER = "root"; //数据库用户 13 | private final static String PASSWORD = "abc@123456"; //数据库密码 14 | 15 | public static void main(String[] args) { 16 | //初始化加密需要的参数 17 | args = encryptInitParam(args); 18 | 19 | //初始化解密需要的参数 20 | //args = decryptInitParam(args); 21 | 22 | //加解密的主入口 23 | Main.main(args); 24 | } 25 | 26 | /** 27 | * 初始化加密需要的参数 28 | * @param args 29 | * @return 30 | */ 31 | private static String[] encryptInitParam(String[] args) { 32 | args = new String[3]; 33 | args[0] = HOST; 34 | args[1] = USER; 35 | args[2] = PASSWORD; 36 | return args; 37 | } 38 | 39 | /** 40 | * 初始化解密需要的参数 41 | * @param args 42 | * @return 43 | */ 44 | private static String[] decryptInitParam(String[] args) { 45 | args = new String[2]; 46 | args[0] = HOST; 47 | args[1] = USER; 48 | return args; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/com/justin/utils/FileUtil.java: -------------------------------------------------------------------------------- 1 | package com.justin.utils; 2 | 3 | import java.io.File; 4 | import java.net.URLDecoder; 5 | 6 | public class FileUtil { 7 | 8 | public static String getJarPath(){ 9 | File file = getFile(); 10 | if (file == null) return null; 11 | return file.getAbsolutePath(); 12 | } 13 | 14 | public static String getJarDir(){ 15 | File file = getFile(); 16 | if (file == null) return null; 17 | return file.getParent(); 18 | } 19 | 20 | public static String getJarName(){ 21 | File file = getFile(); 22 | if (file == null) return null; 23 | return file.getName(); 24 | } 25 | 26 | private static File getFile() { 27 | return new File(getFileName()); 28 | } 29 | 30 | private static String getFileName() { 31 | String path = FileUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile(); 32 | try { 33 | path = URLDecoder.decode(path,"UTF-8"); 34 | } catch (Exception e) { 35 | return null; 36 | } 37 | return path; 38 | } 39 | 40 | public static void main(String[] args) { 41 | System.out.println(getFileName()); 42 | System.out.println(getJarPath()); 43 | System.out.println(getJarDir()); 44 | System.out.println(getJarName()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 基于纯Java编写的密码加解密工具包 2 | == 3 | 4 | 一、项目介绍 5 | -- 6 | 7 | * 工具包基于纯Java编写,采用DES对称加密算法对信息进行加解密,为了防止信息被篡改,再通过MD5摘要算进行信息摘要。 8 | * 主要用于不同场景下对数据库密码的加解密,数据库配置以及数据库连接过程都应该采用密文方式,明文密码容易泄露,影响数据库安全。 9 | * 整个工程可以被编译打包成一个`pwd.jar`加解密工具包,将工具包导入到自己项目或者放到服务器上进行使用,也可以基于源码修改定制自己的工具包。 10 | * 工具包简单、实用、安全,可能也有一些不足或者需要完善的地方,欢迎小伙伴们提`Issues`~ 11 | * 最后,觉得有用的小伙伴记得`Star`支持一下哈 12 | 13 | 二、应用场景 14 | -- 15 | ### 1、项目 16 |   你的项目数据库连接配置,是不是还在用明文密码?如果是的话,那不妨试试使用本工具包,改成使用加密密码,只需要进行如下两个简单的配置改造。 17 | 18 | * 数据库配置 19 | * 引入pwd.jar工具包到工程中 20 | * 通过明文密码获取密文,可以在工程里写个Test测试类,通过pwd.jar包的EncryptPass类的encryptStr方法获取到密文 21 | * 改造properties或yml配置的数据库明文密码为密文密码 22 | * 连接池配置 23 | * 自定义一个自己的DataSource数据源类,如TomcatDataSource,继承原生的DataSource类,将xml配置中的数据源指向自己的TomcatDataSource。 24 | * 重写数据源类TomcatDataSource的setPassword方法 25 | * 在setPassword方法中,可以获取数据库密码密文(spring-mybatis.xml中数据源配置的password属性值从properties或yml配置中加载到密文) 26 | * 获取到密文后,在setPassword方法中使用pwd.jar包的DecryptPass类的getDepass对密文进行解密得到明文 27 | * 更新连接池的密码为得到的明文密码,不同连接池更新方式不同,一般都是通过this.poolProperties.setPassword("密文")方式更新 28 | * 完成`数据库配置`和`连接池配置`后,项目就完成了数据库密码加解密的整合~ 29 | 30 | ### 2、shell脚本 31 |   写过shell脚本的童鞋应该知道,如果shell脚本中需要跟远程数据库打交道,比如每天根据外系统下发的批量数据文件,批量的去更新或插入数据到MySQL数据表, 32 | 在操作MySQL数据库之前,需要先通过数据库用户密码建立连接,这时如果直接在脚本中使用数据库明文密码,就不够安全啦, 33 | 这时不妨试试使用本工具包,改成使用加密密码,只需要进行以下简单的两个操作即可。 34 | 35 | * 生成密文文件 36 | * 将pwd.jar加解密工具包放到服务器,如/home/tomcat/password/pwd.jar 37 | * 生成密文文件encrypt.pass,生成命令为`java -jar pwd.jar host user password |tail -n 1` 38 | * 生成的密文文件在pwd.jar当前目录下,如/home/tomcat/password/encrypt.pass 39 | * 配置sh脚本 40 | * 读取密文文件得到明文密码,读取命令为`java -jar pwd.jar host user |tail -n 1` 其中`|tail -n 1`表示取最后一行输出的明文密码 41 | * 修改sh脚本中的数据库密码为通过命令`java -jar pwd.jar host user |tail -n 1`方式获取 42 | 43 | 三、快速上手 44 | -- 45 | * 关于本加解密工具包,上面的应用场景只是大致介绍了思路和实现步骤~ 46 | * 具体如何使用和快速上手,可以参考我的这篇博客文章学习: 47 | https://blog.csdn.net/JustinQin/article/details/119132853?utm_source=app&app_version=4.12.0 48 | 49 | 50 | -------------------------------------------------------------------------------- /src/com/justin/Main.java: -------------------------------------------------------------------------------- 1 | package com.justin; 2 | 3 | import com.justin.service.pass.IDecrypt; 4 | import com.justin.service.pass.IEncrypt; 5 | import com.justin.service.pass.impl.Decrypt; 6 | import com.justin.service.pass.impl.Encrypt; 7 | 8 | import java.io.IOException; 9 | 10 | /** 11 | * @program: pwd-encrypt-decrypt 12 | * @description: 加解密的主入口 13 | * @author: JustinQin 14 | * @create: 2021/7/18 11:11 15 | * @version: v1.0.0 16 | **/ 17 | public class Main { 18 | public static void main(String[] args) { 19 | int paramLength = args.length; 20 | if((args == null) || ((paramLength != 3) && (paramLength != 2))){ 21 | System.out.println("The param's length must be 2 or 3 !!!"); 22 | System.out.println("Such as:"); 23 | System.out.println("1.encrypt command."); 24 | System.out.println("java -jar pwd.jar host user password"); 25 | System.out.println("2.decrypt command."); 26 | System.out.println("java -jar pwd.jar host user"); 27 | System.exit(1); 28 | } 29 | 30 | String host = args[0]; 31 | String user = args[1]; 32 | String password = (paramLength == 3) ? password = args[2] : null; 33 | 34 | Main main = new Main(); 35 | try { 36 | if (null != password) { 37 | System.out.println(host + "," + user +"," + password); 38 | //明文加密主入口 39 | main.enPass(host,user,password); 40 | System.out.println("Encrypt password is ok."); 41 | }else{ 42 | System.out.println(host + "," + user); 43 | //密文解密主入口 44 | password = main.dePass(host,user); 45 | System.out.println("Decrypt password is ok."); 46 | System.out.println(password); 47 | } 48 | } catch (Exception e) { 49 | e.printStackTrace(); 50 | } 51 | } 52 | 53 | 54 | private void enPass(String host, String user, String password) throws IOException { 55 | IEncrypt iEncrypt = new Encrypt(); 56 | iEncrypt.createPassfile(host, user, password); 57 | } 58 | 59 | private String dePass(String host, String user) throws Exception { 60 | IDecrypt iDecrypt = new Decrypt(); 61 | return iDecrypt.getDepass(host, user); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/com/justin/service/pass/impl/Encrypt.java: -------------------------------------------------------------------------------- 1 | package com.justin.service.pass.impl; 2 | 3 | import java.io.BufferedWriter; 4 | import java.io.File; 5 | import java.io.FileWriter; 6 | import java.io.IOException; 7 | import java.security.MessageDigest; 8 | import java.text.SimpleDateFormat; 9 | import java.util.Date; 10 | 11 | import javax.crypto.Cipher; 12 | import javax.crypto.SecretKey; 13 | import javax.crypto.SecretKeyFactory; 14 | import javax.crypto.spec.DESedeKeySpec; 15 | 16 | import com.justin.constants.Global; 17 | import com.justin.service.pass.IEncrypt; 18 | import com.justin.utils.PwdUtil; 19 | 20 | public class Encrypt implements IEncrypt { 21 | 22 | private static String ALGORITHM = "DESede"; 23 | final byte[] initKeyBytes; 24 | private Cipher encip1; 25 | private Cipher decip1; 26 | private Cipher encip; 27 | private Cipher decip; 28 | private String desKey; 29 | private String dateKey; 30 | 31 | public Encrypt() { 32 | byte[] byteArray = new byte[32]; 33 | byteArray[0] = 12; 34 | byteArray[1] = 42; 35 | byteArray[2] = -33; 36 | byteArray[3] = 25; 37 | byteArray[4] = 126; 38 | byteArray[5] = -25; 39 | byteArray[6] = 66; 40 | byteArray[7] = 74; 41 | byteArray[8] = 19; 42 | byteArray[9] = 2; 43 | byteArray[10] = 51; 44 | byteArray[11] = 15; 45 | byteArray[12] = 11; 46 | byteArray[13] = 88; 47 | byteArray[14] = 22; 48 | byteArray[15] = 34; 49 | byteArray[16] = 12; 50 | byteArray[17] = 42; 51 | byteArray[18] = -33; 52 | byteArray[19] = 25; 53 | byteArray[20] = 126; 54 | byteArray[21] = -25; 55 | byteArray[22] = 66; 56 | byteArray[23] = 74; 57 | byteArray[24] = 19; 58 | byteArray[25] = 2; 59 | byteArray[26] = 51; 60 | byteArray[27] = 15; 61 | byteArray[28] = 11; 62 | byteArray[29] = 88; 63 | byteArray[30] = 22; 64 | byteArray[31] = 34; 65 | this.initKeyBytes = byteArray; 66 | init3DES(); 67 | } 68 | 69 | @Override 70 | public void createPassfile(String host, String user, String password) throws IOException{ 71 | try { 72 | String passFile = getPassFile(); 73 | File file = new File(passFile); 74 | if (!file.exists()) { 75 | file.createNewFile(); 76 | } 77 | String enPass = encryptStr(password); 78 | String md5Date = encrypt1Str(this.dateKey); 79 | 80 | String fileContent = host + ":" + user + ":" + md5Date + ":" + enPass; 81 | String md5FileContent = md5Buffer(fileContent); 82 | 83 | fileContent = fileContent + ":" + md5FileContent; 84 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file,true)); 85 | bufferedWriter.write(fileContent + "\n"); 86 | bufferedWriter.flush(); 87 | bufferedWriter.close(); 88 | } catch (IOException e) { 89 | e.printStackTrace(); 90 | throw e; 91 | } 92 | } 93 | 94 | @Override 95 | public String encryptStr(String password) { 96 | try { 97 | return md2hex(this.encip.doFinal(password.getBytes())); 98 | } catch (Exception e) { 99 | e.printStackTrace(); 100 | } 101 | return null; 102 | } 103 | 104 | private boolean init3DES() { 105 | if (Global.DES_KEY != null) this.desKey = Global.DES_KEY; 106 | try { 107 | SecretKeyFactory secKeyFac1 = SecretKeyFactory.getInstance(ALGORITHM); 108 | DESedeKeySpec desKeySpec1 = new DESedeKeySpec(this.initKeyBytes); 109 | SecretKey desKey1 = secKeyFac1.generateSecret(desKeySpec1); 110 | this.encip1 = Cipher.getInstance(ALGORITHM); 111 | this.decip1 = Cipher.getInstance(ALGORITHM); 112 | this.encip1.init(1, desKey1); 113 | this.decip1.init(2, desKey1); 114 | 115 | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); 116 | this.dateKey = dateFormat.format(new Date()); 117 | this.desKey = md5Buffer(this.dateKey); 118 | 119 | DESedeKeySpec desKeySpec2 = new DESedeKeySpec(this.desKey.getBytes()); 120 | SecretKey desKey2 = secKeyFac1.generateSecret(desKeySpec2); 121 | this.encip = Cipher.getInstance(ALGORITHM); 122 | this.decip = Cipher.getInstance(ALGORITHM); 123 | this.encip.init(1, desKey2); 124 | this.decip.init(2, desKey2); 125 | return true; 126 | } catch (Exception e) { 127 | e.printStackTrace(); 128 | } 129 | return false; 130 | } 131 | 132 | private String md5Buffer(String dateKey) { 133 | try { 134 | MessageDigest md = MessageDigest.getInstance("MD5"); 135 | md.update(dateKey.getBytes()); 136 | return md2hex(md.digest()); 137 | } catch (Exception e) { 138 | e.printStackTrace(); 139 | } 140 | return null; 141 | } 142 | 143 | private String md2hex(byte[] digest) { 144 | String hexString = ""; 145 | String tmp = ""; 146 | 147 | for (int i = 0; i < digest.length; ++i) { 148 | tmp = Integer.toHexString(digest[i] & 0xFF); 149 | if (tmp.length() == 1) { 150 | hexString = hexString + "0" + tmp; 151 | }else{ 152 | hexString = hexString + tmp; 153 | } 154 | } 155 | return hexString.toUpperCase(); 156 | } 157 | 158 | 159 | private String encrypt1Str(String in) { 160 | try { 161 | return md2hex(this.encip1.doFinal(in.getBytes())); 162 | } catch (Exception e) { 163 | e.printStackTrace(); 164 | } 165 | return null; 166 | } 167 | 168 | private String getPassFile() { 169 | String getPassFile = PwdUtil.getPassFile(); 170 | System.out.println("The passFile's is:" + getPassFile); 171 | return getPassFile; 172 | } 173 | 174 | } 175 | -------------------------------------------------------------------------------- /src/com/justin/service/pass/impl/Decrypt.java: -------------------------------------------------------------------------------- 1 | package com.justin.service.pass.impl; 2 | 3 | import java.io.File; 4 | import java.io.FileNotFoundException; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | import java.io.LineNumberReader; 8 | import java.security.MessageDigest; 9 | 10 | import javax.crypto.Cipher; 11 | import javax.crypto.SecretKey; 12 | import javax.crypto.SecretKeyFactory; 13 | import javax.crypto.spec.DESedeKeySpec; 14 | 15 | import com.justin.constants.Global; 16 | import com.justin.service.pass.IDecrypt; 17 | import com.justin.utils.PwdUtil; 18 | 19 | public class Decrypt implements IDecrypt { 20 | 21 | private static String ALGORITHM = "DESede"; 22 | final byte[] initKeyBytes; 23 | private Cipher encip1; 24 | private Cipher decip1; 25 | private Cipher encip; 26 | private Cipher decip; 27 | private String desKey; 28 | private String dateKey; 29 | 30 | public Decrypt() { 31 | byte[] byteArray = new byte[32]; 32 | byteArray[0] = 12; 33 | byteArray[1] = 42; 34 | byteArray[2] = -33; 35 | byteArray[3] = 25; 36 | byteArray[4] = 126; 37 | byteArray[5] = -25; 38 | byteArray[6] = 66; 39 | byteArray[7] = 74; 40 | byteArray[8] = 19; 41 | byteArray[9] = 2; 42 | byteArray[10] = 51; 43 | byteArray[11] = 15; 44 | byteArray[12] = 11; 45 | byteArray[13] = 88; 46 | byteArray[14] = 22; 47 | byteArray[15] = 34; 48 | byteArray[16] = 12; 49 | byteArray[17] = 42; 50 | byteArray[18] = -33; 51 | byteArray[19] = 25; 52 | byteArray[20] = 126; 53 | byteArray[21] = -25; 54 | byteArray[22] = 66; 55 | byteArray[23] = 74; 56 | byteArray[24] = 19; 57 | byteArray[25] = 2; 58 | byteArray[26] = 51; 59 | byteArray[27] = 15; 60 | byteArray[28] = 11; 61 | byteArray[29] = 88; 62 | byteArray[30] = 22; 63 | byteArray[31] = 34; 64 | this.initKeyBytes = byteArray; 65 | } 66 | 67 | @Override 68 | public String getDepass(String host, String user) throws FileNotFoundException, IOException, Exception { 69 | String enPass = getEnPassFromFile(host,user); 70 | init3DES(); 71 | String dePass = decryptStr(enPass); 72 | return dePass; 73 | } 74 | 75 | @Override 76 | public String getDepass(String passfileContent) throws Exception { 77 | String enPass = getEnpassFromContent(passfileContent); 78 | init3DES(); 79 | String dePass = decryptStr(enPass); 80 | return dePass; 81 | } 82 | 83 | @Override 84 | public String decryptStr(String str) { 85 | try { 86 | byte[] hex2md = hex2md(str); 87 | byte[] doFinal = this.decip.doFinal(hex2md); 88 | return new String(doFinal); 89 | } catch (Exception e) { 90 | e.printStackTrace(); 91 | } 92 | return null; 93 | } 94 | 95 | private boolean init3DES() { 96 | if (Global.DES_KEY != null) this.desKey = Global.DES_KEY; 97 | try { 98 | SecretKeyFactory secKeyFac1 = SecretKeyFactory.getInstance(ALGORITHM); 99 | DESedeKeySpec desKeySpec1 = new DESedeKeySpec(this.initKeyBytes); 100 | SecretKey desKey1 = secKeyFac1.generateSecret(desKeySpec1); 101 | this.encip1 = Cipher.getInstance(ALGORITHM); 102 | this.decip1 = Cipher.getInstance(ALGORITHM); 103 | this.encip1.init(1, desKey1); 104 | this.decip1.init(2, desKey1); 105 | 106 | byte[] keyBytes = md5Buffer(decrypt1Str(this.dateKey)).getBytes(); 107 | 108 | DESedeKeySpec desKeySpec2 = new DESedeKeySpec(keyBytes); 109 | SecretKey desKey2 = secKeyFac1.generateSecret(desKeySpec2); 110 | this.encip = Cipher.getInstance(ALGORITHM); 111 | this.decip = Cipher.getInstance(ALGORITHM); 112 | this.encip.init(1, desKey2); 113 | this.decip.init(2, desKey2); 114 | return true; 115 | } catch (Exception e) { 116 | e.printStackTrace(); 117 | } 118 | return false; 119 | } 120 | 121 | private String decrypt1Str(String in) { 122 | try { 123 | return new String(this.decip1.doFinal(hex2md(in))); 124 | } catch (Exception e) { 125 | e.printStackTrace(); 126 | } 127 | return null; 128 | } 129 | 130 | 131 | private byte[] hex2md(String in) { 132 | if (in.length() % 2 != 0 ) in = in + "0"; 133 | byte[] bytes = new byte[in.length() / 2]; 134 | for (int i = 0; i < in.length() / 2; ++i) { 135 | byte[] temp = new byte[2]; 136 | temp[0] = (byte) in.charAt(i * 2); 137 | temp[1] = (byte) in.charAt(i * 2 + 1); 138 | bytes[i] = (byte) Integer.parseInt(new String(temp),16); 139 | } 140 | return bytes; 141 | } 142 | 143 | /** 144 | * 145 | * @param host 146 | * @param user 147 | * @return 148 | * @throws FileNotFoundException 149 | * @throws IOException 150 | * @throws Exception 151 | */ 152 | @SuppressWarnings("resource") 153 | private String getEnPassFromFile(String host, String user) throws FileNotFoundException,IOException,Exception { 154 | String passFileName = getFileName(); 155 | File passFile = new File(passFileName); 156 | try { 157 | LineNumberReader read = new LineNumberReader(new FileReader(passFile)); 158 | String enPass = null; 159 | boolean flag = true; 160 | while (flag) { 161 | String readLine = read.readLine(); 162 | if (null == readLine) { 163 | flag = false; 164 | break; 165 | } 166 | enPass = readLine; 167 | } 168 | 169 | if ((null == enPass) || (null != enPass && !enPass.startsWith(host + ":" + user + ":"))) throw new Exception("The request password not found!"); 170 | 171 | String[] enPassArr = enPass.split(":"); 172 | boolean isMd5OK = md5StrOk(enPassArr[0] + ":" + enPassArr[1] + ":" + enPassArr[2] + ":"+ enPassArr[3],enPassArr[4]); 173 | if (!isMd5OK) throw new Exception("The password file is change!"); 174 | 175 | read.close(); 176 | this.dateKey = enPassArr[2]; 177 | return enPassArr[3]; 178 | } catch (FileNotFoundException e) { 179 | e.printStackTrace(); 180 | throw e; 181 | }catch (IOException e) { 182 | e.printStackTrace(); 183 | throw e; 184 | } 185 | } 186 | 187 | /** 188 | * 189 | * @param passfileContent 190 | * @return 191 | * @throws Exception 192 | */ 193 | private String getEnpassFromContent(String passfileContent) throws Exception { 194 | if (null == passfileContent) throw new Exception("The request password not found!"); 195 | 196 | String[] contentArry = passfileContent.split(":"); 197 | boolean isMd5OK = md5StrOk(contentArry[0] + ":" + contentArry[1] + ":" + contentArry[2] + ":"+ contentArry[3],contentArry[4]); 198 | 199 | if (!isMd5OK) throw new Exception("The password file is change!"); 200 | 201 | this.dateKey = contentArry[2]; 202 | 203 | return contentArry[3]; 204 | } 205 | 206 | private boolean md5StrOk(String from,String md5) { 207 | if (md5 == null) return false; 208 | return md5Buffer(from).equals(md5); 209 | } 210 | 211 | private String md5Buffer(String strSrc) { 212 | try { 213 | MessageDigest md = MessageDigest.getInstance("MD5"); 214 | md.update(strSrc.getBytes()); 215 | return md2hex(md.digest()); 216 | } catch (Exception e) { 217 | e.printStackTrace(); 218 | } 219 | return null; 220 | } 221 | 222 | private String md2hex(byte[] digest) { 223 | String hexString = ""; 224 | String tmp = ""; 225 | 226 | for (int i = 0; i < digest.length; ++i) { 227 | tmp = Integer.toHexString(digest[i] & 0xFF); 228 | if (tmp.length() == 1) { 229 | hexString = hexString + "0" + tmp; 230 | }else{ 231 | hexString = hexString + tmp; 232 | } 233 | } 234 | return hexString.toUpperCase(); 235 | } 236 | 237 | private String getFileName() { 238 | String passFile = PwdUtil.getPassFile(); 239 | System.out.println("The password file path is:" + passFile); 240 | return passFile; 241 | } 242 | 243 | } 244 | --------------------------------------------------------------------------------