P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
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
package foundation.util;
 
import java.nio.charset.Charset;
import java.util.Arrays;
 
public class PKCS7Padding {
    private static final Charset CHARSET = Charset.forName("utf-8");
    private static final int BLOCK_SIZE = 32;
 
    public PKCS7Padding() {
    }
 
    public static byte[] getPaddingBytes(int count) {
        int amountToPad = 32 - count % 32;
        if (amountToPad == 0) {
            amountToPad = 32;
        }
 
        char padChr = chr(amountToPad);
        String tmp = new String();
 
        for (int index = 0; index < amountToPad; ++index) {
            tmp = tmp + padChr;
        }
 
        return tmp.getBytes(CHARSET);
    }
 
    public static byte[] removePaddingBytes(byte[] decrypted) {
        int pad = decrypted[decrypted.length - 1];
        if (pad < 1 || pad > 32) {
            pad = 0;
        }
 
        return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
    }
 
    private static char chr(int a) {
        byte target = (byte)(a & 255);
        return (char)target;
    }
}