initKeyPairBase64() throws Exception {
8 | return initKeyPairBase64("EC");
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/sign001rsa/JdkRsaSign.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sign001rsa;
2 |
3 | import org.apache.commons.codec.binary.Hex;
4 |
5 | import java.security.*;
6 | import java.security.interfaces.RSAPrivateKey;
7 | import java.security.interfaces.RSAPublicKey;
8 | import java.security.spec.PKCS8EncodedKeySpec;
9 | import java.security.spec.X509EncodedKeySpec;
10 | import java.util.AbstractMap;
11 | import java.util.Map;
12 |
13 | /**
14 | * RSA签名流程:
15 | *
16 | * 发送方—>构建密钥对-》公布密钥给接收方—>使用私钥对数据签名-》发送签名、数据给接收方。
17 | * 接收方—》使用公钥、签名验证数据
18 | *
19 | * @author Administrator
20 | */
21 | /**
22 | *
23 | * 使用rsa签名 发送方—>构建密钥对-》公布密钥给接收方。 发送方—>使用私钥对数据签名-》发送签名、数据给接收方—》接收方使用公钥、签名验证数据
24 | */
25 | /**
26 | * 算法 密钥长度 默认长度 签名长度 实现的方
27 | * MD2withRSA 512-65536(64的整数倍) 1024 同密钥 JDK8
28 | * MD5withRSA 同上 1024 同密钥 JDK8
29 | * SHA1withRSA ... 1024 同密钥 JDK8
30 | * SHA224withRSA ... 2048 同密钥 JDK8
31 | * SHA256withRSA ... 2048 同密钥 JDK8
32 | * SHA384withRSA ... 2048 同密钥 JDK8
33 | * SHA512withRSA ... 2048 同密钥 JDK8
34 | * RIPEMD128withRSA 2048 同密钥 BC
35 | * RIPEMD160withRSA 同上 2048 同密钥 BC
36 | */
37 | public class JdkRsaSign {
38 | public final static String RSA = "RSA";
39 | public final static String MD2withRSA = "MD2withRSA";
40 | public final static String MD5withRSA = "MD5withRSA";
41 | public final static String SHA1withRSA = "SHA1withRSA";
42 | public final static String SHA224withRSA = "SHA224withRSA";
43 | public final static String SHA256withRSA = "SHA256withRSA";
44 | public final static String SHA384withRSA = "SHA384withRSA";
45 | public final static String SHA512withRSA = "SHA512withRSA";
46 |
47 | /**
48 | * // 1.初始化密钥
49 | *
50 | * @param algorithm
51 | * @param keySize
52 | * @return
53 | * @throws NoSuchAlgorithmException
54 | */
55 | public static Map.Entry initKeyPair(String algorithm, Integer keySize) throws NoSuchAlgorithmException {
56 | KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
57 | keyPairGenerator.initialize(keySize);
58 | KeyPair keyPair = keyPairGenerator.generateKeyPair();
59 | RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
60 | RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
61 | return new AbstractMap.SimpleEntry<>(rsaPublicKey.getEncoded(), rsaPrivateKey.getEncoded());
62 | }
63 |
64 | /**
65 | * 执行签名
66 | *
67 | * @return
68 | */
69 | public static byte[] prikeySign(String algorithm, String signAlgorithm, byte[] priKey, byte[] data) throws Exception {
70 | PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(priKey);
71 | KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
72 | PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
73 | Signature signature = Signature.getInstance(signAlgorithm);
74 | signature.initSign(privateKey);
75 | signature.update(data);
76 | return signature.sign();
77 | }
78 |
79 | /**
80 | * 执行签名
81 | *
82 | * @return
83 | */
84 | public static boolean pubKeyCheckSign(String algorithm, String signAlgorithm, byte[] pubKey, byte[] data, byte[] sign) throws Exception {
85 | X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey);
86 | KeyFactory keyFactory = KeyFactory.getInstance(algorithm);// 为了数据的完整性
87 | PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
88 |
89 | Signature signature = Signature.getInstance(signAlgorithm);
90 | signature.initVerify(publicKey);
91 | signature.update(data);
92 | return signature.verify(sign);
93 | }
94 | }
95 |
96 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/sign002dsa/JdkDsa.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sign002dsa;
2 |
3 | import java.security.*;
4 | import java.security.interfaces.DSAPrivateKey;
5 | import java.security.interfaces.DSAPublicKey;
6 | import java.security.spec.PKCS8EncodedKeySpec;
7 | import java.security.spec.X509EncodedKeySpec;
8 | import java.util.AbstractMap;
9 | import java.util.Map;
10 |
11 | public class JdkDsa {
12 | public static final String DSA = "DSA";
13 | public static final String SHA1withDSA = "SHA1withDSA";
14 | public static final String SHA224withDSA = "SHA224withDSA";
15 | public static final String SHA256withDSA = "SHA256withDSA";
16 | public static final String SHA384withDSA = "SHA384withDSA";
17 | public static final String SHA512withDSA = "SHA512withDSA";
18 |
19 | /**
20 | * 默认密钥字节数
21 | *
22 | *
23 | * DSA
24 | * Default Keysize 1024
25 | * Keysize must be a multiple of 64, ranging from 512 to 1024 (inclusive).
26 | *
27 | */
28 | private static final int KEY_SIZE = 1024;
29 |
30 | /**
31 | * 默认种子
32 | */
33 | private static final String DEFAULT_SEED = "0f22507a10bbddd07d8a3082122966e3";
34 |
35 | /**
36 | * 用私钥对信息生成数字签名
37 | *
38 | * @param data 加密数据
39 | * @param privateKey 私钥
40 | * @return
41 | * @throws Exception
42 | */
43 | public static byte[] sign(String signAlgorithm, byte[] priKeyBytes, byte[] data) throws Exception {
44 | // 构造PKCS8EncodedKeySpec对象
45 | PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKeyBytes);
46 | // KEY_ALGORITHM 指定的加密算法
47 | KeyFactory keyFactory = KeyFactory.getInstance(JdkDsa.DSA);
48 | // 取私钥匙对象
49 | PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
50 | // 用私钥对信息生成数字签名
51 | Signature signature = Signature.getInstance(signAlgorithm);
52 | signature.initSign(priKey);
53 | signature.update(data);
54 | return signature.sign();
55 | }
56 |
57 | /**
58 | * 校验数字签名
59 | *
60 | * @param data 加密数据
61 | * @param publicKey 公钥
62 | * @param sign 数字签名
63 | * @return 校验成功返回true 失败返回false
64 | * @throws Exception
65 | */
66 | public static boolean verify(String signAlgorithm, byte[] publKeyBytes, byte[] data, byte[] sign) throws Exception {
67 | // 构造X509EncodedKeySpec对象
68 | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publKeyBytes);
69 | // ALGORITHM 指定的加密算法
70 | KeyFactory keyFactory = KeyFactory.getInstance(JdkDsa.DSA);
71 | // 取公钥匙对象
72 | PublicKey pubKey = keyFactory.generatePublic(keySpec);
73 | Signature signature = Signature.getInstance(signAlgorithm);
74 | signature.initVerify(pubKey);
75 | signature.update(data);
76 | // 验证签名是否正常
77 | return signature.verify(sign);
78 | }
79 |
80 | /**
81 | * 生成密钥
82 | *
83 | * @param seed 种子
84 | * @return 密钥对象
85 | * @throws Exception
86 | */
87 | public static Map.Entry initKey(String seed) throws Exception {
88 | KeyPairGenerator keygen = KeyPairGenerator.getInstance(JdkDsa.DSA);
89 | // 初始化随机产生器
90 | SecureRandom secureRandom = new SecureRandom();
91 | secureRandom.setSeed(seed.getBytes());
92 | keygen.initialize(JdkDsa.KEY_SIZE, secureRandom);
93 | KeyPair keys = keygen.genKeyPair();
94 | DSAPublicKey publicKey = (DSAPublicKey) keys.getPublic();
95 | DSAPrivateKey privateKey = (DSAPrivateKey) keys.getPrivate();
96 | return new AbstractMap.SimpleEntry<>(publicKey.getEncoded(), privateKey.getEncoded());
97 | }
98 |
99 | /**
100 | * 默认生成密钥
101 | *
102 | * @return 密钥对象
103 | * @throws Exception
104 | */
105 | public static Map.Entry initKey() throws Exception {
106 | return JdkDsa.initKey(JdkDsa.DEFAULT_SEED);
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/sign003ecc/JdkEcdsa.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sign003ecc;
2 |
3 | import java.security.*;
4 | import java.security.spec.PKCS8EncodedKeySpec;
5 | import java.security.spec.X509EncodedKeySpec;
6 |
7 | public class JdkEcdsa {
8 | public static final String EC = "EC";
9 | public static final String NONEwithECDSA = "NONEwithECDSA";
10 | public static final String RIPEMD160withECDSA = "RIPEMD160withECDSA";
11 | public static final String SHA1withECDSA = "SHA1withECDSA";
12 | public static final String SHA224withECDSA = "SHA224withECDSA";
13 | public static final String SHA256withECDSA = "SHA256withECDSA";
14 | public static final String SHA384withECDSA = "SHA384withECDSA";
15 | public static final String SHA512withECDSA = "SHA512withECDSA";
16 |
17 | public static KeyPair initKey(int keySize, byte[] seed) throws Exception {
18 | KeyPairGenerator keygen = KeyPairGenerator.getInstance(JdkEcdsa.EC);
19 | // 初始化随机产生器
20 | SecureRandom secureRandom = new SecureRandom();
21 | secureRandom.setSeed(seed);
22 | keygen.initialize(keySize, secureRandom);
23 | KeyPair keys = keygen.genKeyPair();
24 | return keys;
25 | }
26 |
27 | public static KeyPair initKey(int keySize) throws Exception {
28 | return initKey(keySize, new SecureRandom().generateSeed(8));
29 | }
30 |
31 | public static byte[] sign(String signAlgorithm, PrivateKey privateKey, byte[] data) throws Exception {
32 | // 2.执行签名[私钥签名]
33 | Signature signature = Signature.getInstance(signAlgorithm);
34 | signature.initSign(privateKey);
35 | signature.update(data);
36 | return signature.sign();
37 | }
38 |
39 | public static byte[] sign(String signAlgorithm, byte[] privateKeyByte, byte[] data) throws Exception {
40 | KeyFactory keyFactory = KeyFactory.getInstance(EC);
41 | PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyByte);
42 | PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
43 |
44 | return sign(signAlgorithm, privateKey, data);
45 | }
46 |
47 | public static boolean verify(String signAlgorithm, byte[] publKeyBytes, byte[] data, byte[] sign) throws Exception {
48 | // 3.验证签名[公钥验签]
49 | X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publKeyBytes);
50 | KeyFactory keyFactory = KeyFactory.getInstance(EC);
51 | PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
52 |
53 | return verify(signAlgorithm,publicKey,data,sign);
54 | }
55 |
56 | public static boolean verify(String signAlgorithm, PublicKey publicKey, byte[] data, byte[] sign) throws Exception {
57 | // 3.验证签名[公钥验签]
58 | Signature signature = Signature.getInstance(signAlgorithm);
59 | signature.initVerify(publicKey);
60 | signature.update(data);
61 | return signature.verify(sign);
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/sm/BcSm2Util.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sm;
2 |
3 | import org.bouncycastle.asn1.gm.GMObjectIdentifiers;
4 | import org.bouncycastle.crypto.InvalidCipherTextException;
5 | import org.bouncycastle.crypto.engines.SM2Engine;
6 | import org.bouncycastle.crypto.params.ECDomainParameters;
7 | import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
8 | import org.bouncycastle.crypto.params.ECPublicKeyParameters;
9 | import org.bouncycastle.crypto.params.ParametersWithRandom;
10 | import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
11 | import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
12 | import org.bouncycastle.jce.provider.BouncyCastleProvider;
13 | import org.bouncycastle.jce.spec.ECParameterSpec;
14 | import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
15 |
16 | import javax.crypto.Cipher;
17 | import javax.crypto.KeyGenerator;
18 | import javax.crypto.spec.SecretKeySpec;
19 | import java.security.*;
20 | import java.security.cert.X509Certificate;
21 | import java.util.Arrays;
22 | import java.util.Base64;
23 |
24 | /**
25 | * @author lihongxu6
26 | * @version 1.0
27 | * @className BcSm4Util
28 | * @description TODO
29 | * @date 2021-01-12 22:34
30 | */
31 | public class BcSm2Util {
32 | static {
33 | Security.addProvider(new BouncyCastleProvider());
34 | }
35 |
36 | /**
37 | * 根据publicKey对原始数据data,使用SM2加密
38 | */
39 | public static byte[] encrypt(byte[] data, PublicKey publicKey) {
40 | ECPublicKeyParameters localECPublicKeyParameters = null;
41 |
42 | if (publicKey instanceof BCECPublicKey) {
43 | BCECPublicKey localECPublicKey = (BCECPublicKey) publicKey;
44 | ECParameterSpec localECParameterSpec = localECPublicKey.getParameters();
45 | ECDomainParameters localECDomainParameters = new ECDomainParameters(localECParameterSpec.getCurve(),
46 | localECParameterSpec.getG(), localECParameterSpec.getN());
47 | localECPublicKeyParameters = new ECPublicKeyParameters(localECPublicKey.getQ(), localECDomainParameters);
48 | }
49 | SM2Engine localSM2Engine = new SM2Engine();
50 | localSM2Engine.init(true, new ParametersWithRandom(localECPublicKeyParameters, new SecureRandom()));
51 | byte[] arrayOfByte2;
52 | try {
53 | arrayOfByte2 = localSM2Engine.processBlock(data, 0, data.length);
54 | return arrayOfByte2;
55 | } catch (InvalidCipherTextException e) {
56 |
57 | e.printStackTrace();
58 | return null;
59 | }
60 | }
61 |
62 | /**
63 | * 根据privateKey对加密数据encodedata,使用SM2解密
64 | */
65 | public static byte[] decrypt(byte[] encodedata, PrivateKey privateKey) {
66 | SM2Engine localSM2Engine = new SM2Engine();
67 | BCECPrivateKey sm2PriK = (BCECPrivateKey) privateKey;
68 | ECParameterSpec localECParameterSpec = sm2PriK.getParameters();
69 | ECDomainParameters localECDomainParameters = new ECDomainParameters(localECParameterSpec.getCurve(),
70 | localECParameterSpec.getG(), localECParameterSpec.getN());
71 | ECPrivateKeyParameters localECPrivateKeyParameters = new ECPrivateKeyParameters(sm2PriK.getD(),
72 | localECDomainParameters);
73 | localSM2Engine.init(false, localECPrivateKeyParameters);
74 | try {
75 | byte[] arrayOfByte3 = localSM2Engine.processBlock(encodedata, 0, encodedata.length);
76 | return arrayOfByte3;
77 | } catch (InvalidCipherTextException e) {
78 | e.printStackTrace();
79 | return null;
80 | }
81 | }
82 |
83 | /**
84 | * 私钥签名
85 | */
86 | public static byte[] signByPrivateKey(byte[] data, PrivateKey privateKey) throws Exception {
87 | Signature sig = Signature.getInstance(GMObjectIdentifiers.sm2sign_with_sm3.toString(), BouncyCastleProvider.PROVIDER_NAME);
88 | sig.initSign(privateKey);
89 | sig.update(data);
90 | byte[] ret = sig.sign();
91 | return ret;
92 | }
93 |
94 | /**
95 | * 公钥验签
96 | */
97 | public static boolean verifyByPublicKey(byte[] data, PublicKey publicKey, byte[] signature) throws Exception {
98 | Signature sig = Signature.getInstance(GMObjectIdentifiers.sm2sign_with_sm3.toString(), BouncyCastleProvider.PROVIDER_NAME);
99 | sig.initVerify(publicKey);
100 | sig.update(data);
101 | boolean ret = sig.verify(signature);
102 | return ret;
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/sm/BcSm3Util.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sm;
2 |
3 | import org.bouncycastle.crypto.digests.SM3Digest;
4 | import org.bouncycastle.crypto.macs.HMac;
5 | import org.bouncycastle.crypto.params.KeyParameter;
6 | import org.bouncycastle.jce.provider.BouncyCastleProvider;
7 |
8 | import java.security.MessageDigest;
9 | import java.security.Security;
10 |
11 | /**
12 | * @author lihongxu6
13 | * @version 1.0
14 | * @className BcSm4Util
15 | * @description TODO
16 | * @date 2021-01-12 22:34
17 | */
18 | public class BcSm3Util {
19 | static {
20 | Security.addProvider(new BouncyCastleProvider());
21 | }
22 |
23 | public static byte[] sm3(byte[] srcData) {
24 | SM3Digest sm3Digest = new SM3Digest();
25 | sm3Digest.update(srcData, 0, srcData.length);
26 | byte[] hash = new byte[sm3Digest.getDigestSize()];
27 | sm3Digest.doFinal(hash, 0);
28 | return hash;
29 | }
30 |
31 | public static String sm3Hex(byte[] srcData) {
32 | byte[] hash = sm3(srcData);
33 | String hexString = org.apache.commons.codec.binary.Hex.encodeHexString(hash);
34 | return hexString;
35 | }
36 |
37 | public static byte[] hmacSm3(byte[] key, byte[] srcData) {
38 | KeyParameter keyParameter = new KeyParameter(key);
39 | SM3Digest digest = new SM3Digest();
40 | HMac mac = new HMac(digest);
41 | mac.init(keyParameter);
42 | mac.update(srcData, 0, srcData.length);
43 | byte[] hash = new byte[mac.getMacSize()];
44 | mac.doFinal(hash, 0);
45 | return hash;
46 | }
47 |
48 | public static String hmacSm3Hex(byte[] key, byte[] srcData) {
49 | byte[] hash = hmacSm3(key, srcData);
50 | String hexString = org.apache.commons.codec.binary.Hex.encodeHexString(hash);
51 | return hexString;
52 | }
53 |
54 | public static byte[] sm3bc(byte[] srcData) throws Exception {
55 | MessageDigest messageDigest = MessageDigest.getInstance("SM3", "BC");
56 | byte[] digest = messageDigest.digest(srcData);
57 | return digest;
58 | }
59 |
60 | public static String sm3bcHex(byte[] srcData) throws Exception {
61 | byte[] hash = sm3bc(srcData);
62 | String hexString = org.apache.commons.codec.binary.Hex.encodeHexString(hash);
63 | return hexString;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/sm/BcSm4Util.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sm;
2 |
3 | import org.bouncycastle.jce.provider.BouncyCastleProvider;
4 |
5 | import javax.crypto.Cipher;
6 | import javax.crypto.KeyGenerator;
7 | import javax.crypto.spec.IvParameterSpec;
8 | import javax.crypto.spec.SecretKeySpec;
9 | import java.security.*;
10 |
11 | /**
12 | * @author lihongxu6
13 | * @version 1.0
14 | * @className BcSm4Util
15 | * @description TODO
16 | * @date 2021-01-12 22:34
17 | */
18 | public class BcSm4Util {
19 | static {
20 | Security.addProvider(new BouncyCastleProvider());
21 | }
22 |
23 | public static final String ALGORITHM_NAME = "SM4";
24 | public static final String DEFAULT_KEY = "random_seed";
25 | // 128-32位16进制;256-64位16进制
26 | public static final int DEFAULT_KEY_SIZE = 128;
27 |
28 |
29 | static {
30 | Security.addProvider(new BouncyCastleProvider());
31 | }
32 |
33 | public static byte[] generateKey() throws NoSuchAlgorithmException, NoSuchProviderException {
34 | return generateKey(DEFAULT_KEY, DEFAULT_KEY_SIZE);
35 | }
36 |
37 | public static byte[] generateKey(String seed) throws NoSuchAlgorithmException, NoSuchProviderException {
38 | return generateKey(seed, DEFAULT_KEY_SIZE);
39 | }
40 |
41 | public static byte[] generateKey(String seed, int keySize) throws NoSuchAlgorithmException, NoSuchProviderException {
42 | KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
43 | SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
44 | if (null != seed && !"".equals(seed)) {
45 | random.setSeed(seed.getBytes());
46 | }
47 | kg.init(keySize, random);
48 | return kg.generateKey().getEncoded();
49 | }
50 |
51 | /**
52 | * @description 加密
53 | */
54 | public static byte[] encrypt(String algorithmName, byte[] key, byte[] iv, byte[] data) throws Exception {
55 | return sm4core(algorithmName,Cipher.ENCRYPT_MODE, key, iv, data);
56 | }
57 |
58 | /**
59 | * @description 解密
60 | */
61 | public static byte[] decrypt(String algorithmName, byte[] key, byte[] iv, byte[] data) throws Exception {
62 | return sm4core(algorithmName, Cipher.DECRYPT_MODE, key, iv, data);
63 | }
64 |
65 | private static byte[] sm4core(String algorithmName, int type, byte[] key, byte[] iv, byte[] data) throws Exception {
66 | Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
67 | Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
68 | if (algorithmName.contains("/ECB/")) {
69 | cipher.init(type, sm4Key);
70 | } else {
71 | IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
72 | cipher.init(type, sm4Key, ivParameterSpec);
73 | }
74 |
75 | return cipher.doFinal(data);
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/sm/KeyUtils.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sm;
2 |
3 | import org.bouncycastle.jce.provider.BouncyCastleProvider;
4 |
5 | import java.security.*;
6 | import java.security.spec.ECGenParameterSpec;
7 | import java.security.spec.PKCS8EncodedKeySpec;
8 | import java.security.spec.X509EncodedKeySpec;
9 | import java.util.Base64;
10 |
11 | /**
12 | * @author lihongxu6
13 | * @version 1.0
14 | * @className KeyUtils
15 | * @description TODO
16 | * @date 2021-01-13 23:27
17 | */
18 | public class KeyUtils {
19 | /**
20 | * 生成国密公私钥对
21 | *
22 | * String[0]
公钥
23 | *
24 | * String[1]
私钥
25 | *
26 | * @return
27 | * @throws Exception
28 | */
29 | public static String[] generateSmKey() throws Exception {
30 | KeyPairGenerator keyPairGenerator = null;
31 | SecureRandom secureRandom = new SecureRandom();
32 | ECGenParameterSpec sm2Spec = new ECGenParameterSpec("sm2p256v1");
33 | keyPairGenerator = KeyPairGenerator.getInstance("EC", new BouncyCastleProvider());
34 | keyPairGenerator.initialize(sm2Spec);
35 | keyPairGenerator.initialize(sm2Spec, secureRandom);
36 | KeyPair keyPair = keyPairGenerator.generateKeyPair();
37 | PrivateKey privateKey = keyPair.getPrivate();
38 | PublicKey publicKey = keyPair.getPublic();
39 | String[] result = {
40 | new String(Base64.getEncoder().encode(publicKey.getEncoded()))
41 | , new String(Base64.getEncoder().encode(privateKey.getEncoded()))
42 | };
43 | return result;
44 | }
45 | /**
46 | * 将Base64转码的公钥串,转化为公钥对象
47 | *
48 | * @param publicKey
49 | * @return
50 | */
51 | public static PublicKey createPublicKey(String publicKey) {
52 | PublicKey publickey = null;
53 | try{
54 | X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
55 | KeyFactory keyFactory = KeyFactory.getInstance("EC", new BouncyCastleProvider());
56 | publickey = keyFactory.generatePublic(publicKeySpec);
57 | } catch (Exception e) {
58 | e.printStackTrace();
59 | }
60 | return publickey;
61 | }
62 |
63 | /**
64 | * 将Base64转码的私钥串,转化为私钥对象
65 | *
66 | * @param privateKey
67 | * @return
68 | */
69 | public static PrivateKey createPrivateKey(String privateKey) {
70 | PrivateKey publickey = null;
71 | try{
72 | PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey));
73 | KeyFactory keyFactory = KeyFactory.getInstance("EC", new BouncyCastleProvider());
74 | publickey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
75 | } catch (Exception e) {
76 | e.printStackTrace();
77 | }
78 | return publickey;
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/symmetric/des3DesAesBlowfishRC2RC4/BcSymmetric.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4;
2 |
3 | import org.bouncycastle.jce.provider.BouncyCastleProvider;
4 |
5 | import javax.crypto.KeyGenerator;
6 | import java.security.Security;
7 | import java.util.Map;
8 |
9 | public class BcSymmetric extends AbstractSymmetric {
10 |
11 | @Override
12 | public void decryptBefore() throws Exception {
13 | Security.addProvider(new BouncyCastleProvider());
14 | return;
15 | }
16 |
17 | @Override
18 | public void encryptBefore() throws Exception {
19 | Security.addProvider(new BouncyCastleProvider());
20 | return;
21 | }
22 |
23 | @Override
24 | public KeyGenerator initKeyBefore(Map.Entry algorithm) throws Exception {
25 | Security.addProvider(new BouncyCastleProvider());
26 | KeyGenerator kg = KeyGenerator.getInstance(algorithm.getKey(), "BC");
27 | return kg;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/symmetric/des3DesAesBlowfishRC2RC4/JdkSymmetric.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4;
2 |
3 | import javax.crypto.KeyGenerator;
4 | import java.util.Map;
5 |
6 |
7 | public class JdkSymmetric extends AbstractSymmetric {
8 |
9 | @Override
10 | public KeyGenerator initKeyBefore(Map.Entry algorithm) throws Exception {
11 | return KeyGenerator.getInstance(algorithm.getKey());
12 | }
13 |
14 | @Override
15 | public void encryptBefore() throws Exception {
16 | return;
17 | }
18 |
19 | @Override
20 | public void decryptBefore() throws Exception {
21 | return;
22 | }
23 |
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/symmetric/pbe/AbstractSymmetric.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.pbe;
2 |
3 | import javax.crypto.Cipher;
4 | import javax.crypto.SecretKeyFactory;
5 | import javax.crypto.spec.IvParameterSpec;
6 | import javax.crypto.spec.PBEKeySpec;
7 | import javax.crypto.spec.PBEParameterSpec;
8 | import java.lang.reflect.Constructor;
9 | import java.security.Key;
10 | import java.security.SecureRandom;
11 | import java.security.spec.AlgorithmParameterSpec;
12 |
13 | public abstract class AbstractSymmetric {
14 | public abstract void decryptBefore() throws Exception;
15 |
16 | private int keyObtentionIterations = 1000;
17 |
18 | /**
19 | * 解密
20 | *
21 | * @param data
22 | * @param key
23 | * @return
24 | * @throws Exception
25 | */
26 | public byte[] decrypt(String algorithm, Key key, byte[] salt, byte[] iv,byte[] data) throws Exception {
27 | this.decryptBefore();
28 | //解密
29 | PBEParameterSpec pbeParameterSpec = this.buildPBEParameterSpec(salt, iv);
30 | Cipher cipher = Cipher.getInstance(algorithm);
31 | cipher.init(Cipher.DECRYPT_MODE, key, pbeParameterSpec);
32 | return cipher.doFinal(data);
33 | }
34 |
35 | public abstract void encryptBefore() throws Exception;
36 |
37 | /**
38 | * 加密
39 | *
40 | * @param data
41 | * @param key
42 | * @return
43 | * @throws Exception
44 | */
45 | public byte[] encrypt(String algorithm, Key key, byte[] salt, byte[] iv, byte[] data) throws Exception {
46 | this.encryptBefore();
47 | //加密
48 | PBEParameterSpec pbeParameterSpec = this.buildPBEParameterSpec(salt, iv);
49 | Cipher cipher = Cipher.getInstance(algorithm);
50 | cipher.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpec);
51 | return cipher.doFinal(data);
52 | }
53 |
54 | public abstract SecretKeyFactory initKeyBefore(String algorithm) throws Exception;
55 |
56 | /**
57 | * 生成密钥
58 | *
59 | * @return
60 | * @throws Exception
61 | */
62 | public Key initKey(String algorithm, String password) throws Exception {
63 | SecretKeyFactory factory = this.initKeyBefore(algorithm);
64 | //口令与密钥
65 | PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
66 | return factory.generateSecret(pbeKeySpec);
67 | }
68 |
69 | public static byte[] initSalt() throws Exception {
70 | return initSalt(8);
71 | }
72 |
73 | public static byte[] initSalt(int length) throws Exception {
74 | //初始化盐
75 | SecureRandom random = new SecureRandom();
76 | return random.generateSeed(length);
77 | }
78 |
79 | private PBEParameterSpec buildPBEParameterSpec(byte[] salt, byte[] iv) {
80 | PBEParameterSpec parameterSpec;
81 | if (iv == null) {
82 | parameterSpec = new PBEParameterSpec(salt, keyObtentionIterations);
83 | } else {
84 | try {
85 | Class[] parameters = new Class[]{byte[].class, Integer.TYPE, AlgorithmParameterSpec.class};
86 | Constructor java8Constructor = PBEParameterSpec.class.getConstructor(parameters);
87 | Object[] parameterValues = new Object[]{salt, keyObtentionIterations, new IvParameterSpec(iv)};
88 | parameterSpec = (PBEParameterSpec) java8Constructor.newInstance(parameterValues);
89 | } catch (Exception var7) {
90 | parameterSpec = new PBEParameterSpec(salt, keyObtentionIterations);
91 | }
92 | }
93 | return parameterSpec;
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/symmetric/pbe/BcPbe.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.pbe;
2 |
3 | import org.bouncycastle.jce.provider.BouncyCastleProvider;
4 |
5 | import javax.crypto.SecretKeyFactory;
6 | import java.security.Key;
7 | import java.security.Security;
8 |
9 | public class BcPbe extends AbstractSymmetric {
10 | @Override
11 | public void decryptBefore() throws Exception {
12 | Security.addProvider(new BouncyCastleProvider());
13 |
14 | }
15 |
16 | @Override
17 | public void encryptBefore() throws Exception {
18 | Security.addProvider(new BouncyCastleProvider());
19 |
20 | }
21 |
22 | @Override
23 | public SecretKeyFactory initKeyBefore(String algorithm) throws Exception {
24 | Security.addProvider(new BouncyCastleProvider());
25 |
26 | SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm,"BC");
27 | return factory;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/symmetric/pbe/JdkPbe.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.pbe;
2 |
3 | import javax.crypto.KeyGenerator;
4 | import javax.crypto.SecretKeyFactory;
5 | import java.security.Key;
6 | import java.util.Map;
7 |
8 | public class JdkPbe extends AbstractSymmetric {
9 | @Override
10 | public void decryptBefore() throws Exception {
11 |
12 | }
13 |
14 | @Override
15 | public void encryptBefore() throws Exception {
16 |
17 | }
18 |
19 | @Override
20 | public SecretKeyFactory initKeyBefore(String algorithm) throws Exception {
21 | return SecretKeyFactory.getInstance(algorithm);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/CtorStatic.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security;
2 |
3 | /**
4 | * 静态代码块>构造代码块>构造函数>普通代码块
5 | */
6 | public class CtorStatic {
7 | static {
8 | System.out.println("静态代码块");
9 | }
10 |
11 | {
12 | System.out.println("构造代码块");
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/CtorStaticTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security;
2 |
3 | //
4 | public class CtorStaticTest {
5 | }
6 |
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/asymmetric001rsa/JdkRsaTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.asymmetric001rsa;
2 |
3 | import org.apache.commons.codec.binary.Hex;
4 | import org.junit.Test;
5 |
6 | import java.security.NoSuchAlgorithmException;
7 | import java.util.Base64;
8 | import java.util.Map;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | public class JdkRsaTest {
13 |
14 | @Test
15 | public void initKeyPair() throws Exception {
16 | String msg="cesfds";
17 | Map.Entry pair = JdkRsa.initKeyPair(JdkRsa.RSA, 512);
18 | System.out.println("pubKey:"+ Base64.getEncoder().encodeToString(pair.getKey()));
19 | System.out.println("priKey:"+ Base64.getEncoder().encodeToString(pair.getValue()));
20 | byte[] encrypt = JdkRsa.pubKeyEncrypt(JdkRsa.RSA, pair.getKey(), msg.getBytes());
21 | System.out.println("encrypt:"+ Base64.getEncoder().encodeToString(encrypt));
22 | byte[] bytes = JdkRsa.priKeyDecrypt(JdkRsa.RSA, pair.getValue(), encrypt);
23 | System.out.println("txt:"+ new String(bytes));
24 | }
25 |
26 |
27 | @Test
28 | public void initKeyPair2() throws Exception {
29 | String msg="cesfds";
30 | Map.Entry pair = JdkRsa.initKeyPair(JdkRsa.RSA, 1024);
31 | System.out.println("pubKey:"+ Base64.getEncoder().encodeToString(pair.getKey()));
32 | System.out.println("priKey:"+ Base64.getEncoder().encodeToString(pair.getValue()));
33 | byte[] encrypt = JdkRsa.pubKeyEncrypt(JdkRsa.RSA, pair.getKey(), msg.getBytes());
34 | System.out.println("encrypt:"+ Base64.getEncoder().encodeToString(encrypt));
35 | byte[] bytes = JdkRsa.priKeyDecrypt(JdkRsa.RSA, pair.getValue(), encrypt);
36 | System.out.println("txt:"+ new String(bytes));
37 | }
38 |
39 | @Test
40 | public void initKeyPair2048() throws Exception {
41 | String msg="cesfds";
42 | Map.Entry pair = JdkRsa.initKeyPair(JdkRsa.RSA, 2048);
43 | System.out.println("pubKey:"+ Base64.getEncoder().encodeToString(pair.getKey()));
44 | System.out.println("priKey:"+ Base64.getEncoder().encodeToString(pair.getValue()));
45 | byte[] encrypt = JdkRsa.pubKeyEncrypt(JdkRsa.RSA, pair.getKey(), msg.getBytes());
46 | System.out.println("encrypt:"+ Base64.getEncoder().encodeToString(encrypt));
47 | byte[] bytes = JdkRsa.priKeyDecrypt(JdkRsa.RSA, pair.getValue(), encrypt);
48 | System.out.println("txt:"+ new String(bytes));
49 | }
50 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/asymmetric002ecc/BcEccTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.asymmetric002ecc;
2 |
3 | import org.junit.Test;
4 |
5 | import javax.crypto.Cipher;
6 | import java.security.KeyPair;
7 | import java.util.Base64;
8 |
9 | import static org.junit.Assert.*;
10 |
11 | public class BcEccTest {
12 |
13 | @Test
14 | public void initKeyPair() throws Exception {
15 | String msg="我是测试数据";
16 | KeyPair keyPair = BcEcc.initKeyPair("", 256);
17 | System.out.println("pub:"+ Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded()));
18 | System.out.println("pri:"+ Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()));
19 |
20 | byte[] encrypt = BcEcc.encrypt(msg.getBytes(), keyPair.getPublic());
21 | System.out.println("encrypt DAta:"+ Base64.getEncoder().encodeToString(encrypt));
22 |
23 | byte[] decrypt = BcEcc.decrypt(encrypt, keyPair.getPrivate());
24 | System.out.println("txt DAta:"+ new String(decrypt));
25 | }
26 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/asymmetric002ecc/JdkEccTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.asymmetric002ecc;
2 |
3 | import org.junit.Test;
4 |
5 | import java.security.KeyPair;
6 | import java.util.Base64;
7 |
8 | import static org.junit.Assert.*;
9 |
10 | public class JdkEccTest {
11 | @Test
12 | public void initKeyPair() throws Exception {
13 | String msg="我是测试数据";
14 | KeyPair keyPair = JdkEcc.initKeyPair("", 256);
15 | System.out.println("pub:"+ Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded()));
16 | System.out.println("pri:"+ Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()));
17 | // com.sun.crypto.provider.AESCipher
18 | byte[] encrypt = JdkEcc.encrypt(msg.getBytes(), keyPair.getPublic());
19 | System.out.println("encrypt DAta:"+ Base64.getEncoder().encodeToString(encrypt));
20 | //
21 | byte[] decrypt = BcEcc.decrypt(encrypt, keyPair.getPrivate());
22 | System.out.println("txt DAta:"+ new String(decrypt));
23 | }
24 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base001base64/BouncyCastleBase64Test.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base001base64;
2 |
3 | import org.junit.Assert;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import static org.junit.Assert.*;
8 |
9 | public class BouncyCastleBase64Test {
10 |
11 | BouncyCastleBase64 bouncyCastleBase64;
12 |
13 | @Before
14 | public void setUp() throws Exception {
15 | bouncyCastleBase64 = new BouncyCastleBase64();
16 | }
17 |
18 | @Test
19 | public void base64Encoder() throws Exception {
20 | String msg = "aaaaa";
21 | String encoder = bouncyCastleBase64.base64Encoder(msg, "utf-8");
22 | String decoder = bouncyCastleBase64.base64Decoder(encoder, "utf-8");
23 | Assert.assertEquals(msg, decoder);
24 | }
25 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base001base64/CommonsCodecBase64Test.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base001base64;
2 |
3 | import org.junit.Assert;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import static org.junit.Assert.*;
8 |
9 | public class CommonsCodecBase64Test {
10 | CommonsCodecBase64 commonsCodecBase64;
11 |
12 | @Before
13 | public void setUp() throws Exception {
14 | commonsCodecBase64 = new CommonsCodecBase64();
15 | }
16 |
17 | @Test
18 | public void testBASE64Encoder() throws Exception {
19 | String encodeMsg = "aaa";
20 | String encoder = commonsCodecBase64.base64Encoder(encodeMsg, "utf-8");
21 | String decoder = commonsCodecBase64.base64Decoder(encoder, "utf-8");
22 | Assert.assertEquals(encodeMsg, decoder);
23 |
24 | }
25 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base001base64/Jdk8Base64Test.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base001base64;
2 |
3 | import org.junit.Assert;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import static org.junit.Assert.*;
8 |
9 | public class Jdk8Base64Test {
10 |
11 | Jdk8Base64 jdk8Base64;
12 | @Before
13 | public void setUp() throws Exception {
14 | jdk8Base64=new Jdk8Base64();
15 | }
16 |
17 | @Test
18 | public void base64Encoder() throws Exception {
19 | String encodeMsg="A";
20 | String encoder = jdk8Base64.base64Encoder(encodeMsg, "utf-8");
21 | String decoder = jdk8Base64.base64Decoder(encoder, "utf-8");
22 | Assert.assertEquals(encodeMsg,decoder);
23 | }
24 |
25 | @Test
26 | public void base64Decoder() {
27 | }
28 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base001base64/SunJdkBase64Test.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base001base64;
2 |
3 | import org.junit.Assert;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import static org.junit.Assert.*;
8 |
9 | public class SunJdkBase64Test {
10 |
11 | SunJdkBase64 sunJdkBase64;
12 |
13 | @Before
14 | public void setUp() throws Exception {
15 | sunJdkBase64 = new SunJdkBase64();
16 | }
17 |
18 | @Test
19 | public void base64Encoder() throws Exception {
20 | String encodeMsg = "测试";
21 | String encoder = sunJdkBase64.base64Encoder(encodeMsg, "utf-8");
22 | String decoder = sunJdkBase64.base64Decoder(encoder, "utf-8");
23 | Assert.assertEquals(encodeMsg,decoder);
24 | }
25 |
26 | @Test
27 | public void base64Decoder() {
28 | }
29 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base002base58/Base58Test.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base002base58;
2 |
3 | import org.junit.Assert;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import java.io.UnsupportedEncodingException;
8 |
9 | import static org.junit.Assert.*;
10 |
11 | public class Base58Test {
12 |
13 | Base58 base58;
14 |
15 | @Before
16 | public void setUp() throws Exception {
17 | base58 = new Base58();
18 | }
19 |
20 | @Test
21 | public void encode() throws Exception {
22 | String msg = "453534534534543534:dfsdfsdfsdfsdfsdfdsfdsfs";
23 | String encode = Base58.encode(msg.getBytes("UTF-8"), "UTF-8");
24 | System.out.println(encode);
25 |
26 | String dencode = new String(Base58.decode(encode), "UTF-8");
27 | System.out.println(dencode);
28 | Assert.assertEquals(msg, dencode);
29 | }
30 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base003md5AndSha/BcMD5AndShaTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base003md5AndSha;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | public class BcMD5AndShaTest {
8 |
9 | @Test
10 | public void msgSafeBase() throws Exception {
11 | System.out.println(BcMD5AndSha.msgSafeBase("测试",BcMD5AndSha.MD2));
12 | // System.out.println(BcMD5AndSha.msgSafeBase("测试",BcMD5AndSha.MD3)); //没有
13 | System.out.println(BcMD5AndSha.msgSafeBase("测试",BcMD5AndSha.MD4));
14 | System.out.println(BcMD5AndSha.msgSafeBase("测试",BcMD5AndSha.MD5));
15 | System.out.println(BcMD5AndSha.msgSafeBase("测试",BcMD5AndSha.SHA1));
16 | System.out.println(BcMD5AndSha.msgSafeBase("测试",BcMD5AndSha.SHA224));
17 | System.out.println(BcMD5AndSha.msgSafeBase("测试",BcMD5AndSha.SHA256));
18 | System.out.println(BcMD5AndSha.msgSafeBase("测试",BcMD5AndSha.SHA384));
19 | System.out.println(BcMD5AndSha.msgSafeBase("测试",BcMD5AndSha.SHA512));
20 | }
21 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base003md5AndSha/JdkMD5AndShaTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base003md5AndSha;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | public class JdkMD5AndShaTest {
8 |
9 | @Test
10 | public void msgSafeBase() throws Exception {
11 | System.out.println(JdkMD5AndSha.msgSafeBase("测试",JdkMD5AndSha.MD2));
12 | // System.out.println(MD5AndSha.msgSafeBase("测试",MD5AndSha.MD3));//没有
13 | // System.out.println(MD5AndSha.msgSafeBase("测试",MD5AndSha.MD4));//没有
14 | System.out.println(JdkMD5AndSha.msgSafeBase("测试",JdkMD5AndSha.MD5));
15 | System.out.println(JdkMD5AndSha.msgSafeBase("测试",JdkMD5AndSha.SHA1));
16 | System.out.println(JdkMD5AndSha.msgSafeBase("测试",JdkMD5AndSha.SHA224));
17 | System.out.println(JdkMD5AndSha.msgSafeBase("测试",JdkMD5AndSha.SHA256));
18 | System.out.println(JdkMD5AndSha.msgSafeBase("测试",JdkMD5AndSha.SHA384));
19 | System.out.println(JdkMD5AndSha.msgSafeBase("测试",JdkMD5AndSha.SHA512));
20 | }
21 |
22 | @Test
23 | public void hashCheck() throws Exception {
24 | System.out.println("AaAaAa".hashCode());//1952508096
25 | System.out.println("BBAaBB".hashCode());//1952508096
26 | }
27 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base004hmac/HmacMD5UtilsTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base004hmac;
2 |
3 | import org.junit.Test;
4 |
5 | public class HmacMD5UtilsTest {
6 |
7 | @Test
8 | public void initMacKey() {
9 | }
10 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base004hmac/HmacUtilsTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base004hmac;
2 |
3 | import org.junit.Test;
4 |
5 | public class HmacUtilsTest {
6 |
7 | @Test
8 | public void initMacKey() throws Exception {
9 | String msg = "测试数据";
10 | String key = HmacUtils.initMacKey(HmacUtils.HmacMD5);
11 | String hash = HmacUtils.hashMsgCode(HmacUtils.HmacMD5, key, msg.getBytes("utf-8"));
12 | System.out.println("key:"+msg+";hash:"+hash);
13 | boolean check = HmacUtils.check(HmacUtils.HmacMD5, key, hash, msg);
14 | System.out.println("check:"+check);
15 |
16 |
17 | key = HmacUtils.initMacKey(HmacUtils.HmacSHA1);
18 | hash = HmacUtils.hashMsgCode(HmacUtils.HmacSHA1, key, msg.getBytes("utf-8"));
19 | System.out.println("key:"+msg+";hash:"+hash);
20 | check = HmacUtils.check(HmacUtils.HmacSHA1, key, hash, msg);
21 | System.out.println("check:"+check);
22 |
23 | key = HmacUtils.initMacKey(HmacUtils.HmacSHA224);
24 | hash = HmacUtils.hashMsgCode(HmacUtils.HmacSHA224, key, msg.getBytes("utf-8"));
25 | System.out.println("key:"+msg+";hash:"+hash);
26 | check = HmacUtils.check(HmacUtils.HmacSHA224, key, hash, msg);
27 | System.out.println("check:"+check);
28 |
29 | key = HmacUtils.initMacKey(HmacUtils.HmacSHA256);
30 | hash = HmacUtils.hashMsgCode(HmacUtils.HmacSHA256, key, msg.getBytes("utf-8"));
31 | System.out.println("key:"+msg+";hash:"+hash);
32 | check = HmacUtils.check(HmacUtils.HmacSHA256, key, hash, msg);
33 | System.out.println("check:"+check);
34 |
35 |
36 | key = HmacUtils.initMacKey(HmacUtils.HmacSHA384);
37 | hash = HmacUtils.hashMsgCode(HmacUtils.HmacSHA384, key, msg.getBytes("utf-8"));
38 | System.out.println("key:"+msg+";hash:"+hash);
39 | check = HmacUtils.check(HmacUtils.HmacSHA384, key, hash, msg);
40 | System.out.println("check:"+check);
41 |
42 |
43 | key = HmacUtils.initMacKey(HmacUtils.HmacSHA512);
44 | hash = HmacUtils.hashMsgCode(HmacUtils.HmacSHA512, key, msg.getBytes("utf-8"));
45 | System.out.println("key:"+msg+";hash:"+hash);
46 | check = HmacUtils.check(HmacUtils.HmacSHA512, key, hash, msg);
47 | System.out.println("check:"+check);
48 | }
49 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base005ripemd/HmacRipeMDCoderTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base005ripemd;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | public class HmacRipeMDCoderTest {
8 |
9 | @Test
10 | public void encodeHmacRipeMDHex() throws Exception {
11 | String str = "RIPEMD消息摘要";
12 | System.out.println("原文:" + str);
13 | String key = HmacRipeMDCoder.initHmacRipeMDKey(HmacRipeMDCoder.HmacRipeMD128);
14 | String hex = HmacRipeMDCoder.encodeHmacRipeMDHex(HmacRipeMDCoder.HmacRipeMD128, key,str.getBytes());
15 | System.out.println("十六进制消息摘要算法值:" + hex);
16 |
17 |
18 | key = HmacRipeMDCoder.initHmacRipeMDKey(HmacRipeMDCoder.HmacRipeMD160);
19 | hex = HmacRipeMDCoder.encodeHmacRipeMDHex(HmacRipeMDCoder.HmacRipeMD160, key,str.getBytes());
20 | System.out.println("十六进制消息摘要算法值:" + hex);
21 | }
22 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base005ripemd/RipeMDCoderTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base005ripemd;
2 |
3 | import org.junit.Test;
4 |
5 | public class RipeMDCoderTest {
6 |
7 | @Test
8 | public void encodeRipeMD() throws Exception {
9 | String str = "RIPEMD消息摘要";
10 | System.out.println("原文:" + str);
11 | String data1hex = RipeMDCoder.encodeRipeMDHex(RipeMDCoder.RipeMD128, str.getBytes());
12 | System.out.println("十六进制消息摘要算法值:" + data1hex);
13 | data1hex = RipeMDCoder.encodeRipeMDHex(RipeMDCoder.RipeMD160, str.getBytes());
14 | System.out.println("十六进制消息摘要算法值:" + data1hex);
15 | data1hex = RipeMDCoder.encodeRipeMDHex(RipeMDCoder.RipeMD256, str.getBytes());
16 | System.out.println("十六进制消息摘要算法值:" + data1hex);
17 | data1hex = RipeMDCoder.encodeRipeMDHex(RipeMDCoder.RipeMD320, str.getBytes());
18 | System.out.println("十六进制消息摘要算法值:" + data1hex);
19 | }
20 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base006others/BcExtHashUtilTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base006others;
2 |
3 | import com.github.bjlhx15.security.base005ripemd.RipeMDCoder;
4 | import org.junit.Test;
5 |
6 | import static org.junit.Assert.*;
7 |
8 | public class BcExtHashUtilTest {
9 |
10 | @Test
11 | public void encodeExtHash() throws Exception {
12 | String str = "RIPEMD消息摘要";
13 | System.out.println("原文:" + str);
14 | String data1hex = "";
15 |
16 | data1hex = BcExtHashUtil.encodeExtHashHex(BcExtHashUtil.RipeMD128, str.getBytes());
17 | System.out.println("十六进制消息摘要算法值:" + data1hex);
18 | data1hex = BcExtHashUtil.encodeExtHashHex(BcExtHashUtil.RipeMD160, str.getBytes());
19 | System.out.println("十六进制消息摘要算法值:" + data1hex);
20 | data1hex = BcExtHashUtil.encodeExtHashHex(BcExtHashUtil.RipeMD256, str.getBytes());
21 | System.out.println("十六进制消息摘要算法值:" + data1hex);
22 | data1hex = BcExtHashUtil.encodeExtHashHex(BcExtHashUtil.RipeMD320, str.getBytes());
23 | System.out.println("十六进制消息摘要算法值:" + data1hex);
24 |
25 |
26 | data1hex = BcExtHashUtil.encodeExtHashHex(BcExtHashUtil.Tiger, str.getBytes());
27 | System.out.println("十六进制消息摘要算法值:" + data1hex);
28 | data1hex = BcExtHashUtil.encodeExtHashHex(BcExtHashUtil.Whirlpool, str.getBytes());
29 | System.out.println("十六进制消息摘要算法值:" + data1hex);
30 | data1hex = BcExtHashUtil.encodeExtHashHex(BcExtHashUtil.Gost3411, str.getBytes());
31 | System.out.println("十六进制消息摘要算法值:" + data1hex);
32 | }
33 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/base007crc/CrcUtilTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.base007crc;
2 |
3 | import org.junit.Test;
4 |
5 | import java.nio.ByteBuffer;
6 | import java.util.ArrayList;
7 | import java.util.List;
8 | import java.util.zip.CRC32;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | public class CrcUtilTest {
13 |
14 | @Test
15 | public void crc4_itu() {
16 | List ip = getIP();
17 | for (int i = 0; i < ip.size(); i++) {
18 | if (i > 50) {
19 | return;
20 | }
21 | String s = ip.get(i);
22 |
23 | int b = CrcUtil.crc32(s.getBytes(), 0, s.length());
24 | System.out.println(b);
25 |
26 | }
27 |
28 | }
29 |
30 | public static Long ip2int(String ip) {
31 | Long num = 0L;
32 | if (ip == null) {
33 | return num;
34 | }
35 |
36 | try {
37 | ip = ip.replaceAll("[^0-9\\.]", ""); //去除字符串前的空字符
38 | String[] ips = ip.split("\\.");
39 | if (ips.length == 4) {
40 | num = Long.parseLong(ips[0], 10) * 256L * 256L * 256L + Long.parseLong(ips[1], 10) * 256L * 256L + Long.parseLong(ips[2], 10) * 256L + Long.parseLong(ips[3], 10);
41 | num = num >>> 0;
42 | }
43 | } catch (NullPointerException ex) {
44 | System.out.println(ip);
45 | }
46 |
47 | return num;
48 | }
49 |
50 | @Test
51 | public void testIP() {
52 | System.out.println(ip2int("255.255.255.255"));
53 | System.out.println(ip2int("255.255.255.254"));
54 | }
55 |
56 |
57 | @Test
58 | public void crc32() {
59 | // byte[] b = new byte[100];//用于验证的数据
60 | Long ip2int = ip2int("255.255.255.255");
61 | byte[] b = ByteBuffer.allocate(8).putLong(ip2int).array();
62 | // byte[] b="255.255.255.255".getBytes();
63 | CRC32 c = new CRC32();
64 | c.reset();//Resets CRC-32 to initial value.
65 | c.update(b, 0, b.length);//将数据丢入CRC32解码器
66 | long value = (long) c.getValue();//获取CRC32 的值 默认返回值类型为long 用于保证返回值是一个正数
67 | System.out.println(value);//2422070025
68 | System.out.println("255.255.255.255".hashCode());
69 | System.out.println(ip2int.hashCode());
70 | }
71 |
72 | List getIP() {
73 | List result = new ArrayList<>();
74 | for (int i = 1; i < 255; i++) {
75 | for (int j = 0; j < 255; j++) {
76 | for (int k = 0; k < 255; k++) {
77 | for (int l = 0; l < 255; l++) {
78 | if(result.size()>100){
79 | return result;
80 | }
81 | result.add(i + "." + j + "." + k + "." + l);
82 | }
83 | }
84 | }
85 | }
86 | return result;
87 | }
88 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/changekey001DH/BcDHCoderTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.changekey001DH;
2 |
3 | import org.junit.Before;
4 | import org.junit.Test;
5 |
6 | import java.util.Base64;
7 | import java.util.Map;
8 |
9 | public class BcDHCoderTest {
10 |
11 | AbstractDHCoder abstractDHCoder=null;
12 | @Before
13 | public void setUp(){
14 | abstractDHCoder=new BcDHCoder();
15 | }
16 |
17 | @Test
18 | public void initKey() throws Exception {
19 | String msg="我是密文";
20 | // 甲方构建密钥对
21 | Map.Entry entry = AbstractDHCoder.initKey();
22 | System.out.println("甲方 pubkey:"+ Base64.getEncoder().encodeToString(entry.getKey()));
23 | System.out.println("甲方 prikey:"+ Base64.getEncoder().encodeToString(entry.getValue()));
24 |
25 | System.out.println("甲方将 公钥 发送给 乙方");
26 | System.out.println("乙方 使用甲方pubkey构建密钥对");
27 | Map.Entry entryYi = AbstractDHCoder.initKeyByPubKey(entry.getKey());
28 | System.out.println("乙方 pubkey:"+ Base64.getEncoder().encodeToString(entryYi.getKey()));
29 | System.out.println("乙方 prikey:"+ Base64.getEncoder().encodeToString(entryYi.getValue()));
30 |
31 | System.out.println("乙方 pubkey 发送给 甲方");
32 |
33 | System.out.println("甲方加密数据,使用自己私钥以及 对方公钥");
34 | byte[] encrypt = abstractDHCoder.encrypt(AbstractDHCoder.DESede,msg.getBytes(), entryYi.getKey(), entry.getValue());
35 | System.out.println("甲方将加密数据,发送给乙方");
36 | System.out.println("乙方 使用自己私钥以及 对方公钥 解密");
37 | byte[] decrypt = abstractDHCoder.decrypt(AbstractDHCoder.DESede,encrypt, entry.getKey(), entryYi.getValue());
38 | System.out.println(new String(decrypt));
39 |
40 | // System.out.println("乙方回发消息");
41 | byte[] encrypt2 = abstractDHCoder.encrypt(AbstractDHCoder.DESede,"乙方回发消息".getBytes(), entry.getKey(), entryYi.getValue());
42 |
43 | byte[] decrypt2 = abstractDHCoder.decrypt(AbstractDHCoder.DESede,encrypt2, entryYi.getKey(), entry.getValue());
44 | System.out.println(new String(decrypt2));
45 | }
46 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/changekey001DH/JdkDHCoderTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.changekey001DH;
2 |
3 | import org.junit.Before;
4 | import org.junit.Test;
5 |
6 | import java.util.Base64;
7 | import java.util.Map;
8 |
9 | import static org.junit.Assert.*;
10 |
11 | public class JdkDHCoderTest {
12 |
13 | AbstractDHCoder abstractDHCoder=null;
14 | @Before
15 | public void setUp(){
16 | abstractDHCoder=new JdkDHCoder();
17 | }
18 |
19 | @Test
20 | public void initKey() throws Exception {
21 | String msg="我是密文";
22 | // 甲方构建密钥对
23 | Map.Entry entry = AbstractDHCoder.initKey();
24 | System.out.println("甲方 pubkey:"+ Base64.getEncoder().encodeToString(entry.getKey()));
25 | System.out.println("甲方 prikey:"+ Base64.getEncoder().encodeToString(entry.getValue()));
26 |
27 | System.out.println("甲方将 公钥 发送给 乙方");
28 | System.out.println("乙方 使用甲方pubkey构建密钥对");
29 | Map.Entry entryYi = AbstractDHCoder.initKeyByPubKey(entry.getKey());
30 | System.out.println("乙方 pubkey:"+ Base64.getEncoder().encodeToString(entryYi.getKey()));
31 | System.out.println("乙方 prikey:"+ Base64.getEncoder().encodeToString(entryYi.getValue()));
32 |
33 | System.out.println("乙方 pubkey 发送给 甲方");
34 |
35 | System.out.println("甲方加密数据,使用自己私钥以及 对方公钥");
36 | byte[] encrypt = abstractDHCoder.encrypt(AbstractDHCoder.DESede,msg.getBytes(), entryYi.getKey(), entry.getValue());
37 | System.out.println("甲方将加密数据,发送给乙方");
38 | System.out.println("乙方 使用自己私钥以及 对方公钥 解密");
39 | byte[] decrypt = abstractDHCoder.decrypt(AbstractDHCoder.DESede,encrypt, entry.getKey(), entryYi.getValue());
40 | System.out.println(new String(decrypt));
41 |
42 | // System.out.println("乙方回发消息");
43 | byte[] encrypt2 = abstractDHCoder.encrypt(AbstractDHCoder.DESede,"乙方回发消息".getBytes(), entry.getKey(), entryYi.getValue());
44 |
45 | byte[] decrypt2 = abstractDHCoder.decrypt(AbstractDHCoder.DESede,encrypt2, entryYi.getKey(), entry.getValue());
46 | System.out.println(new String(decrypt2));
47 | }
48 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/changekey002ECDH/BcECDHCoderTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.changekey002ECDH;
2 |
3 | import org.junit.Test;
4 |
5 | import javax.crypto.Cipher;
6 | import java.security.KeyPair;
7 | import java.util.ArrayList;
8 | import java.util.Base64;
9 | import java.util.List;
10 |
11 | public class BcECDHCoderTest {
12 |
13 | @Test
14 | public void initKey() throws Exception {
15 |
16 |
17 | // Cipher.ARIARFC3211WRAP -> org.bouncycastle.jcajce.provider.symmetric.ARIA $RFC3211Wrap
18 | String msg = "我是密文";
19 | List list = new ArrayList<>();
20 | // list.add("EC");
21 | list.add("ECDH");
22 | list.add("ECDHC");
23 | for (String algo : list) {
24 |
25 | // 甲方构建密钥对
26 | KeyPair keyPair = BcECDHCoder.initKey(algo,BcECDHCoder.secp256k1);
27 | System.out.println("甲方 pubkey:" + Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded()));
28 | System.out.println("甲方 prikey:" + Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()));
29 |
30 | System.out.println("甲方将 公钥 发送给 乙方");
31 | System.out.println("乙方 使用甲方pubkey构建密钥对");
32 | KeyPair keyPair2 = BcECDHCoder.initKeyByPubKey(algo, keyPair.getPublic());
33 |
34 | System.out.println("乙方 pubkey:" + Base64.getEncoder().encodeToString(keyPair2.getPublic().getEncoded()));
35 | System.out.println("乙方 prikey:" + Base64.getEncoder().encodeToString(keyPair2.getPrivate().getEncoded()));
36 |
37 | System.out.println("乙方 pubkey 发送给 甲方");
38 | System.out.println("甲方加密数据,使用自己私钥以及 对方公钥");
39 | byte[] encrypt = BcECDHCoder.encrypt(algo,BcECDHCoder.AES, msg.getBytes(), keyPair2.getPublic(), keyPair.getPrivate());
40 | System.out.println("甲方将加密数据,发送给乙方");
41 | System.out.println("乙方 使用自己私钥以及 对方公钥 解密");
42 | byte[] decrypt = BcECDHCoder.decrypt(algo,BcECDHCoder.AES, encrypt,keyPair.getPublic(), keyPair2.getPrivate());
43 | System.out.println(new String(decrypt));
44 |
45 | // System.out.println("乙方回发消息");
46 | byte[] encrypt2 = BcECDHCoder.encrypt(algo,BcECDHCoder.AES, "乙方回发消息".getBytes(), keyPair.getPublic(),keyPair2.getPrivate());
47 |
48 | byte[] decrypt2 = BcECDHCoder.decrypt(algo,BcECDHCoder.AES, encrypt2, keyPair2.getPublic(), keyPair.getPrivate());
49 | System.out.println(new String(decrypt2));
50 |
51 | }
52 | }
53 |
54 |
55 | @Test
56 | public void initKey2() throws Exception {
57 |
58 | String msg = "我是密文";
59 | List list = new ArrayList<>();
60 | list.add("ECDH");
61 | for (String algo : list) {
62 |
63 | // 甲方构建密钥对
64 | KeyPair keyPair = BcECDHCoder.initKey(algo,BcECDHCoder.secp256k1);
65 | System.out.println("甲方 pubkey:" + Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded()));
66 | System.out.println("甲方 prikey:" + Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()));
67 |
68 | System.out.println("甲方将 公钥 发送给 乙方");
69 | System.out.println("乙方 使用甲方pubkey构建密钥对");
70 | KeyPair keyPair2 = BcECDHCoder.initKeyByPubKey(algo, keyPair.getPublic());
71 |
72 | System.out.println("乙方 pubkey:" + Base64.getEncoder().encodeToString(keyPair2.getPublic().getEncoded()));
73 | System.out.println("乙方 prikey:" + Base64.getEncoder().encodeToString(keyPair2.getPrivate().getEncoded()));
74 |
75 | System.out.println("乙方 pubkey 发送给 甲方");
76 | System.out.println("甲方加密数据,使用自己私钥以及 对方公钥");
77 | byte[] encrypt = BcECDHCoder.encrypt(algo,BcECDHCoder.DESede, msg.getBytes(), keyPair2.getPublic(), keyPair.getPrivate());
78 | System.out.println("甲方将加密数据,发送给乙方");
79 | System.out.println("乙方 使用自己私钥以及 对方公钥 解密");
80 | byte[] decrypt = BcECDHCoder.decrypt(algo,BcECDHCoder.DESede, encrypt,keyPair.getPublic(), keyPair2.getPrivate());
81 | System.out.println(new String(decrypt));
82 |
83 | // System.out.println("乙方回发消息");
84 | byte[] encrypt2 = BcECDHCoder.encrypt(algo,BcECDHCoder.DESede, "乙方回发消息".getBytes(), keyPair.getPublic(),keyPair2.getPrivate());
85 |
86 | byte[] decrypt2 = BcECDHCoder.decrypt(algo,BcECDHCoder.DESede, encrypt2, keyPair2.getPublic(), keyPair.getPrivate());
87 | System.out.println(new String(decrypt2));
88 |
89 | }
90 | }
91 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/changekey003DigitalEnvelopeDH/DigitalEnvelopeTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.changekey003DigitalEnvelopeDH;
2 |
3 | import org.junit.Test;
4 |
5 | import java.security.KeyPair;
6 |
7 | import static org.junit.Assert.*;
8 |
9 | public class DigitalEnvelopeTest {
10 |
11 | @Test
12 | public void generatorKeyPair() {
13 | KeyPair pair = DigitalEnvelope.generatorKeyPair();
14 | Thread client = new DigitalEnvelope.Client(pair);
15 | Thread server = new DigitalEnvelope.Server(pair.getPublic());
16 | server.start();
17 | client.start();
18 |
19 | }
20 |
21 | public static void main(String[] args) {
22 | KeyPair pair = DigitalEnvelope.generatorKeyPair();
23 | Thread client = new DigitalEnvelope.Client(pair);
24 | Thread server = new DigitalEnvelope.Server(pair.getPublic());
25 | server.start();
26 | client.start();
27 | }
28 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/encryptSign001BcEcc/BcEccAlgorithmDHUtilTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.encryptSign001BcEcc;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.AbstractMap;
6 | import java.util.Map;
7 |
8 | public class BcEccAlgorithmDHUtilTest {
9 |
10 | @Test
11 | public void initKeyByPubKey() throws Exception {
12 | String msg = "我是测试数据对的纷纷";
13 | System.out.println("1、A 初始化密钥对=================");
14 | Map.Entry entryA = new AbstractMap.SimpleEntry<>("MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEklGeocxDXXpJRKKHSILpbvMkZs/HbJApy5s7mJdmJPfOhMzqfOM4DVJmdW+aXOS80PR+5NQ7tExkwTSVGpj4Gg=="
15 | ,"MIGNAgEAMBAGByqGSM49AgEGBSuBBAAKBHYwdAIBAQQg3NhqyL1GPnvjDIj5l9OMDYCXB+25NTSjfgv2dWriXwagBwYFK4EEAAqhRANCAASSUZ6hzENdeklEoodIgulu8yRmz8dskCnLmzuYl2Yk986EzOp84zgNUmZ1b5pc5LzQ9H7k1Du0TGTBNJUamPga");
16 | //BcEccAlgorithmDHUtil.initKeyPairBase64();
17 | System.out.println("A pubKey:" + entryA.getKey());
18 | System.out.println("A priKey:" + entryA.getValue());
19 | System.out.println();
20 |
21 | System.out.println("2、A 将 公钥 发送给B");
22 |
23 | System.out.println("B 用A的公钥生成密钥对,将公钥发给A");
24 | Map.Entry entryB = BcEccAlgorithmDHUtil.initKeyByPubKey(entryA.getKey());
25 | System.out.println("B pubKey:" + entryB.getKey());
26 | System.out.println("B priKey:" + entryB.getValue());
27 | System.out.println("=================================");
28 |
29 | System.out.println("A 向 B 发送数据【密文、签名】");
30 | System.out.println("A 需要用B的 公钥加密数据,以及自己的私钥 加密数据");
31 | String bytes = BcEccAlgorithmDHUtil.encryptBase64Utf8(entryB.getKey(),entryA.getValue(),msg);
32 | System.out.println("密文:" + bytes);
33 |
34 | System.out.println("A 需要用自己的 私钥签名");
35 | String sign = BcEccAlgorithmDHUtil.sign(entryA.getValue(), msg);
36 | System.out.println("sign:" + sign);
37 |
38 | System.out.println("A 向 B 发送数据:ok");
39 | System.out.println("======================");
40 |
41 |
42 |
43 | System.out.println("B用 需要用自己 的私钥解密,以及A的公钥解密");
44 | String de = BcEccAlgorithmDHUtil.decryptBase64Utf8(entryA.getKey(),entryB.getValue(), bytes);
45 | System.out.println("解密后:" + de);
46 |
47 | System.out.println("B需要用A 的公钥验签");
48 | boolean check = BcEccAlgorithmDHUtil.verify(entryA.getKey(), de, sign);
49 | System.out.println("check:" + check);
50 | }
51 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/sign001rsa/JdkRsaSignTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sign001rsa;
2 |
3 | import com.github.bjlhx15.security.asymmetric001rsa.JdkRsa;
4 | import org.junit.Test;
5 | import sun.security.rsa.SunRsaSign;
6 |
7 | import java.util.ArrayList;
8 | import java.util.Base64;
9 | import java.util.List;
10 | import java.util.Map;
11 |
12 | import static org.junit.Assert.*;
13 |
14 | public class JdkRsaSignTest {
15 |
16 | // SunRsaSign: Signature.SHA256withRSA -> sun.security.rsa.RSASignature$SHA256withRSA
17 | // aliases: [1.2.840.113549.1.1.11, OID.1.2.840.113549.1.1.11]
18 | // attributes: {SupportedKeyClasses=java.security.interfaces.RSAPublicKey|java.security.interfaces.RSAPrivateKey}
19 | @Test
20 | public void initKeyPair() throws Exception {
21 | List list = new ArrayList<>();
22 | list.add(JdkRsaSign.MD2withRSA);
23 | list.add(JdkRsaSign.MD5withRSA);
24 | list.add(JdkRsaSign.SHA1withRSA);
25 | list.add(JdkRsaSign.SHA224withRSA);
26 | list.add(JdkRsaSign.SHA256withRSA);
27 | list.add(JdkRsaSign.SHA384withRSA);
28 | list.add(JdkRsaSign.SHA512withRSA);
29 | String msg = "cesfds";
30 |
31 | for (String signAlgo : list) {
32 | Map.Entry pair = JdkRsaSign.initKeyPair(JdkRsaSign.RSA, 1024);
33 | System.out.println("pubKey:" + Base64.getEncoder().encodeToString(pair.getKey()));
34 | System.out.println("priKey:" + Base64.getEncoder().encodeToString(pair.getValue()));
35 | byte[] encrypt = JdkRsaSign.prikeySign(JdkRsaSign.RSA, signAlgo, pair.getValue(), msg.getBytes());
36 | System.out.println("encrypt:" + Base64.getEncoder().encodeToString(encrypt));
37 | boolean check = JdkRsaSign.pubKeyCheckSign(JdkRsaSign.RSA, signAlgo, pair.getKey(), msg.getBytes(), encrypt);
38 | System.out.println("txt:" + check);
39 | }
40 | }
41 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/sign002dsa/JdkDsaTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sign002dsa;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.ArrayList;
6 | import java.util.Base64;
7 | import java.util.List;
8 | import java.util.Map;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | public class JdkDsaTest {
13 |
14 | @Test
15 | public void sign() throws Exception {
16 | String msg = "密文";
17 | Map.Entry key = JdkDsa.initKey();
18 | System.out.println("pub:" + Base64.getEncoder().encodeToString(key.getKey()));
19 | System.out.println("pri:" + Base64.getEncoder().encodeToString(key.getValue()));
20 | // sun.security.provider.DSA
21 | List list = new ArrayList<>();
22 | list.add(JdkDsa.DSA);//==SHA1withDSA
23 | list.add(JdkDsa.SHA1withDSA);
24 | list.add(JdkDsa.SHA224withDSA);
25 | list.add(JdkDsa.SHA256withDSA);
26 |
27 | for (String algo : list) {
28 | byte[] dsas = JdkDsa.sign(algo, key.getValue(), msg.getBytes());
29 | System.out.println("sign:" + Base64.getEncoder().encodeToString(dsas));
30 |
31 | boolean dsa = JdkDsa.verify(algo, key.getKey(), msg.getBytes(), dsas);
32 | System.out.println("check sign:" + dsa);
33 |
34 | }
35 |
36 | }
37 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/sign003ecc/JdkEcdsaTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sign003ecc;
2 |
3 | import com.github.bjlhx15.security.asymmetric002ecc.BcEcc;
4 | import com.github.bjlhx15.security.asymmetric002ecc.JdkEcc;
5 | import org.junit.Test;
6 |
7 | import java.security.KeyPair;
8 | import java.util.ArrayList;
9 | import java.util.Base64;
10 | import java.util.List;
11 |
12 | import static org.junit.Assert.*;
13 |
14 | public class JdkEcdsaTest {
15 |
16 | @Test
17 | public void initKey() throws Exception {
18 | String msg="我是测试数据";
19 | KeyPair keyPair = JdkEcdsa.initKey( 256);
20 | System.out.println("pub:"+ Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded()));
21 | System.out.println("pri:"+ Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()));
22 | List list= new ArrayList<>();
23 | // sun.security.ec.ECDSASignature
24 | list.add(JdkEcdsa.NONEwithECDSA);
25 | // list.add(JdkEcdsa.RIPEMD160withECDSA);
26 | list.add(JdkEcdsa.SHA1withECDSA);
27 | list.add(JdkEcdsa.SHA224withECDSA);
28 | list.add(JdkEcdsa.SHA256withECDSA);
29 | list.add(JdkEcdsa.SHA384withECDSA);
30 | list.add(JdkEcdsa.SHA512withECDSA);
31 | for (String algo : list) {
32 | byte[] sign = JdkEcdsa.sign(algo,keyPair.getPrivate(),msg.getBytes());
33 | System.out.println("sign:"+ Base64.getEncoder().encodeToString(sign));
34 |
35 | boolean checkb= JdkEcdsa.verify(algo,keyPair.getPublic(),msg.getBytes(),sign);
36 | System.out.println("checkb:"+ checkb);
37 | }
38 |
39 |
40 | }
41 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/sm/BcSm2UtilTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sm;
2 |
3 | import org.apache.commons.codec.binary.Hex;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import java.security.PrivateKey;
8 | import java.security.PublicKey;
9 | import java.util.Base64;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * @author lihongxu6
15 | * @version 1.0
16 | * @className BcSm2UtilTest
17 | * @description TODO
18 | * @date 2021-01-13 22:34
19 | */
20 | public class BcSm2UtilTest {
21 | private String test = "woshi测试数据。。..";
22 |
23 | java.security.PublicKey publicKey = null;
24 | java.security.PrivateKey privateKey = null;
25 |
26 | @Before
27 | public void setup() throws Exception {//生成公私钥对
28 | String[] keys = KeyUtils.generateSmKey();
29 |
30 | System.out.println("原始数据:" + test);
31 | System.out.println("公钥:" + new String(keys[0]));
32 | System.out.println();
33 | publicKey = KeyUtils.createPublicKey(keys[0]);
34 |
35 | System.out.println("私钥:" + new String(keys[1]));
36 | System.out.println();
37 | privateKey = KeyUtils.createPrivateKey(keys[1]);
38 | }
39 |
40 | @Test
41 | public void encrypt() throws Exception {
42 | byte[] encrypt = BcSm2Util.encrypt(test.getBytes(), publicKey);
43 | String encryptBase64Str = Base64.getEncoder().encodeToString(encrypt);
44 | System.out.println("加密数据:" + encryptBase64Str);
45 |
46 | byte[] decrypt = BcSm2Util.decrypt(encrypt, privateKey);
47 |
48 | System.out.println("解密数据:"+new String(decrypt));
49 |
50 | byte[] sign = BcSm2Util.signByPrivateKey(test.getBytes(), privateKey);
51 | System.out.println("数据签名:"+ Base64.getEncoder().encodeToString(sign));
52 |
53 | boolean b = BcSm2Util.verifyByPublicKey(test.getBytes(), publicKey,sign);
54 | System.out.println("数据验签:"+ b);
55 | }
56 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/sm/BcSm3UtilTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sm;
2 |
3 | import org.junit.Assert;
4 | import org.junit.Test;
5 |
6 | import static org.junit.Assert.*;
7 |
8 | /**
9 | * @author lihongxu6
10 | * @version 1.0
11 | * @className BcSm3UtilTest
12 | * @description TODO
13 | * @date 2021-01-13 23:17
14 | */
15 | public class BcSm3UtilTest {
16 | private String test="woshi测试数据。。..";
17 |
18 | @Test
19 | public void sm3() throws Exception {
20 | String s = BcSm3Util.sm3Hex(test.getBytes());
21 | System.out.println(s);
22 | String s2 = BcSm3Util.sm3bcHex(test.getBytes());
23 | System.out.println(s2);
24 | Assert.assertEquals(s,s2);
25 | }
26 |
27 | @Test
28 | public void hmacSm3Hex() {
29 | String s = BcSm3Util.hmacSm3Hex("AAAA".getBytes(),test.getBytes());
30 | System.out.println(s);
31 | }
32 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/sm/BcSm4UtilTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.sm;
2 |
3 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.AbstractSymmetric;
4 | import org.apache.commons.codec.binary.Hex;
5 | import org.apache.commons.lang3.RandomStringUtils;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 |
9 | import javax.crypto.IllegalBlockSizeException;
10 | import java.security.NoSuchAlgorithmException;
11 | import java.security.NoSuchProviderException;
12 | import java.util.ArrayList;
13 | import java.util.Base64;
14 | import java.util.List;
15 | import java.util.Random;
16 | import java.util.concurrent.TimeUnit;
17 |
18 | import static org.junit.Assert.*;
19 |
20 | /**
21 | * @author lihongxu6
22 | * @version 1.0
23 | * @className BcSm4UtilTest
24 | * @description TODO
25 | * @date 2021-01-13 23:47
26 | */
27 | public class BcSm4UtilTest {
28 | byte[] key = BcSm4Util.generateKey();
29 | byte[] iv = null;
30 |
31 | String text = "我是加密数据,请测试。。8888";
32 |
33 | public BcSm4UtilTest() throws NoSuchProviderException, NoSuchAlgorithmException {
34 | }
35 |
36 | @Test
37 | public void bcSm4UtilTest() throws Exception {
38 | List algorithm = new ArrayList<>();
39 | algorithm.add(("SM4/ECB/NOPADDING"));
40 | algorithm.add(("SM4/ECB/PKCS5PADDING"));
41 | algorithm.add(("SM4/ECB/ISO10126PADDING"));
42 | algorithm.add(("SM4/CBC/NOPADDING"));
43 | algorithm.add(("SM4/CBC/PKCS5PADDING"));
44 | algorithm.add(("SM4/CBC/ISO10126PADDING"));
45 | algorithm.add(("SM4/PCBC/NOPADDING"));
46 | algorithm.add(("SM4/PCBC/PKCS5PADDING"));
47 | algorithm.add(("SM4/PCBC/ISO10126PADDING"));
48 | algorithm.add(("SM4/CTR/NOPADDING"));
49 | algorithm.add(("SM4/CTR/PKCS5PADDING"));
50 | algorithm.add(("SM4/CTR/ISO10126PADDING"));
51 | algorithm.add(("SM4/CTS/NOPADDING"));
52 | algorithm.add(("SM4/CTS/PKCS5PADDING"));
53 | algorithm.add(("SM4/CTS/ISO10126PADDING"));
54 | if (iv == null)
55 | iv = AbstractSymmetric.initIv(16);
56 |
57 | for (String s : algorithm) {
58 | //SM4加密
59 | try {
60 | System.out.println("SM4加密算法: " + s);
61 | System.out.println("SM4加密原始数据: " + text);
62 | System.out.println("SM4加密key: " + Base64.getEncoder().encodeToString(key));
63 | System.out.println("SM4加密iv: " + Base64.getEncoder().encodeToString(iv));
64 |
65 | byte[] encrypt = BcSm4Util.encrypt(s, key, iv, text.getBytes());
66 | System.out.println("SM4加密数据密文: " + Base64.getEncoder().encodeToString(encrypt));
67 |
68 | //SM4解密
69 | byte[] decrypt = BcSm4Util.decrypt(s, key, iv, encrypt);
70 | System.out.println("SM4解密数据: " + new String(decrypt));
71 | } catch (Exception e) {
72 | if (e instanceof IllegalBlockSizeException) {
73 | System.err.println("SM4解密数据:算法 " + s + "数据需自己手工对齐");
74 | } else {
75 | System.err.println("SM4解密数据:算法 " + s +"::"+ e.getMessage());
76 | }
77 | } finally {
78 | System.err.println("---------------------------------------");
79 | TimeUnit.SECONDS.sleep(1);
80 | }
81 | }
82 | }
83 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/symmetric/des3DesAesBlowfishRC2RC4/BcSymmetricDesTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4;
2 |
3 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.AbstractSymmetric;
4 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.BcSymmetric;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import java.util.*;
9 |
10 | public class BcSymmetricDesTest {
11 |
12 | String msg = "测试数据2222";
13 | Map.Entry padding = null;
14 | byte[] key = null;
15 | byte[] iv = null;
16 | byte[] encrypt = null;
17 | byte[] decrypt = null;
18 | AbstractSymmetric symmetric=null;
19 | @Before
20 | public void before(){
21 | symmetric=new BcSymmetric();
22 | }
23 |
24 | // SunJCE: Cipher.DES -> com.sun.crypto.provider.DESCipher
25 | // attributes: {SupportedPaddings=NOPADDING|PKCS5PADDING|ISO10126PADDING,
26 | // SupportedKeyFormats=RAW, SupportedModes=ECB|CBC|PCBC|CTR|CTS|CFB|OFB|CFB8|CFB16|CFB24|CFB32|CFB40|CFB48|CFB56|CFB64
27 | // |OFB8|OFB16|OFB24|OFB32|OFB40|OFB48|OFB56|OFB64}
28 |
29 | @Test
30 | public void encryptAll() throws Exception {
31 | System.out.println("原文:" + msg);
32 | List> paddingListMap = new ArrayList<>();
33 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES"));//相当于:DES/ECB/PKCS5PADDING
34 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/ECB/NOPADDING"));
35 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/ECB/PKCS5PADDING"));
36 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/ECB/ISO10126PADDING"));
37 |
38 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CBC/NOPADDING"));
39 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CBC/PKCS5PADDING"));
40 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CBC/ISO10126PADDING"));
41 |
42 |
43 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/PCBC/NOPADDING"));
44 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/PCBC/PKCS5PADDING"));
45 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/PCBC/ISO10126PADDING"));
46 |
47 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CTR/NOPADDING"));
48 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CTR/PKCS5PADDING"));
49 | //paddingListMap.add(new AbstractMap.SimpleEntry<>("DES","DES/CTR/ISO10126PADDING"));//不支持
50 |
51 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CTS/NOPADDING"));
52 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CTS/PKCS5PADDING"));
53 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("DES","DES/CTS/ISO10126PADDING"));//不支持
54 | for (Map.Entry entry : paddingListMap) {
55 | try {
56 | boolean b = encryptCheck(entry);
57 | if (b) {
58 | System.out.println(entry.getValue() + ":支持");
59 | }
60 | } catch (Exception e) {
61 | e.printStackTrace();
62 | System.out.println(entry.getValue() + ":不支持");
63 | }
64 | }
65 | }
66 |
67 | boolean encryptCheck(Map.Entry entry) throws Exception {
68 | padding = entry;
69 | //Wrong keysize: DES key must be 64 bits long.或者不写也可以 或者 49-64 之间
70 | key = symmetric.initKey(padding,64);//Base64.getDecoder().decode("koY9NPFJGf4=");//
71 | iv = symmetric.initIv();
72 | System.out.println("key.length:" + key.length);
73 | System.out.println("key:" + Base64.getEncoder().encodeToString(key));
74 | System.out.println("iv:" + Base64.getEncoder().encodeToString(iv));
75 | encrypt = symmetric.encrypt(padding, key, iv, msg.getBytes("utf-8"));
76 | System.out.println("encrypt:" + Base64.getEncoder().encodeToString(encrypt));
77 | decrypt = symmetric.decrypt(padding, key, iv, this.encrypt);
78 | //System.out.println("decrypt:" + Base64.getEncoder().encodeToString(encrypt));
79 | //System.out.println("解密原文:" + new String(decrypt, "utf-8"));
80 | return true;
81 | }
82 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/symmetric/des3DesAesBlowfishRC2RC4/BcSymmetricTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4;
2 |
3 | import com.github.bjlhx15.security.changekey001DH.AbstractDHCoder;
4 | import com.github.bjlhx15.security.changekey001DH.BcDHCoder;
5 | import com.github.bjlhx15.security.changekey001DH.JdkDHCoder;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 |
9 | import java.util.Base64;
10 | import java.util.Map;
11 |
12 | import static org.junit.Assert.*;
13 |
14 | public class BcSymmetricTest {
15 |
16 | AbstractDHCoder abstractDHCoder=null;
17 | @Before
18 | public void setUp(){
19 | abstractDHCoder=new BcDHCoder();
20 | }
21 |
22 | @Test
23 | public void initKey() throws Exception {
24 | String msg="我是密文";
25 | // 甲方构建密钥对
26 | Map.Entry entry = AbstractDHCoder.initKey();
27 | System.out.println("甲方 pubkey:"+ Base64.getEncoder().encodeToString(entry.getKey()));
28 | System.out.println("甲方 prikey:"+ Base64.getEncoder().encodeToString(entry.getValue()));
29 |
30 | System.out.println("甲方将 公钥 发送给 乙方");
31 | System.out.println("乙方 使用甲方pubkey构建密钥对");
32 | Map.Entry entryYi = AbstractDHCoder.initKeyByPubKey(entry.getKey());
33 | System.out.println("乙方 pubkey:"+ Base64.getEncoder().encodeToString(entryYi.getKey()));
34 | System.out.println("乙方 prikey:"+ Base64.getEncoder().encodeToString(entryYi.getValue()));
35 |
36 | System.out.println("乙方 pubkey 发送给 甲方");
37 |
38 | System.out.println("甲方加密数据,使用自己私钥以及 对方公钥");
39 | byte[] encrypt = abstractDHCoder.encrypt(AbstractDHCoder.AES,msg.getBytes(), entryYi.getKey(), entry.getValue());
40 | System.out.println("甲方将加密数据,发送给乙方");
41 | System.out.println("乙方 使用自己私钥以及 对方公钥 解密");
42 | byte[] decrypt = abstractDHCoder.decrypt(AbstractDHCoder.AES,encrypt, entry.getKey(), entryYi.getValue());
43 | System.out.println(new String(decrypt));
44 |
45 | // System.out.println("乙方回发消息");
46 | byte[] encrypt2 = abstractDHCoder.encrypt(AbstractDHCoder.AES,"乙方回发消息".getBytes(), entry.getKey(), entryYi.getValue());
47 |
48 | byte[] decrypt2 = abstractDHCoder.decrypt(AbstractDHCoder.AES,encrypt2, entryYi.getKey(), entry.getValue());
49 | System.out.println(new String(decrypt2));
50 | }
51 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/symmetric/des3DesAesBlowfishRC2RC4/JdkSymmetric3DesTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4;
2 |
3 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.AbstractSymmetric;
4 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.JdkSymmetric;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import java.util.*;
9 |
10 | public class JdkSymmetric3DesTest {
11 | String msg = "测试数据2222";
12 | Map.Entry padding = null;
13 | byte[] key = null;
14 | byte[] iv = null;
15 | byte[] encrypt = null;
16 | byte[] decrypt = null;
17 | AbstractSymmetric symmetric=null;
18 | @Before
19 | public void before(){
20 | symmetric=new JdkSymmetric();
21 | }
22 | // SunJCE: Cipher.DESede -> com.sun.crypto.provider.DESedeCipher
23 | // aliases: [TripleDES]
24 | // attributes: {SupportedPaddings=NOPADDING|PKCS5PADDING|ISO10126PADDING,
25 | // SupportedKeyFormats=RAW, SupportedModes=ECB|CBC|PCBC|CTR|CTS|CFB|OFB|CFB8|CFB16|CFB24|CFB32|CFB40|CFB48|CFB56|CFB64
26 | // |OFB8|OFB16|OFB24|OFB32|OFB40|OFB48|OFB56|OFB64}
27 |
28 | @Test
29 | public void encryptAll() throws Exception {
30 | System.out.println("原文:" + msg);
31 | List> paddingListMap = new ArrayList<>();
32 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede"));//相当于:DES/ECB/PKCS5PADDING
33 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/ECB/NOPADDING"));
34 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/ECB/PKCS5PADDING"));
35 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/ECB/ISO10126PADDING"));
36 |
37 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/CBC/NOPADDING"));
38 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/CBC/PKCS5PADDING"));
39 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/CBC/ISO10126PADDING"));
40 |
41 |
42 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/PCBC/NOPADDING"));
43 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/PCBC/PKCS5PADDING"));
44 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/PCBC/ISO10126PADDING"));
45 |
46 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/CTR/NOPADDING"));
47 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/CTR/PKCS5PADDING"));
48 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede","DESede/CTR/ISO10126PADDING"));//不支持
49 |
50 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/CTS/NOPADDING"));
51 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede", "DESede/CTS/PKCS5PADDING"));
52 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("DESede","DESede/CTS/ISO10126PADDING"));//不支持
53 | for (Map.Entry entry : paddingListMap) {
54 | try {
55 | boolean b = encryptCheck(entry);
56 | if (b) {
57 | System.out.println(entry.getValue() + ":支持");
58 | }
59 | } catch (Exception e) {
60 | e.printStackTrace();
61 | System.out.println(entry.getValue() + ":不支持");
62 | }
63 | }
64 | }
65 |
66 | boolean encryptCheck(Map.Entry entry) throws Exception {
67 | padding = entry;
68 | //Wrong keysize: must be equal to 56,或者不写也可以
69 | key = symmetric.initKey(padding);//Base64.getDecoder().decode("koY9NPFJGf4=");//
70 | iv = AbstractSymmetric.initIv();
71 | System.out.println("key:" + Base64.getEncoder().encodeToString(key));
72 | System.out.println("iv:" + Base64.getEncoder().encodeToString(iv));
73 | encrypt = symmetric.encrypt(padding, key, iv, msg.getBytes("utf-8"));
74 | System.out.println("encrypt:" + Base64.getEncoder().encodeToString(encrypt));
75 | decrypt = symmetric.decrypt(padding, key, iv, this.encrypt);
76 | //System.out.println("decrypt:" + Base64.getEncoder().encodeToString(encrypt));
77 | //System.out.println("解密原文:" + new String(decrypt, "utf-8"));
78 | return true;
79 | }
80 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/symmetric/des3DesAesBlowfishRC2RC4/JdkSymmetricAesTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4;
2 |
3 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.AbstractSymmetric;
4 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.JdkSymmetric;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import java.util.*;
9 |
10 | public class JdkSymmetricAesTest {
11 | String msg = "测试数据2222";
12 | Map.Entry padding = null;
13 | byte[] key = null;
14 | byte[] iv = null;
15 | byte[] encrypt = null;
16 | byte[] decrypt = null;
17 | AbstractSymmetric symmetric = null;
18 |
19 | @Before
20 | public void before() {
21 | symmetric = new JdkSymmetric();
22 | }
23 |
24 | // SunJCE: Cipher.AES -> com.sun.crypto.provider.AESCipher$General
25 | // aliases: [Rijndael]
26 | // attributes: {SupportedPaddings=NOPADDING|PKCS5PADDING|ISO10126PADDING,
27 | // SupportedKeyFormats=RAW, SupportedModes=ECB|CBC|PCBC|CTR|CTS|CFB|OFB|CFB8|CFB16|CFB24|CFB32|CFB40|CFB48|CFB56|CFB64
28 | // |OFB8|OFB16|OFB24|OFB32|OFB40|OFB48|OFB56|OFB64|GCM|CFB72|CFB80|CFB88|CFB96|CFB104|CFB112|CFB120|CFB128|OFB72|OFB80
29 | // |OFB88|OFB96|OFB104|OFB112|OFB120|OFB128}
30 |
31 | @Test
32 | public void encryptAll() throws Exception {
33 | System.out.println("原文:" + msg);
34 | List> paddingListMap = new ArrayList<>();
35 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES"));
36 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/ECB/NOPADDING"));
37 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/ECB/PKCS5PADDING"));
38 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/ECB/ISO10126PADDING"));
39 |
40 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/CBC/NOPADDING"));
41 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/CBC/PKCS5PADDING"));
42 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/CBC/ISO10126PADDING"));
43 |
44 |
45 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/PCBC/NOPADDING"));
46 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/PCBC/PKCS5PADDING"));
47 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/PCBC/ISO10126PADDING"));
48 |
49 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/CTR/NOPADDING"));
50 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/CTR/PKCS5PADDING"));
51 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("AES","AES/CTR/ISO10126PADDING"));//不支持
52 |
53 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/CTS/NOPADDING"));
54 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/CTS/PKCS5PADDING"));
55 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("AES","AES/CTS/ISO10126PADDING"));//不支持
56 |
57 |
58 | paddingListMap.add(new AbstractMap.SimpleEntry<>("AES", "AES/GCM/PKCS5PADDING"));
59 | for (Map.Entry entry : paddingListMap) {
60 | try {
61 | boolean b = encryptCheck(entry);
62 | if (b) {
63 | System.out.println(entry.getValue() + ":支持");
64 | System.out.println("---------------------------------");
65 |
66 | }
67 | } catch (Exception e) {
68 | e.printStackTrace();
69 | System.out.println(entry.getValue() + ":不支持");
70 | }
71 | }
72 | }
73 |
74 | boolean encryptCheck(Map.Entry entry) throws Exception {
75 | padding = entry;
76 | //Wrong keysize: must be equal to 56,或者不写也可以
77 | if (key == null)
78 | key = symmetric.initKey(padding, 128);//Base64.getDecoder().decode("koY9NPFJGf4=");//
79 |
80 | if (iv == null) {
81 | if (entry.getValue().contains("GCM")) {
82 |
83 | iv = AbstractSymmetric.initIv(12);
84 | } else {
85 |
86 | iv = AbstractSymmetric.initIv(16);
87 | }
88 | }
89 |
90 | System.out.println("key:" + Base64.getEncoder().encodeToString(key));
91 | System.out.println("iv:" + Base64.getEncoder().encodeToString(iv));
92 | encrypt = symmetric.encrypt(padding, key, iv, msg.getBytes("utf-8"));
93 | System.out.println("encrypt:" + Base64.getEncoder().encodeToString(encrypt));
94 | decrypt = symmetric.decrypt(padding, key, iv, this.encrypt);
95 | System.out.println("decrypt:" + Base64.getEncoder().encodeToString(encrypt));
96 | System.out.println("解密原文:" + new String(decrypt, "utf-8"));
97 | return true;
98 | }
99 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/symmetric/des3DesAesBlowfishRC2RC4/JdkSymmetricBlowfishTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4;
2 |
3 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.AbstractSymmetric;
4 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.JdkSymmetric;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import java.util.*;
9 |
10 | public class JdkSymmetricBlowfishTest {
11 | String msg = "测试数据2222";
12 | Map.Entry padding = null;
13 | byte[] key = null;
14 | byte[] iv = null;
15 | byte[] encrypt = null;
16 | byte[] decrypt = null;
17 | AbstractSymmetric symmetric=null;
18 | @Before
19 | public void before(){
20 | symmetric=new JdkSymmetric();
21 | }
22 |
23 | // SunJCE: Cipher.Blowfish -> com.sun.crypto.provider.BlowfishCipher
24 | // attributes: {SupportedPaddings=NOPADDING|PKCS5PADDING|ISO10126PADDING,
25 | // SupportedKeyFormats=RAW, SupportedModes=ECB|CBC|PCBC|CTR|CTS|CFB|OFB|CFB8|CFB16|CFB24|CFB32|CFB40|CFB48|CFB56|CFB64
26 | // |OFB8|OFB16|OFB24|OFB32|OFB40|OFB48|OFB56|OFB64}
27 |
28 | @Test
29 | public void encryptAll() throws Exception {
30 | System.out.println("原文:" + msg);
31 | List> paddingListMap = new ArrayList<>();
32 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish"));
33 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/ECB/NOPADDING"));
34 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/ECB/PKCS5PADDING"));
35 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/ECB/ISO10126PADDING"));
36 |
37 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/CBC/NOPADDING"));
38 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/CBC/PKCS5PADDING"));
39 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/CBC/ISO10126PADDING"));
40 |
41 |
42 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/PCBC/NOPADDING"));
43 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/PCBC/PKCS5PADDING"));
44 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/PCBC/ISO10126PADDING"));
45 |
46 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/CTR/NOPADDING"));
47 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/CTR/PKCS5PADDING"));
48 |
49 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/CTS/NOPADDING"));
50 | paddingListMap.add(new AbstractMap.SimpleEntry<>("Blowfish", "Blowfish/CTS/PKCS5PADDING"));
51 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("AES","AES/CTR/ISO10126PADDING"));//不支持
52 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("AES","AES/CTS/ISO10126PADDING"));//不支持
53 | for (Map.Entry entry : paddingListMap) {
54 | try {
55 | boolean b = encryptCheck(entry);
56 | if (b) {
57 | System.out.println(entry.getValue() + ":支持");
58 | }
59 | } catch (Exception e) {
60 | e.printStackTrace();
61 | System.out.println(entry.getValue() + ":不支持");
62 | }
63 | }
64 | }
65 |
66 | boolean encryptCheck(Map.Entry entry) throws Exception {
67 | padding = entry;
68 | key = symmetric.initKey(padding);//Base64.getDecoder().decode("koY9NPFJGf4=");//
69 | iv = AbstractSymmetric.initIv();
70 | System.out.println("key:" + Base64.getEncoder().encodeToString(key));
71 | System.out.println("iv:" + Base64.getEncoder().encodeToString(iv));
72 | encrypt = symmetric.encrypt(padding, key, iv, msg.getBytes("utf-8"));
73 | System.out.println("encrypt:" + Base64.getEncoder().encodeToString(encrypt));
74 | decrypt = symmetric.decrypt(padding, key, iv, this.encrypt);
75 | //System.out.println("decrypt:" + Base64.getEncoder().encodeToString(encrypt));
76 | //System.out.println("解密原文:" + new String(decrypt, "utf-8"));
77 | return true;
78 | }
79 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/symmetric/des3DesAesBlowfishRC2RC4/JdkSymmetricDesTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4;
2 |
3 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.AbstractSymmetric;
4 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.JdkSymmetric;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import java.util.*;
9 |
10 | public class JdkSymmetricDesTest {
11 | String msg = "测试数据2222";
12 | Map.Entry padding = null;
13 | byte[] key = null;
14 | byte[] iv = null;
15 | byte[] encrypt = null;
16 | byte[] decrypt = null;
17 |
18 | AbstractSymmetric symmetric=null;
19 | @Before
20 | public void before(){
21 | symmetric=new JdkSymmetric();
22 | }
23 |
24 |
25 | // DES SupportedPaddings=NOPADDING|PKCS5PADDING|ISO10126PADDING,
26 | // DES SupportedModes=ECB|CBC|PCBC|CTR|CTS|CFB|OFB|
27 | // CFB8|CFB16|CFB24|CFB32|CFB40|CFB48|CFB56|CFB64|
28 | // OFB8|OFB16|OFB24|OFB32|OFB40|OFB48|OFB56|OFB64
29 | @Test
30 | public void encryptAll() throws Exception {
31 | System.out.println("原文:" + msg);
32 | List> paddingListMap = new ArrayList<>();
33 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES"));//相当于:DES/ECB/PKCS5PADDING
34 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/ECB/NOPADDING"));
35 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/ECB/PKCS5PADDING"));
36 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/ECB/ISO10126PADDING"));
37 |
38 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CBC/NOPADDING"));
39 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CBC/PKCS5PADDING"));
40 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CBC/ISO10126PADDING"));
41 |
42 |
43 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/PCBC/NOPADDING"));
44 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/PCBC/PKCS5PADDING"));
45 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/PCBC/ISO10126PADDING"));
46 |
47 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CTR/NOPADDING"));
48 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CTR/PKCS5PADDING"));
49 | //paddingListMap.add(new AbstractMap.SimpleEntry<>("DES","DES/CTR/ISO10126PADDING"));//不支持
50 |
51 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CTS/NOPADDING"));
52 | paddingListMap.add(new AbstractMap.SimpleEntry<>("DES", "DES/CTS/PKCS5PADDING"));
53 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("DES","DES/CTS/ISO10126PADDING"));//不支持
54 | for (Map.Entry entry : paddingListMap) {
55 | try {
56 | boolean b = encryptCheck(entry);
57 | if (b) {
58 | System.out.println(entry.getValue() + ":支持");
59 | }
60 | } catch (Exception e) {
61 | e.printStackTrace();
62 | System.out.println(entry.getValue() + ":不支持");
63 | }
64 | }
65 | }
66 |
67 | boolean encryptCheck(Map.Entry entry) throws Exception {
68 | padding = entry;
69 | //Wrong keysize: must be equal to 56,或者不写也可以
70 | key = symmetric.initKey(padding,56);//Base64.getDecoder().decode("koY9NPFJGf4=");//
71 | iv = AbstractSymmetric.initIv();
72 | System.out.println("key:" + Base64.getEncoder().encodeToString(key));
73 | System.out.println("iv:" + Base64.getEncoder().encodeToString(iv));
74 | encrypt = symmetric.encrypt(padding, key, iv, msg.getBytes("utf-8"));
75 | System.out.println("encrypt:" + Base64.getEncoder().encodeToString(encrypt));
76 | decrypt = symmetric.decrypt(padding, key, iv, this.encrypt);
77 | //System.out.println("decrypt:" + Base64.getEncoder().encodeToString(encrypt));
78 | //System.out.println("解密原文:" + new String(decrypt, "utf-8"));
79 | return true;
80 | }
81 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/symmetric/des3DesAesBlowfishRC2RC4/JdkSymmetricRC2Test.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4;
2 |
3 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.AbstractSymmetric;
4 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.JdkSymmetric;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import java.util.*;
9 |
10 | public class JdkSymmetricRC2Test {
11 | String msg = "测试数据2222";
12 | Map.Entry padding = null;
13 | byte[] key = null;
14 | byte[] iv = null;
15 | byte[] encrypt = null;
16 | byte[] decrypt = null;
17 | AbstractSymmetric symmetric=null;
18 | @Before
19 | public void before(){
20 | symmetric=new JdkSymmetric();
21 | }
22 |
23 | // SunJCE: Cipher.RC2 -> com.sun.crypto.provider.RC2Cipher
24 | // attributes: {SupportedPaddings=NOPADDING|PKCS5PADDING|ISO10126PADDING,
25 | // SupportedKeyFormats=RAW, SupportedModes=ECB|CBC|PCBC|CTR|CTS|CFB|OFB|CFB8|CFB16|CFB24|CFB32|CFB40|CFB48|CFB56|CFB64
26 | // |OFB8|OFB16|OFB24|OFB32|OFB40|OFB48|OFB56|OFB64}
27 |
28 | @Test
29 | public void encryptAll() throws Exception {
30 | System.out.println("原文:" + msg);
31 | List> paddingListMap = new ArrayList<>();
32 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2"));
33 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/ECB/NOPADDING"));
34 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/ECB/PKCS5PADDING"));
35 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/ECB/ISO10126PADDING"));
36 |
37 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/CBC/NOPADDING"));
38 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/CBC/PKCS5PADDING"));
39 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/CBC/ISO10126PADDING"));
40 |
41 |
42 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/PCBC/NOPADDING"));
43 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/PCBC/PKCS5PADDING"));
44 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/PCBC/ISO10126PADDING"));
45 |
46 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/CTR/NOPADDING"));
47 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/CTR/PKCS5PADDING"));
48 |
49 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/CTS/NOPADDING"));
50 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC2", "RC2/CTS/PKCS5PADDING"));
51 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("AES","AES/CTR/ISO10126PADDING"));//不支持
52 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("AES","AES/CTS/ISO10126PADDING"));//不支持
53 | for (Map.Entry entry : paddingListMap) {
54 | try {
55 | boolean b = encryptCheck(entry);
56 | if (b) {
57 | System.out.println(entry.getValue() + ":支持");
58 | }
59 | } catch (Exception e) {
60 | e.printStackTrace();
61 | System.out.println(entry.getValue() + ":不支持");
62 | }
63 | }
64 | }
65 |
66 | boolean encryptCheck(Map.Entry entry) throws Exception {
67 | padding = entry;
68 | key = symmetric.initKey(padding);//Base64.getDecoder().decode("koY9NPFJGf4=");//
69 | iv = AbstractSymmetric.initIv();
70 | System.out.println("key:" + Base64.getEncoder().encodeToString(key));
71 | System.out.println("iv:" + Base64.getEncoder().encodeToString(iv));
72 | encrypt = symmetric.encrypt(padding, key, iv, msg.getBytes("utf-8"));
73 | System.out.println("encrypt:" + Base64.getEncoder().encodeToString(encrypt));
74 | decrypt = symmetric.decrypt(padding, key, iv, this.encrypt);
75 | //System.out.println("decrypt:" + Base64.getEncoder().encodeToString(encrypt));
76 | //System.out.println("解密原文:" + new String(decrypt, "utf-8"));
77 | return true;
78 | }
79 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/symmetric/des3DesAesBlowfishRC2RC4/JdkSymmetricRC4Test.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4;
2 |
3 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.AbstractSymmetric;
4 | import com.github.bjlhx15.security.symmetric.des3DesAesBlowfishRC2RC4.JdkSymmetric;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import java.util.*;
9 |
10 | public class JdkSymmetricRC4Test {
11 | String msg = "测试数据2222";
12 | Map.Entry padding = null;
13 | byte[] key = null;
14 | byte[] iv = null;
15 | byte[] encrypt = null;
16 | byte[] decrypt = null;
17 | AbstractSymmetric symmetric=null;
18 | @Before
19 | public void before(){
20 | symmetric=new JdkSymmetric();
21 | }
22 |
23 | // SunJCE: Cipher.ARCFOUR -> com.sun.crypto.provider.ARCFOURCipher
24 | // aliases: [RC4]
25 | // attributes: {SupportedPaddings=NOPADDING, SupportedKeyFormats=RAW, SupportedModes=ECB}
26 | @Test
27 | public void encryptAll() throws Exception {
28 | System.out.println("原文:" + msg);
29 | List> paddingListMap = new ArrayList<>();
30 | // paddingListMap.add(new AbstractMap.SimpleEntry<>("RC4", "RC4"));
31 | paddingListMap.add(new AbstractMap.SimpleEntry<>("RC4", "RC4/ECB/NOPADDING"));
32 | for (Map.Entry entry : paddingListMap) {
33 | try {
34 | boolean b = encryptCheck(entry);
35 | if (b) {
36 | System.out.println(entry.getValue() + ":支持");
37 | }
38 | } catch (Exception e) {
39 | e.printStackTrace();
40 | System.out.println(entry.getValue() + ":不支持");
41 | }
42 | }
43 | }
44 |
45 | boolean encryptCheck(Map.Entry entry) throws Exception {
46 | padding = entry;
47 | key = symmetric.initKey(padding);//Base64.getDecoder().decode("koY9NPFJGf4=");//
48 | iv = AbstractSymmetric.initIv();
49 | System.out.println("key:" + Base64.getEncoder().encodeToString(key));
50 | System.out.println("iv:" + Base64.getEncoder().encodeToString(iv));
51 | encrypt = symmetric.encrypt(padding, key, iv, msg.getBytes("utf-8"));
52 | System.out.println("encrypt:" + Base64.getEncoder().encodeToString(encrypt));
53 | decrypt = symmetric.decrypt(padding, key, iv, this.encrypt);
54 | //System.out.println("decrypt:" + Base64.getEncoder().encodeToString(encrypt));
55 | //System.out.println("解密原文:" + new String(decrypt, "utf-8"));
56 | return true;
57 | }
58 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/symmetric/pbe/BcPbeTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.pbe;
2 |
3 | import org.bouncycastle.jce.provider.BouncyCastleProvider;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import java.security.Key;
8 | import java.security.SecureRandom;
9 | import java.util.ArrayList;
10 | import java.util.Base64;
11 | import java.util.List;
12 | import java.util.Map;
13 |
14 | import static org.junit.Assert.*;
15 |
16 | public class BcPbeTest {
17 |
18 | String msg = "测试数据2222";
19 | Map.Entry padding = null;
20 | Key key = null;
21 | byte[] salt = null;
22 | byte[] encrypt = null;
23 | byte[] decrypt = null;
24 |
25 | AbstractSymmetric symmetric=null;
26 | @Before
27 | public void before(){
28 | symmetric=new BcPbe();
29 | }
30 |
31 | @Test
32 | public void encryptAll() throws Exception {
33 | //javax.crypto.CipherSpi 实现类 即可
34 | System.out.println("原文:" + msg);
35 | List list = new ArrayList<>();
36 | list.add("PBEWithMD5AndDES");
37 | // list.add("PBEWithMD5AndTripleDES");
38 | //
39 | // list.add("PBEWithSHA1AndDESede");
40 | // list.add("PBEWithSHA1AndRC2_40");
41 | // list.add("PBEWithSHA1AndRC4_40");
42 | // list.add("PBEWithSHA1AndRC2_128");
43 | // list.add("PBEWithSHA1AndRC4_128");
44 |
45 | list.add("PBEWithMD2AndDES");
46 | list.add("PBEWithMD5AndRC2");
47 | list.add("PBEWithSHA1AndDES");
48 | list.add("PBEWithSHA1AndRC2");
49 |
50 | list.add("PBEWithSHAAndIDEA-CBC");
51 | list.add("PBEWITHSHAAND3-KEYTRIPLEDES-CBC");
52 | list.add("PBEWithSHAAnd3KeyTripleDES");
53 | list.add("PBEWITHSHAAND2-KEYTRIPLEDES-CBC");
54 | list.add("PBEWithSHAAnd128BitRC2-CBC");
55 | list.add("PBEWithSHAAnd40BitRC2-CBC");
56 | list.add("PBEWithSHAAnd128BitRC4");
57 | list.add("PBEWithSHAAnd40BitRC4");
58 | list.add("PBEWithSHAAndTwofish-CBC");
59 |
60 | list.add("PBEWITHSHA1AND128BITAES-CBC-BC");
61 | list.add("PBEWITHSHA1AND192BITAES-CBC-BC");
62 | list.add("PBEWITHSHA1AND256BITAES-CBC-BC");
63 | list.add("PBEWITHSHA-1AND128BITAES-CBC-BC");
64 | list.add("PBEWITHSHA-1AND192BITAES-CBC-BC");
65 | list.add("PBEWITHSHA-1AND256BITAES-CBC-BC");
66 | list.add("PBEWITHSHA-256AND128BITAES-CBC-BC");
67 | list.add("PBEWITHSHA-256AND192BITAES-CBC-BC");
68 | list.add("PBEWITHSHA-256AND256BITAES-CBC-BC");
69 | list.add("PBEWITHSHA-256AND128BITAES-BC");
70 | list.add("PBEWITHSHA-256AND192BITAES-BC");
71 | list.add("PBEWITHSHA-256AND256BITAES-BC");
72 | for (String entry : list) {
73 | try {
74 | boolean b = encryptCheck(entry,"123456");
75 | if (b) {
76 | System.out.println(entry + ":支持");
77 | }
78 | } catch (Exception e) {
79 | e.printStackTrace();
80 | System.out.println(entry + ":不支持");
81 | }
82 | }
83 | }
84 |
85 | boolean encryptCheck(String algorithm,String password) throws Exception {
86 | key = symmetric.initKey(algorithm,password);
87 | salt = AbstractSymmetric.initSalt(8);
88 | System.out.println("key:" + Base64.getEncoder().encodeToString(key.getEncoded()));
89 | System.out.println("salt:" + Base64.getEncoder().encodeToString(salt));
90 |
91 | byte[] iv = new SecureRandom().generateSeed(8);
92 | encrypt = symmetric.encrypt(algorithm, key, salt,iv, msg.getBytes("utf-8"));
93 | System.out.println("encrypt:" + Base64.getEncoder().encodeToString(encrypt));
94 | decrypt = symmetric.decrypt(algorithm, key, salt,iv, this.encrypt);
95 | //System.out.println("decrypt:" + Base64.getEncoder().encodeToString(encrypt));
96 | //System.out.println("解密原文:" + new String(decrypt, "utf-8"));
97 | return true;
98 | }
99 | }
--------------------------------------------------------------------------------
/algorithm-sign-impl/src/test/java/com/github/bjlhx15/security/symmetric/pbe/JdkPbeTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.security.symmetric.pbe;
2 |
3 | import com.sun.crypto.provider.SunJCE;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import javax.crypto.Cipher;
8 | import java.security.Key;
9 | import java.security.SecureRandom;
10 | import java.util.*;
11 |
12 | import static org.junit.Assert.*;
13 |
14 | public class JdkPbeTest {
15 | String msg = "测试数据2222";
16 | Map.Entry padding = null;
17 | Key key = null;
18 | byte[] salt = null;
19 | byte[] encrypt = null;
20 | byte[] decrypt = null;
21 |
22 | AbstractSymmetric symmetric=null;
23 | @Before
24 | public void before(){
25 | symmetric=new JdkPbe();
26 | }
27 |
28 |
29 | @Test
30 | public void encryptAll() throws Exception {
31 | System.out.println("原文:" + msg);
32 | List list = new ArrayList<>();
33 | list.add("PBEWithMD5AndDES");
34 | list.add("PBEWithMD5AndTripleDES");
35 |
36 | list.add("PBEWithSHA1AndDESede");
37 | list.add("PBEWithSHA1AndRC2_40");
38 | list.add("PBEWithSHA1AndRC4_40");
39 | list.add("PBEWithSHA1AndRC2_128");
40 | list.add("PBEWithSHA1AndRC4_128");
41 | for (String entry : list) {
42 | try {
43 | boolean b = encryptCheck(entry,"123456");
44 | if (b) {
45 | System.out.println(entry + ":支持");
46 | }
47 | } catch (Exception e) {
48 | e.printStackTrace();
49 | System.out.println(entry + ":不支持");
50 | }
51 | }
52 | }
53 |
54 | @Test
55 | public void encryptAll2() throws Exception {
56 | System.out.println("原文:" + msg);
57 | String algo="PBEWITHHMACSHA1ANDAES_128,PBEWITHHMACSHA1ANDAES_256,PBEWITHHMACSHA224ANDAES_128,PBEWITHHMACSHA224ANDAES_256,PBEWITHHMACSHA256ANDAES_128,PBEWITHHMACSHA256ANDAES_256,PBEWITHHMACSHA384ANDAES_128,PBEWITHHMACSHA384ANDAES_256,PBEWITHHMACSHA512ANDAES_128,PBEWITHHMACSHA512ANDAES_256,PBEWITHMD5ANDDES,PBEWITHMD5ANDTRIPLEDES,PBEWITHSHA1ANDDESEDE,PBEWITHSHA1ANDRC2_128,PBEWITHSHA1ANDRC2_40,PBEWITHSHA1ANDRC4_128,PBEWITHSHA1ANDRC4_40";
58 | String[] split = algo.split(",");
59 | for (String entry : split) {
60 | try {
61 | System.out.println(entry + ":算法开始=====");
62 | boolean b = encryptCheck(entry,"123456");
63 | if (b) {
64 | System.out.println(entry + ":支持");
65 | }
66 | System.out.println(entry + ":算法结束=====");
67 | } catch (Exception e) {
68 | // e.printStackTrace();
69 | System.err.println(entry + ":不支持"+e.getMessage());
70 | }
71 | System.out.println();
72 | }
73 | }
74 |
75 | boolean encryptCheck(String algorithm,String password) throws Exception {
76 | key = symmetric.initKey(algorithm,password);
77 | salt = AbstractSymmetric.initSalt(8);
78 | System.out.println("key:" + Base64.getEncoder().encodeToString(key.getEncoded()));
79 | System.out.println("salt:" + Base64.getEncoder().encodeToString(salt));
80 | byte[] iv = new SecureRandom().generateSeed(16);
81 | encrypt = symmetric.encrypt(algorithm, key, salt,iv, msg.getBytes("utf-8"));
82 | System.out.println("encrypt:" + Base64.getEncoder().encodeToString(encrypt));
83 | decrypt = symmetric.decrypt(algorithm, key, salt,iv, this.encrypt);
84 | //System.out.println("decrypt:" + Base64.getEncoder().encodeToString(encrypt));
85 | //System.out.println("解密原文:" + new String(decrypt, "utf-8"));
86 | return true;
87 | }
88 | }
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/java/com/github/bjlhx15/sign/eg001/seal/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.boot.builder.SpringApplicationBuilder;
6 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7 | import org.springframework.context.annotation.ImportResource;
8 |
9 | @SpringBootApplication
10 | //@EnableAsync
11 | @ImportResource({
12 | "classpath:spring-config.xml"
13 | })
14 | //@PropertySource(value = {
15 | // "${config.path}"
16 | //}, encoding = "utf-8")
17 | //@Import({MyImportBeanDefinitionRegistrar.class})
18 | //@Import({MyImportSelector.class})
19 | //@Import({TestJoinBean.class})
20 | public class DemoApplication extends SpringBootServletInitializer {
21 | @Override
22 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
23 | return application.sources(DemoApplication.class);
24 | }
25 |
26 |
27 | //main启动
28 | public static void main(String[] args) {
29 | System.setProperty("es.set.netty.runtime.available.processors", "false");
30 | SpringApplication.run(DemoApplication.class, args);
31 | System.err.println("\r\n---项目 启动成功---");
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/java/com/github/bjlhx15/sign/eg001/seal/imgcompress/CompressImg.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.imgcompress;
2 |
3 | import net.coobird.thumbnailator.Thumbnails;
4 |
5 | import java.io.*;
6 |
7 | /**
8 | * @author lihongxu6
9 | * @version 1.0
10 | * @className CompressImg
11 | * @description TODO
12 | * @date 2021-01-23 10:03
13 | */
14 | public class CompressImg {
15 | public static ByteArrayOutputStream commpressPicForScale(InputStream inputStream, double size) throws Exception {
16 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
17 | int available = inputStream.available();
18 | if (available < size * 1024) {
19 | inputToOut(inputStream, outputStream);
20 | return outputStream;
21 | }
22 |
23 | Thumbnails.of(inputStream)
24 | .outputQuality(0.5f)
25 | .scale(0.8f)
26 | .toOutputStream(outputStream);
27 | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
28 | return commpressPicForScale(byteArrayInputStream, size);
29 | }
30 |
31 | public static ByteArrayInputStream commpressPicForScaleInput(InputStream inputStream, double size) throws Exception {
32 | ByteArrayOutputStream outputStream = commpressPicForScale(inputStream, size);
33 | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
34 | return byteArrayInputStream;
35 | }
36 |
37 | public static void inputToOut(InputStream source, OutputStream target) throws IOException {
38 | byte[] buf = new byte[8192];
39 | int length;
40 | while ((length = source.read(buf)) > 0) {
41 | target.write(buf, 0, length);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/java/com/github/bjlhx15/sign/eg001/seal/pdf/PdfUtil.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.pdf;
2 |
3 | import com.itextpdf.text.DocumentException;
4 | import com.itextpdf.text.pdf.AcroFields;
5 | import com.itextpdf.text.pdf.BaseFont;
6 | import com.itextpdf.text.pdf.PdfReader;
7 | import com.itextpdf.text.pdf.PdfStamper;
8 |
9 | import java.io.ByteArrayOutputStream;
10 | import java.io.FileOutputStream;
11 | import java.io.IOException;
12 | import java.io.OutputStream;
13 | import java.util.ArrayList;
14 | import java.util.Iterator;
15 | import java.util.List;
16 | import java.util.Map;
17 |
18 | /**
19 | * @author lihongxu6
20 | * @version 1.0
21 | * @className PdfUtil
22 | * @description TODO
23 | * @date 2021-01-20 16:53
24 | */
25 | public class PdfUtil {
26 | /**
27 | * @param fields
28 | * @param data
29 | * @throws IOException
30 | * @throws DocumentException
31 | */
32 | private static void fillData(AcroFields fields, Map data) throws IOException, DocumentException {
33 | List keys = new ArrayList();
34 | Map formFields = fields.getFields();
35 | for (String key : data.keySet()) {
36 | if(formFields.containsKey(key)){
37 | String value = data.get(key);
38 | fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的
39 | keys.add(key);
40 | }
41 | }
42 | Iterator itemsKey = formFields.keySet().iterator();
43 | while(itemsKey.hasNext()){
44 | String itemKey = itemsKey.next();
45 | if(!keys.contains(itemKey)){
46 | fields.setField(itemKey, " ");
47 | }
48 | }
49 | }
50 |
51 | /**
52 | * @param templatePdfPath
53 | * 模板pdf路径
54 | * @param generatePdfPath
55 | * 生成pdf路径
56 | * @param data
57 | * 数据
58 | */
59 | public static String generatePDF(String templatePdfPath, String generatePdfPath, Map data) {
60 | OutputStream fos = null;
61 | ByteArrayOutputStream bos = null;
62 | try {
63 | PdfReader reader = new PdfReader(templatePdfPath);
64 | bos = new ByteArrayOutputStream();
65 | /* 将要生成的目标PDF文件名称 */
66 | PdfStamper ps = new PdfStamper(reader, bos);
67 | /* 使用中文字体 */
68 | BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
69 | ArrayList fontList = new ArrayList();
70 | fontList.add(bf);
71 | /* 取出报表模板中的所有字段 */
72 | AcroFields fields = ps.getAcroFields();
73 | fields.setSubstitutionFonts(fontList);
74 | fillData(fields, data);
75 | /* 必须要调用这个,否则文档不会生成的 如果为false那么生成的PDF文件还能编辑,一定要设为true*/
76 | ps.setFormFlattening(true);
77 | ps.close();
78 | fos = new FileOutputStream(generatePdfPath);
79 | fos.write(bos.toByteArray());
80 | fos.flush();
81 | return generatePdfPath;
82 | } catch (Exception e) {
83 | e.printStackTrace();
84 | } finally {
85 | if (fos != null) {
86 | try {
87 | fos.close();
88 | } catch (IOException e) {
89 | e.printStackTrace();
90 | }
91 | }
92 | if (bos != null) {
93 | try {
94 | bos.close();
95 | } catch (IOException e) {
96 | e.printStackTrace();
97 | }
98 | }
99 | }
100 | return null;
101 | }
102 |
103 | }
104 |
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/java/com/github/bjlhx15/sign/eg001/seal/pkcs12/Extension.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.pkcs12;
2 |
3 | /**
4 | * @author lihongxu6
5 | * @version 1.0
6 | * @className Extension
7 | * @description TODO
8 | * @date 2021-01-20 13:57
9 | */
10 | public class Extension {
11 | private String oid;
12 |
13 | private boolean critical;
14 |
15 | private byte[] value;
16 |
17 | public String getOid() {
18 | return oid;
19 | }
20 |
21 | public byte[] getValue() {
22 | return value;
23 | }
24 | public boolean isCritical() {
25 | return critical;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/java/com/github/bjlhx15/sign/eg001/seal/sealimg/conf/SealCircle.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.sealimg.conf;
2 |
3 | /**
4 | * @author lihongxu6
5 | * @version 1.0
6 | * @className SealCircle
7 | * @description TODO
8 | * @date 2021-01-20 00:00
9 | */
10 | public class SealCircle {
11 | public SealCircle(Integer lineSize, Integer width,Integer height) {
12 | this.lineSize = lineSize;
13 | this.width = width;
14 | this.height = height;
15 | }
16 |
17 | /**
18 | * 线宽度
19 | */
20 | private Integer lineSize;
21 | /**
22 | * 半径
23 | */
24 | private Integer width;
25 | /**
26 | * 半径
27 | */
28 | private Integer height;
29 |
30 | public Integer getLineSize() {
31 | return lineSize;
32 | }
33 |
34 | public Integer getHeight() {
35 | return height;
36 | }
37 |
38 | public Integer getWidth() {
39 | return width;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/java/com/github/bjlhx15/sign/eg001/seal/sealimg/conf/SealConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.sealimg.conf;
2 |
3 | import java.awt.*;
4 |
5 | /**
6 | * @author lihongxu6
7 | * @version 1.0
8 | * @className SealConfiguration
9 | * @description TODO
10 | * @date 2021-01-20 00:01
11 | */
12 | public class SealConfiguration {
13 | /**
14 | * 主文字
15 | */
16 | private SealFont mainFont;
17 | /**
18 | * 副文字
19 | */
20 | private SealFont viceFont;
21 | /**
22 | * 抬头文字
23 | */
24 | private SealFont titleFont;
25 | /**
26 | * 中心文字
27 | */
28 | private SealFont centerFont;
29 | /**
30 | * 边线圆
31 | */
32 | private SealCircle borderCircle;
33 | /**
34 | * 内边线圆
35 | */
36 | private SealCircle borderInnerCircle;
37 | /**
38 | * 内线圆
39 | */
40 | private SealCircle innerCircle;
41 | /**
42 | * 背景色,默认红色
43 | */
44 | private Color backgroudColor = Color.RED;
45 | /**
46 | * 图片输出尺寸,默认300
47 | */
48 | private Integer imageSize = 300;
49 |
50 | public SealConfiguration setMainFont(SealFont mainFont) {
51 | this.mainFont = mainFont;
52 | return this;
53 | }
54 |
55 | public SealConfiguration setViceFont(SealFont viceFont) {
56 | this.viceFont = viceFont;
57 | return this;
58 | }
59 |
60 | public SealConfiguration setTitleFont(SealFont titleFont) {
61 | this.titleFont = titleFont;
62 | return this;
63 | }
64 |
65 | public SealConfiguration setCenterFont(SealFont centerFont) {
66 | this.centerFont = centerFont;
67 | return this;
68 | }
69 |
70 | public SealConfiguration setBorderCircle(SealCircle borderCircle) {
71 | this.borderCircle = borderCircle;
72 | return this;
73 | }
74 |
75 | public SealConfiguration setBorderInnerCircle(SealCircle borderInnerCircle) {
76 | this.borderInnerCircle = borderInnerCircle;
77 | return this;
78 | }
79 |
80 | public SealConfiguration setInnerCircle(SealCircle innerCircle) {
81 | this.innerCircle = innerCircle;
82 | return this;
83 | }
84 |
85 | public SealConfiguration setBackgroudColor(Color backgroudColor) {
86 | this.backgroudColor = backgroudColor;
87 | return this;
88 | }
89 |
90 | public SealConfiguration setImageSize(Integer imageSize) {
91 | this.imageSize = imageSize;
92 | return this;
93 | }
94 |
95 | public SealFont getMainFont() {
96 | return mainFont;
97 | }
98 |
99 | public SealFont getViceFont() {
100 | return viceFont;
101 | }
102 |
103 | public SealFont getTitleFont() {
104 | return titleFont;
105 | }
106 |
107 | public SealFont getCenterFont() {
108 | return centerFont;
109 | }
110 |
111 | public SealCircle getBorderCircle() {
112 | return borderCircle;
113 | }
114 |
115 | public SealCircle getBorderInnerCircle() {
116 | return borderInnerCircle;
117 | }
118 |
119 | public SealCircle getInnerCircle() {
120 | return innerCircle;
121 | }
122 |
123 | public Color getBackgroudColor() {
124 | return backgroudColor;
125 | }
126 |
127 | public Integer getImageSize() {
128 | return imageSize;
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/java/com/github/bjlhx15/sign/eg001/seal/sealimg/conf/SealFont.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.sealimg.conf;
2 |
3 | import java.awt.*;
4 |
5 | /**
6 | * @author lihongxu6
7 | * @version 1.0
8 | * @className SealFont
9 | * @description TODO
10 | * @date 2021-01-20 00:01
11 | */
12 | public class SealFont {
13 | public SealFont(String fontText) {
14 | this.fontText = fontText;
15 | }
16 |
17 | public SealFont() {
18 | }
19 |
20 | /**
21 | * 字体内容
22 | */
23 | private String fontText;
24 | /**
25 | * 是否加粗
26 | */
27 | private Boolean isBold = true;
28 | /**
29 | * 字形名,默认为宋体
30 | */
31 | private String fontFamily = "宋体";
32 | /**
33 | * 字体大小
34 | */
35 | private Integer fontSize;
36 | /**
37 | * 字距
38 | */
39 | private Double fontSpace;
40 | /**
41 | * 边距(环边距或上边距)
42 | */
43 | private Integer marginSize;
44 |
45 | /**
46 | * 获取系统支持的字形名集合
47 | */
48 | public static String[] getSupportFontNames() {
49 | return GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
50 | }
51 |
52 | public SealFont setFontSpace(Double fontSpace) {
53 | this.fontSpace = fontSpace;
54 | return this;
55 | }
56 |
57 | public SealFont setMarginSize(Integer marginSize) {
58 | this.marginSize = marginSize;
59 | return this;
60 | }
61 |
62 | public SealFont setFontFamily(String fontFamily) {
63 | this.fontFamily = fontFamily;
64 | return this;
65 | }
66 |
67 | public SealFont setFontText(String fontText) {
68 | this.fontText = fontText;
69 | return this;
70 | }
71 |
72 | public SealFont setFontSize(Integer fontSize) {
73 | this.fontSize = fontSize;
74 | return this;
75 | }
76 |
77 | public SealFont setBold(Boolean bold) {
78 | isBold = bold;
79 | return this;
80 | }
81 |
82 | public String getFontText() {
83 | return fontText;
84 | }
85 |
86 | public String getFontFamily() {
87 | return fontFamily;
88 | }
89 |
90 | public Integer getFontSize() {
91 | return fontSize;
92 | }
93 |
94 | public Double getFontSpace() {
95 | return fontSpace;
96 | }
97 |
98 | public Integer getMarginSize() {
99 | return marginSize;
100 | }
101 |
102 | public Boolean isBold() {
103 | return isBold;
104 | }
105 |
106 | }
107 |
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/java/com/github/bjlhx15/sign/eg001/seal/sign/SignPdf.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.sign;
2 |
3 | import com.itextpdf.text.Image;
4 | import com.itextpdf.text.Rectangle;
5 | import com.itextpdf.text.pdf.PdfReader;
6 | import com.itextpdf.text.pdf.PdfSignatureAppearance;
7 | import com.itextpdf.text.pdf.PdfStamper;
8 | import com.itextpdf.text.pdf.security.*;
9 | import org.bouncycastle.jce.provider.BouncyCastleProvider;
10 |
11 | import java.io.ByteArrayOutputStream;
12 | import java.io.File;
13 | import java.io.FileInputStream;
14 | import java.io.IOException;
15 | import java.security.KeyStore;
16 | import java.security.PrivateKey;
17 | import java.security.Security;
18 | import java.security.cert.Certificate;
19 | import java.util.UUID;
20 |
21 | /**
22 | * @author lihongxu6
23 | * @version 1.0
24 | * @className SignPdf
25 | * @description TODO
26 | * @date 2021-01-25 22:07
27 | */
28 | public class SignPdf {
29 | /**
30 | * @param password 秘钥密码
31 | * @param keyStorePath 秘钥文件路径
32 | * @param signPdfSrc 签名的PDF文件
33 | * @param signImage 签名图片文件
34 | * @param x x坐标
35 | * @param y y坐标
36 | * @return
37 | */
38 | public static byte[] sign(String password, String keyStorePath, String signPdfSrc, String signImage,
39 | float x, float y) {
40 | File signPdfSrcFile = new File(signPdfSrc);
41 | PdfReader reader = null;
42 | ByteArrayOutputStream signPDFData = null;
43 | PdfStamper stp = null;
44 | FileInputStream fos = null;
45 | try {
46 | BouncyCastleProvider provider = new BouncyCastleProvider();
47 | Security.addProvider(provider);
48 | KeyStore ks = KeyStore.getInstance("PKCS12", new BouncyCastleProvider());
49 | fos = new FileInputStream(keyStorePath);
50 | // 私钥密码 为Pkcs生成证书是的私钥密码 123456
51 | ks.load(fos, password.toCharArray());
52 | String alias = (String) ks.aliases().nextElement();
53 | PrivateKey key = (PrivateKey) ks.getKey(alias, password.toCharArray());
54 | Certificate[] chain = ks.getCertificateChain(alias);
55 | reader = new PdfReader(signPdfSrc);
56 | signPDFData = new ByteArrayOutputStream();
57 | // 临时pdf文件
58 | File temp = new File(signPdfSrcFile.getParent(), System.currentTimeMillis() + ".pdf");
59 | stp = PdfStamper.createSignature(reader, signPDFData, '\0', temp, true);
60 | stp.setFullCompression();
61 | PdfSignatureAppearance sap = stp.getSignatureAppearance();
62 | sap.setReason("数字签名,不可改变");
63 | // 使用png格式透明图片
64 | Image image = Image.getInstance(signImage);
65 | sap.setImageScale(0);
66 | sap.setSignatureGraphic(image);
67 | sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC);
68 | // 是对应x轴和y轴坐标
69 | sap.setVisibleSignature(new Rectangle(x, y, x + 185, y + 68), 1,
70 | UUID.randomUUID().toString().replaceAll("-", ""));
71 | stp.getWriter().setCompressionLevel(5);
72 | ExternalDigest digest = new BouncyCastleDigest();
73 | ExternalSignature signature = new PrivateKeySignature(key, DigestAlgorithms.SHA512, provider.getName());
74 | MakeSignature.signDetached(sap, digest, signature, chain, null, null, null, 0, MakeSignature.CryptoStandard.CADES);
75 | stp.close();
76 | reader.close();
77 | return signPDFData.toByteArray();
78 | } catch (Exception e) {
79 | e.printStackTrace();
80 | } finally {
81 |
82 | if (signPDFData != null) {
83 | try {
84 | signPDFData.close();
85 | } catch (IOException e) {
86 | }
87 | }
88 |
89 | if (fos != null) {
90 | try {
91 | fos.close();
92 | } catch (IOException e) {
93 | }
94 | }
95 | }
96 | return null;
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/WechatIMG2.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/WechatIMG2.jpeg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/WechatIMG2_new.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/WechatIMG2_new.jpeg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | #spring.profiles.active=@profiles.active@
2 |
3 | server.port=8080
4 |
5 |
6 | logging.config=classpath:log4j2-spring-dev.xml
7 |
8 | preset_auth_role_permission=[{"type":"examiner","name":"\u7528\u7AE0\u5BA1\u6838\u4EBA","menuUrls":["/home","/contract","/audit","/bcStorage","/company","/user"],"btnCodes":[],"dataCodes":[]},{"type":"stamper","name":"\u7528\u7AE0\u5458","menuUrls":["/home","/contract","/audit","/bcStorage","/company","/user"],"btnCodes":["contractManager:allFile:sign"],"dataCodes":[]},{"type":"admin","name":"\u7BA1\u7406\u5458","menuUrls":["/home","/contract","/audit","/bcStorage","/company","/user"],"btnCodes":["contractManager:allFile:sign"],"dataCodes":[]}]
9 |
10 |
11 | config.path=classpath:/application-init-cert.properties,classpath:/application-init-cert2.properties
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/book.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/book.docx
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/book.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/book.pdf
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/book2.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/book2.docx
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/cert/keystore.cer:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/cert/keystore.cer
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/cert/keystore.p12:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/cert/keystore.p12
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_+90.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_+90.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_-90.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_-90.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_110%.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_110%.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_120x120.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_120x120.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_1280x1024.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_1280x1024.gif
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_1280x1024.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_1280x1024.png
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_1280x1024_BufferedImage.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_1280x1024_BufferedImage.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_1280x1024_OutputStream.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_1280x1024_OutputStream.png
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_200x300.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_200x300.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_25%.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_25%.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_2560x2048.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_2560x2048.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_region_bootom_right.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_region_bootom_right.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_region_center.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_region_center.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_region_coord.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_region_coord.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_watermark_bottom_right.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_watermark_bottom_right.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/img/WechatIMG_watermark_center.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/img/WechatIMG_watermark_center.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/log4j2-spring-dev.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
6 |
7 |
8 |
9 |
10 | logs/
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
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 |
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/sealimg/公章.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/sealimg/公章.png
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/sealimg/私章.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/sealimg/私章.png
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/signed.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/signed.pdf
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/spring-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/test-Org.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/test-Org.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/test-cp1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/test-cp1.jpg
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/tpl.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/tpl.docx
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/tpl.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/tpl.pdf
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/tpl2.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/tpl2.docx
--------------------------------------------------------------------------------
/eg001-seal-sign/src/main/resources/watermark.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/main/resources/watermark.png
--------------------------------------------------------------------------------
/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/doc/WordToPdfTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.doc;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * @author lihongxu6
9 | * @version 1.0
10 | * @className WordToPdfTest
11 | * @description TODO
12 | * @date 2021-01-25 16:17
13 | */
14 | public class WordToPdfTest {
15 |
16 | @Test
17 | public void htmlToPdf() {
18 | String docxHtml = WordToPdf.docx2Html("src/main/resources/book2.docx", null);
19 | docxHtml = WordToPdf.formatHtml(docxHtml);
20 | WordToPdf.htmlToPdf(docxHtml, "src/main/resources/book2.pdf");
21 | }
22 | }
--------------------------------------------------------------------------------
/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/doc/WordUntils2Test.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.doc;
2 |
3 | import com.alibaba.fastjson.JSONArray;
4 | import com.alibaba.fastjson.JSONObject;
5 | import org.junit.Test;
6 |
7 | import java.util.HashMap;
8 | import java.util.Map;
9 |
10 | /**
11 | * @author lihongxu6
12 | * @version 1.0
13 | * @className WordUntils2Test
14 | * @description TODO
15 | * @date 2021-01-23 22:45
16 | */
17 | public class WordUntils2Test {
18 |
19 | @Test
20 | public void generateWord() throws Exception {
21 | Map data = new HashMap();
22 | //key为pdf模板的form表单的名字,value为需要填充的值
23 | data.put("${title}", "证书协议");
24 | data.put("${name}", "李宏旭");
25 | data.put("${gender}", "男");
26 | data.put("${Aname}", "李小旭");
27 | data.put("${Bname}", "李大旭");
28 | WordUtil.generateWord(
29 | "src/main/resources/tpl2.docx", "src/main/resources/book2.docx", data);
30 | }
31 | }
--------------------------------------------------------------------------------
/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/imgcompress/CompressImgTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.imgcompress;
2 |
3 | import org.apache.poi.util.IOUtils;
4 | import org.junit.Test;
5 |
6 | import java.io.*;
7 |
8 | /**
9 | * @author lihongxu6
10 | * @version 1.0
11 | * @className CompressImgTest
12 | * @description TODO
13 | * @date 2021-01-23 10:03
14 | */
15 | public class CompressImgTest {
16 |
17 | /**
18 | * 压缩至500kb,输出流
19 | */
20 | @Test
21 | public void handle() throws Exception {
22 | File file = new File("src/main/resources/WechatIMG2.jpeg");
23 | InputStream inputStream = new FileInputStream(file);
24 | ByteArrayOutputStream byteArrayOutputStream = CompressImg.commpressPicForScale(inputStream, 500);
25 | try (OutputStream outputStream = new FileOutputStream("src/main/resources/WechatIMG2_new.jpeg")) {
26 | byteArrayOutputStream.writeTo(outputStream);
27 | }
28 | }
29 |
30 | /**
31 | * 压缩至300kb,输入流
32 | */
33 | @Test
34 | public void commpressPicForScaleInput() throws Exception {
35 | File file = new File("src/main/resources/WechatIMG2.jpeg");
36 | InputStream inputStream = new FileInputStream(file);
37 | ByteArrayInputStream inputStream2 = CompressImg.commpressPicForScaleInput(inputStream, 300);
38 |
39 | try (FileOutputStream fileOutputStream = new FileOutputStream("src/main/resources/WechatIMG2_new.jpeg")) {
40 | IOUtils.copy(inputStream2, fileOutputStream);
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/imgcompress/ThumbnailsTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.imgcompress;
2 |
3 | import net.coobird.thumbnailator.Thumbnails;
4 | import net.coobird.thumbnailator.geometry.Positions;
5 | import org.junit.Test;
6 |
7 | import javax.imageio.ImageIO;
8 | import java.awt.image.BufferedImage;
9 | import java.io.*;
10 |
11 | /**
12 | * @author lihongxu6
13 | * @version 1.0
14 | * @className CompressImgTest
15 | * @description TODO
16 | * @date 2021-01-23 10:03
17 | */
18 | public class ThumbnailsTest {
19 |
20 | @Test
21 | public void handle() throws Exception {
22 |
23 | String inputPath = "src/main/resources/WechatIMG2.jpeg";
24 | String outputPathPrefix = "src/main/resources/img/WechatIMG_";
25 | String watermark = "src/main/resources/watermark.png";
26 |
27 | /**
28 | * size(width,height) 指定大小进行缩放,若图片横比200小,高比300小,不变
29 | * 若图片横比200小,高比300大,高缩小到300,图片比例不变 若图片横比200大,高比300小,横缩小到200,图片比例不变
30 | * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300
31 | */
32 | Thumbnails.of(inputPath).size(200, 300).toFile(outputPathPrefix + "200x300.jpg");
33 | Thumbnails.of(inputPath).size(2560, 2048).toFile(outputPathPrefix + "2560x2048.jpg");
34 |
35 | /**
36 | * scale(比例):按照比例进行缩放
37 | */
38 | Thumbnails.of(inputPath).scale(0.25f).toFile(outputPathPrefix + "25%.jpg");
39 | Thumbnails.of(inputPath).scale(1.10f).toFile(outputPathPrefix + "110%.jpg");
40 |
41 | /**
42 | * 不按照比例,指定大小进行缩放 设为false
43 | * keepAspectRatio(true) 默认是按照比例缩放的
44 | */
45 | Thumbnails.of(inputPath).size(120, 120).keepAspectRatio(false).toFile(outputPathPrefix + "120x120.jpg");
46 |
47 | /**
48 | * rotate(角度),旋转,正数:顺时针 负数:逆时针
49 | */
50 | Thumbnails.of(inputPath).size(1280, 1024).rotate(90).toFile(outputPathPrefix+"+90.jpg");
51 | Thumbnails.of(inputPath).size(1280, 1024).rotate(-90).toFile(outputPathPrefix+"-90.jpg");
52 |
53 | /**
54 | * watermark 水印 (位置,水印图,透明度)
55 | */
56 | Thumbnails.of(inputPath).size(1280, 1024)
57 | .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(watermark)), 0.5f)
58 | .outputQuality(0.8f).toFile(outputPathPrefix + "watermark_bottom_right.jpg");
59 | Thumbnails.of(inputPath).size(1280, 1024)
60 | .watermark(Positions.CENTER, ImageIO.read(new File(watermark)), 0.5f)
61 | .outputQuality(0.8f).toFile(outputPathPrefix + "watermark_center.jpg");
62 |
63 | /**
64 | * 裁剪
65 | */
66 | // 图片中心400*400的区域
67 | Thumbnails.of(inputPath).sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false)
68 | .toFile(outputPathPrefix + "region_center.jpg");
69 | //图片右下400*400的区域
70 | Thumbnails.of(inputPath).sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false)
71 | .toFile(outputPathPrefix + "region_bootom_right.jpg");
72 | //指定坐标
73 | Thumbnails.of(inputPath).sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false)
74 | .toFile(outputPathPrefix + "region_coord.jpg");
75 |
76 | /**
77 | * outputFormat 转化图像格式(图像格式)
78 | */
79 | Thumbnails.of(inputPath).size(1280, 1024).outputFormat("png").toFile(outputPathPrefix + "1280x1024.png");
80 | Thumbnails.of(inputPath).size(1280, 1024).outputFormat("gif").toFile(outputPathPrefix + "1280x1024.gif");
81 |
82 | /**
83 | * toOutputStream 输出到OutputStream(流对象)
84 | */
85 | OutputStream os = new FileOutputStream(outputPathPrefix + "1280x1024_OutputStream.png");
86 | Thumbnails.of(inputPath).size(1280, 1024).toOutputStream(os);
87 |
88 | /**
89 | * asBufferedImage() 输出到BufferedImage,返回BufferedImage
90 | */
91 | BufferedImage thumbnail = Thumbnails.of(inputPath).size(1280, 1024).asBufferedImage();
92 | ImageIO.write(thumbnail, "jpg", new File(outputPathPrefix + "1280x1024_BufferedImage.jpg"));
93 | }
94 |
95 | }
--------------------------------------------------------------------------------
/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/pdf/PdfUtilTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.pdf;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.HashMap;
6 | import java.util.Map;
7 |
8 | import static org.junit.Assert.*;
9 |
10 | /**
11 | * @author lihongxu6
12 | * @version 1.0
13 | * @className PdfUtilTest
14 | * @description TODO
15 | * @date 2021-01-20 16:54
16 | */
17 | public class PdfUtilTest {
18 |
19 | @Test
20 | public void generatePDF() {
21 | Map data = new HashMap();
22 | //key为pdf模板的form表单的名字,value为需要填充的值
23 | data.put("title", "证书协议");
24 | data.put("name", "李宏旭");
25 | data.put("gender", "男");
26 | data.put("Aname", "李小旭");
27 | data.put("Bname", "李大旭");
28 | PdfUtil.generatePDF("src/main/resources/tpl.pdf","src/main/resources/book.pdf", data);
29 |
30 | }
31 | }
--------------------------------------------------------------------------------
/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/pkcs12/Pkcs12UtilTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.pkcs12;
2 |
3 | import org.junit.Test;
4 |
5 | import java.io.File;
6 | import java.io.FileOutputStream;
7 | import java.util.Map;
8 |
9 | /**
10 | * @author lihongxu6
11 | * @version 1.0
12 | * @className Pkcs12UtilTest
13 | * @description TODO
14 | * @date 2021-01-20 14:02
15 | */
16 | public class Pkcs12UtilTest {
17 |
18 | @Test
19 | public void createCert() throws Exception {
20 | // CN: 名字与姓氏 OU : 组织单位名称
21 | // O :组织名称 L : 城市或区域名称 E : 电子邮件
22 | // ST: 州或省份名称 C: 单位的两字母国家代码
23 | String issuerStr = "CN=李宏旭测试公司,OU=github研发部,O=github有限公司,C=CN,E=bjlhx15@163com,L=北京,ST=北京";
24 | String subjectStr = "CN=LihongxuTestCompany,OU=github研发部,O=github有限公司,C=CN,E=bjlhx15@163com,L=北京,ST=北京";
25 | String certificateCRL = "https://blog.itag.top";
26 | Map result = Pkcs12Util.createCert("123456", issuerStr, subjectStr, certificateCRL);
27 |
28 | FileOutputStream outPutStream = new FileOutputStream("src/main/resources/cert/keystore.p12"); // ca.jks
29 | outPutStream.write(result.get("keyStoreData"));
30 | outPutStream.close();
31 | FileOutputStream fos = new FileOutputStream(new File("src/main/resources/cert/keystore.cer"));
32 | fos.write(result.get("certificateData"));
33 | fos.flush();
34 | fos.close();
35 | }
36 | }
--------------------------------------------------------------------------------
/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/sealimg/公章.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/sealimg/公章.png
--------------------------------------------------------------------------------
/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/sealimg/私章.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bjlhx15/algorithm-sign/f8c2ee9bfe1cc751031ec88b63e88ea54b5258c8/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/sealimg/私章.png
--------------------------------------------------------------------------------
/eg001-seal-sign/src/test/java/com/github/bjlhx15/sign/eg001/seal/sign/SignPdfTest.java:
--------------------------------------------------------------------------------
1 | package com.github.bjlhx15.sign.eg001.seal.sign;
2 |
3 | import org.junit.Test;
4 |
5 | import java.io.File;
6 | import java.io.FileOutputStream;
7 | import java.io.IOException;
8 |
9 | import static org.junit.Assert.*;
10 |
11 | /**
12 | * @author lihongxu6
13 | * @version 1.0
14 | * @className SignPdfTest
15 | * @description TODO
16 | * @date 2021-01-25 22:10
17 | */
18 | public class SignPdfTest {
19 |
20 | @Test
21 | public void sign() throws Exception {
22 | byte[] fileData = SignPdf.sign("123456", "src/main/resources/cert/keystore.p12",
23 | "src/main/resources/book.pdf",
24 | "src/main/resources/sealimg/私章.png", 350, 500);
25 | FileOutputStream f = new FileOutputStream(new File("src/main/resources/signed.pdf"));
26 | f.write(fileData);
27 | f.close();
28 | }
29 | }
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.github.bjlhx15
6 | algorithm-sign
7 | 0.0.1-SNAPSHOT
8 | pom
9 |
10 | algorithm-sign
11 | http://maven.apache.org
12 |
13 |
14 |
15 | UTF-8
16 | UTF-8
17 | 1.8
18 | 1.8
19 | 1.8
20 |
21 |
22 |
23 | algorithm-sign-impl
24 | algorithm-sign-001-jasypt
25 | algorithm-sign-002-jasypt-springboot
26 | eg001-seal-sign
27 |
28 |
29 |
30 | commons-codec
31 | commons-codec
32 | 1.11
33 |
34 |
35 | org.apache.commons
36 | commons-lang3
37 | 3.8.1
38 |
39 |
40 | org.bouncycastle
41 | bcprov-jdk15on
42 | 1.58
43 |
44 |
45 | junit
46 | junit
47 | 4.13.1
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------