派生自 projectDept/qhighschool

yn147
2023-05-10 96286178ee1c257c130cb2ad964a781f36c4eee5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.qxueyou.scc.base.util;
 
 
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.caucho.hessian.client.HessianProxyFactory;
//import com.qxueyou.scc.base.service.IConfigService;
 
/**
 * RSA加解密--调用远程加解密服务
 * @author 德虎
 *
 */
public class RSAUtils {
 
    private static Logger log = LogManager.getLogger(RSAUtils.class);
    
    private static RSAClient client;
    
    /**
     * 构建远程RSA加解密客户端
     */
    private static void buildClient() {
        try {
            String  url = null;
            HessianProxyFactory hessianFactory = new HessianProxyFactory();
            client = (RSAClient) hessianFactory.create(RSAClient.class, url);
        } catch (Exception e) {
            log.error(e, e);
        }
    }
    
    /**
     * 调用远程加密服务对字符串加密
     * @param string
     * @return
     */
    public static String encrypt(String string) {
        if (client == null) {
            buildClient();
        }
        if (StringUtils.isEmpty(string)) {
            return "";
        }
        try {
            return client.encrypt(string);
        } catch (Exception e) {
            log.error("加解密服务初始化错误:"+e.getMessage(), e);
        }
        return null;
    }
    
    /**
     * 调用远程解密服务对密文字符串解决
     * @param string
     * @return
     */
    public static String decrypt(String string) {
        if (client == null) {
            buildClient();
        }
        if (StringUtils.isEmpty(string)) {
            return "";
        }
        try {
            return client.decrypt(string);
        } catch (Exception e) {
            log.error(e, e);
        }
        return null;
    }
 
    /**
     * Hessian本地接口
     * @author user
     *
     */
    interface RSAClient {
        String encrypt(String string);
        String decrypt(String string);
    }
 
}