zbb378@sohu.com
2024-11-04 b2bad51be3c8d3e78d7f81a19415faeac2d0297c
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
package com.nova.sankuai.infra.utils.huifubao.common;
 
import org.springframework.stereotype.Component;
 
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
 
 
@Component
public class SignEncrypt {
 
    public static final String BC_PROV_ALGORITHM_SHA1RSA = "SHA1withRSA";
 
    private static final int MAX_ENCRYPT_BLOCK = 117;
 
    //private static final int MAX_DECRYPT_BLOCK = 128;
    private static final int MAX_DECRYPT_BLOCK = 256;
 
    public boolean verify(String data, String sign, PublicKey publicKey) {
        try {
            Signature signature = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA);
            signature.initVerify(publicKey);
            signature.update(data.getBytes(Constants.UTF));
            return signature.verify(org.bouncycastle.util.encoders.Base64.decode(sign.getBytes()));
        } catch (Exception e) {
            return false;
        }
    }
 
 
    public String sign(String obj, PrivateKey privateKey) {
        String sign = "";
        try {
            Signature signature = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA);
            signature.initSign(privateKey);
            signature.update(obj.getBytes(Constants.UTF));
            sign = new String(org.bouncycastle.util.encoders.Base64.encode(signature.sign()));
        } catch (Exception e) {
        }
        return sign;
    }
 
    public String encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        int inputLen = data.getBytes().length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offset = 0;
        byte[] cache;
        int i = 0;
        while (inputLen - offset > 0) {
            if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
            }
            out.write(cache, 0, cache.length);
            i++;
            offset = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return new String(org.bouncycastle.util.encoders.Base64.encode(encryptedData), Constants.UTF);
    }
 
    public String decrypt(String data, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] dataBytes = org.bouncycastle.util.encoders.Base64.decode(data.getBytes());
        int inputLen = dataBytes.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offset = 0;
        byte[] cache;
        int i = 0;
        while (inputLen - offset > 0) {
            if (inputLen - offset > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
            }
            out.write(cache, 0, cache.length);
            i++;
            offset = i * MAX_DECRYPT_BLOCK;
        }
        byte[] decryptedData = out.toByteArray();
        out.close();
        return new String(decryptedData, Constants.UTF);
    }
 
}