教育训练处考试制证系统后端
heheng
2025-02-21 5b8607464905c4a585fb8e859985730e1ee61f40
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.gkhy.exam.pay.utils;
 
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.io.FileUtils;
import org.dom4j.*;
 
import java.io.File;
import java.io.IOException;
import java.util.Map;
 
public class BillSign {
    ISignCommond signCommond;
    private static final String HEADER_TAG = "Header";
    private static final String EINVOICE_TAG = "EInvoiceData";
 
 
    public BillSign(ISignCommond signCommond) {
        /*  28 */
        this.signCommond = signCommond;
 
    }
 
    public BillSign(){}
 
 
    public String signBill(byte[] bytes) throws Exception {
        /*  41 */
        return signBill(new String(bytes));
 
    }
 
 
    public String signBill(File file) throws BillSignException {
        /*  54 */
        if (!file.exists()) {
            /*  55 */
            throw new BillSignException("文件不存在。文件名称" + file.getAbsolutePath());
 
        }
 
 
        try {
            /*  59 */
            byte[] bytes = FileUtils.readFileToByteArray(file);
            /*  60 */
            return signBill(new String(bytes));
            /*  61 */
        } catch (IOException e) {
            /*  62 */
            throw new BillSignException("文件读取失败。文件名称" + file.getAbsolutePath(), e);
 
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
 
    }
 
 
    public String signBill(String xml) throws Exception {
        /*  78 */
        Document xmlDoc = null;
 
        try {
            /*  80 */
            xmlDoc = DocumentHelper.parseText(xml);
            /*  81 */
        } catch (DocumentException e) {
            /*  82 */
            throw new BillSignException("解析票据文件失败。", e);
 
        }
 
 
        /*  86 */
        String plain = readRefSignDto(xmlDoc);
 
 
        /*  89 */
        Node signNode = genUnitSignNode(plain);
 
 
        /*  92 */
        addUnitSign(xmlDoc, signNode);
 
        /*  94 */
        return xmlDoc.asXML();
 
    }
 
 
    private Node genUnitSignNode(String plain) throws Exception {
        /* 100 */
        PayUtils payUtils = new PayUtils();
        JSONObject jsonObject = payUtils.signFileV2(plain);
        SignDto content = (SignDto) jsonObject.get("content");
 
        /* 102 */
        Document document = DocumentHelper.createDocument();
        /* 103 */
        Element signature = document.addElement("Signature");
        /* 104 */
        signature.addAttribute("id", "InvoicingParty");
 
        /* 106 */
        Element signedInfo = signature.addElement("SignedInfo");
        /* 107 */
        signedInfo.addElement("Reference").addAttribute("URI", "/EInvoice/Header|/EInvoice/EInvoiceData");
        /* 108 */
        signedInfo.addElement("SignatureAlgorithm").setText(content.getSignatureAlgorithm());
        /* 109 */
        signedInfo.addElement("SignatureFormat").setText(content.getSignatureFormat());
 
        /* 111 */
        signature.addElement("SignatureTime").setText(content.getSignatureTime());
        /* 112 */
        signature.addElement("SignatureValue").setText(content.getSignatureValue());
 
        /* 114 */
        Element keyInfo = signature.addElement("KeyInfo");
        /* 115 */
        keyInfo.addElement("SerialNumber").setText(content.getSerialNumber());
        /* 116 */
        keyInfo.addElement("X509IssuerName").setText(content.getIssuerDn());
        /* 117 */
        return (Node) signature;
 
    }
 
 
    public String readRefSignDto(Document xmlDoc) throws BillSignException {
        /* 130 */
        Element root = xmlDoc.getRootElement();
        /* 131 */
        if (root.element("Header") == null || root.element("EInvoiceData") == null) {
            /* 132 */
            throw new BillSignException("票据文件格式不正确");
 
        }
        /* 134 */
        return root.element("Header").asXML() + root.element("EInvoiceData").asXML();
 
    }
 
 
    private void addUnitSign(Document xmlDoc, Node signNode) {
        /* 146 */
        Element eInvoiceSignature = xmlDoc.getRootElement().addElement("EInvoiceSignature");
        /* 147 */
        eInvoiceSignature.add(signNode);
 
    }
 
}