教育训练处考试制证系统后端
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
package com.gkhy.exam.pay.utils;
 
import com.alibaba.fastjson2.JSONObject;
import org.dom4j.*;
 
public class BillSignUtils {
 
    private static final String HEADER_TAG = "Header";
    private static final String EINVOICE_TAG = "EInvoiceData";
 
 
//    public static String signBill(byte[] bytes) throws BillSignException {
//        /*  41 */
//        return signBill(new String(bytes));
//
//    }
//
//
//    public static 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);
//
//        }
//
//    }
 
 
    public static String signBill(String xml, JSONObject data) throws BillSignException {
        /*  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, data);
 
 
        /*  92 */
        addUnitSign(xmlDoc, signNode);
 
        /*  94 */
        return xmlDoc.asXML();
 
    }
 
 
    private static Node genUnitSignNode(String plain, JSONObject data) {
        /* 100 */
        SignDto signDto = new SignDto(data.getDate("signTime"), data.getString("signResult"), data.getString("issure"), data.getString("sn"));
 
        /* 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(signDto.getSignatureAlgorithm());
        /* 109 */
        signedInfo.addElement("SignatureFormat").setText(signDto.getSignatureFormat());
 
        /* 111 */
        signature.addElement("SignatureTime").setText(signDto.getSignatureTime());
        /* 112 */
        signature.addElement("SignatureValue").setText(signDto.getSignatureValue());
 
        /* 114 */
        Element keyInfo = signature.addElement("KeyInfo");
        /* 115 */
        keyInfo.addElement("SerialNumber").setText(signDto.getSerialNumber());
        /* 116 */
        keyInfo.addElement("X509IssuerName").setText(signDto.getIssuerDn());
        /* 117 */
        return (Node) signature;
 
    }
 
 
    private static 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 static void addUnitSign(Document xmlDoc, Node signNode) {
        /* 146 */
        Element eInvoiceSignature = xmlDoc.getRootElement().addElement("EInvoiceSignature");
        /* 147 */
        eInvoiceSignature.add(signNode);
 
    }
 
}