├── .gitignore ├── README.md ├── SECURITY.md ├── algorithm-sign-001-jasypt ├── .gitignore ├── pom.xml ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── github │ │ │ └── bjlhx15 │ │ │ └── security │ │ │ └── jasypt │ │ │ ├── JasyptUtils.java │ │ │ └── RC128TextEncryptor.java │ └── test │ │ └── java │ │ └── com │ │ └── github │ │ └── bjlhx15 │ │ └── security │ │ └── jasypt │ │ ├── AES256TextEncryptorTest.java │ │ ├── BasicPasswordEncryptorTest.java │ │ ├── BasicTextEncryptorTest.java │ │ ├── JasyptUtilsTest.java │ │ ├── RC128TextEncryptorTest.java │ │ ├── StandardPBEStringEncryptorTest.java │ │ └── StrongTextEncryptorTest.java └── 说明.md ├── algorithm-sign-002-jasypt-springboot ├── .gitignore ├── pom.xml ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── bjlhx15 │ │ │ └── security │ │ │ └── jasypt │ │ │ ├── SpringRunnerMain.java │ │ │ ├── configuration │ │ │ └── JasyptConfig.java │ │ │ └── controller │ │ │ └── TestController.java │ │ └── resources │ │ ├── application.properties │ │ └── security.properties └── 说明.md ├── algorithm-sign-impl ├── .gitignore ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── bjlhx15 │ │ ├── cert │ │ ├── CertificateCoder.java │ │ └── CertificateCoderTest.java │ │ └── security │ │ ├── asymmetric001rsa │ │ └── JdkRsa.java │ │ ├── asymmetric002ecc │ │ ├── BcEcc.java │ │ └── JdkEcc.java │ │ ├── base001base64 │ │ ├── BouncyCastleBase64.java │ │ ├── CommonsCodecBase64.java │ │ ├── Jdk8Base64.java │ │ └── SunJdkBase64.java │ │ ├── base002base58 │ │ └── Base58.java │ │ ├── base003md5AndSha │ │ ├── BcMD5AndSha.java │ │ └── JdkMD5AndSha.java │ │ ├── base004hmac │ │ └── HmacUtils.java │ │ ├── base005ripemd │ │ ├── HmacRipeMDCoder.java │ │ └── RipeMDCoder.java │ │ ├── base006others │ │ └── BcExtHashUtil.java │ │ ├── base007crc │ │ └── CrcUtil.java │ │ ├── cert │ │ └── CertificateCoder.java │ │ ├── changekey001DH │ │ ├── AbstractDHCoder.java │ │ ├── BcDHCoder.java │ │ ├── JdkDHCoder.java │ │ └── 注意 │ │ ├── changekey002ECDH │ │ ├── BcECDHCoder.java │ │ └── 注意 │ │ ├── changekey003DigitalEnvelopeDH │ │ └── DigitalEnvelope.java │ │ ├── encryptSign001BcEcc │ │ ├── AbstractBcEccAlgorithmUtil.java │ │ ├── BcEccAlgorithmDHUtil.java │ │ └── BcEccAlgorithmUtil.java │ │ ├── sign001rsa │ │ └── JdkRsaSign.java │ │ ├── sign002dsa │ │ └── JdkDsa.java │ │ ├── sign003ecc │ │ └── JdkEcdsa.java │ │ ├── sm │ │ ├── BcSm2Util.java │ │ ├── BcSm3Util.java │ │ ├── BcSm4Util.java │ │ └── KeyUtils.java │ │ └── symmetric │ │ ├── des3DesAesBlowfishRC2RC4 │ │ ├── AbstractSymmetric.java │ │ ├── BcSymmetric.java │ │ └── JdkSymmetric.java │ │ └── pbe │ │ ├── AbstractSymmetric.java │ │ ├── BcPbe.java │ │ └── JdkPbe.java │ └── test │ └── java │ └── com │ └── github │ └── bjlhx15 │ └── security │ ├── CtorStatic.java │ ├── CtorStaticTest.java │ ├── asymmetric001rsa │ └── JdkRsaTest.java │ ├── asymmetric002ecc │ ├── BcEccTest.java │ └── JdkEccTest.java │ ├── base001base64 │ ├── BouncyCastleBase64Test.java │ ├── CommonsCodecBase64Test.java │ ├── Jdk8Base64Test.java │ └── SunJdkBase64Test.java │ ├── base002base58 │ └── Base58Test.java │ ├── base003md5AndSha │ ├── BcMD5AndShaTest.java │ └── JdkMD5AndShaTest.java │ ├── base004hmac │ ├── HmacMD5UtilsTest.java │ └── HmacUtilsTest.java │ ├── base005ripemd │ ├── HmacRipeMDCoderTest.java │ └── RipeMDCoderTest.java │ ├── base006others │ └── BcExtHashUtilTest.java │ ├── base007crc │ └── CrcUtilTest.java │ ├── changekey001DH │ ├── BcDHCoderTest.java │ └── JdkDHCoderTest.java │ ├── changekey002ECDH │ └── BcECDHCoderTest.java │ ├── changekey003DigitalEnvelopeDH │ └── DigitalEnvelopeTest.java │ ├── encryptSign001BcEcc │ ├── BcEccAlgorithmDHUtilTest.java │ └── BcEccAlgorithmUtilTest.java │ ├── sign001rsa │ └── JdkRsaSignTest.java │ ├── sign002dsa │ └── JdkDsaTest.java │ ├── sign003ecc │ └── JdkEcdsaTest.java │ ├── sm │ ├── BcSm2UtilTest.java │ ├── BcSm3UtilTest.java │ └── BcSm4UtilTest.java │ └── symmetric │ ├── des3DesAesBlowfishRC2RC4 │ ├── BcSymmetricDesTest.java │ ├── BcSymmetricTest.java │ ├── JdkSymmetric3DesTest.java │ ├── JdkSymmetricAesTest.java │ ├── JdkSymmetricBlowfishTest.java │ ├── JdkSymmetricDesTest.java │ ├── JdkSymmetricRC2Test.java │ └── JdkSymmetricRC4Test.java │ └── pbe │ ├── BcPbeTest.java │ └── JdkPbeTest.java ├── eg001-seal-sign ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── bjlhx15 │ │ │ └── sign │ │ │ └── eg001 │ │ │ └── seal │ │ │ ├── DemoApplication.java │ │ │ ├── doc │ │ │ ├── WordToPdf.java │ │ │ └── WordUtil.java │ │ │ ├── imgcompress │ │ │ └── CompressImg.java │ │ │ ├── pdf │ │ │ └── PdfUtil.java │ │ │ ├── pkcs12 │ │ │ ├── Extension.java │ │ │ └── Pkcs12Util.java │ │ │ ├── sealimg │ │ │ ├── SealUtil.java │ │ │ └── conf │ │ │ │ ├── SealCircle.java │ │ │ │ ├── SealConfiguration.java │ │ │ │ └── SealFont.java │ │ │ └── sign │ │ │ └── SignPdf.java │ └── resources │ │ ├── WechatIMG2.jpeg │ │ ├── WechatIMG2_new.jpeg │ │ ├── application.properties │ │ ├── book.docx │ │ ├── book.pdf │ │ ├── book2.docx │ │ ├── cert │ │ ├── keystore.cer │ │ └── keystore.p12 │ │ ├── img │ │ ├── WechatIMG_+90.jpg │ │ ├── WechatIMG_-90.jpg │ │ ├── WechatIMG_110%.jpg │ │ ├── WechatIMG_120x120.jpg │ │ ├── WechatIMG_1280x1024.gif │ │ ├── WechatIMG_1280x1024.png │ │ ├── WechatIMG_1280x1024_BufferedImage.jpg │ │ ├── WechatIMG_1280x1024_OutputStream.png │ │ ├── WechatIMG_200x300.jpg │ │ ├── WechatIMG_25%.jpg │ │ ├── WechatIMG_2560x2048.jpg │ │ ├── WechatIMG_region_bootom_right.jpg │ │ ├── WechatIMG_region_center.jpg │ │ ├── WechatIMG_region_coord.jpg │ │ ├── WechatIMG_watermark_bottom_right.jpg │ │ └── WechatIMG_watermark_center.jpg │ │ ├── log4j2-spring-dev.xml │ │ ├── sealimg │ │ ├── 公章.png │ │ └── 私章.png │ │ ├── signed.pdf │ │ ├── spring-config.xml │ │ ├── test-Org.jpg │ │ ├── test-cp1.jpg │ │ ├── tpl.docx │ │ ├── tpl.pdf │ │ ├── tpl2.docx │ │ └── watermark.png │ └── test │ └── java │ └── com │ └── github │ └── bjlhx15 │ └── sign │ └── eg001 │ └── seal │ ├── doc │ ├── WordToPdfTest.java │ └── WordUntils2Test.java │ ├── imgcompress │ ├── CompressImgTest.java │ └── ThumbnailsTest.java │ ├── pdf │ └── PdfUtilTest.java │ ├── pkcs12 │ └── Pkcs12UtilTest.java │ ├── sealimg │ ├── SealUtilTest.java │ ├── 公章.png │ └── 私章.png │ └── sign │ └── SignPdfTest.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /bin/ 27 | target 28 | /target 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # algorithm-sign 2 | 3 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Use this section to tell people about which versions of your project are 6 | currently being supported with security updates. 7 | 8 | | Version | Supported | 9 | | ------- | ------------------ | 10 | | 5.1.x | :white_check_mark: | 11 | | 5.0.x | :x: | 12 | | 4.0.x | :white_check_mark: | 13 | | < 4.0 | :x: | 14 | 15 | ## Reporting a Vulnerability 16 | 17 | Use this section to tell people how to report a vulnerability. 18 | 19 | Tell them where to go, how often they can expect to get an update on a 20 | reported vulnerability, what to expect if the vulnerability is accepted or 21 | declined, etc. 22 | -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /bin/ 27 | -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.bjlhx15 7 | algorithm-sign-001-jasypt 8 | 0.0.13 9 | 4.0.0 10 | jar 11 | 12 | 13 | UTF-8 14 | UTF-8 15 | 1.8 16 | 1.8 17 | 1.8 18 | 19 | 20 | 21 | 22 | org.jasypt 23 | jasypt 24 | 1.9.3 25 | 26 | 27 | org.slf4j 28 | slf4j-api 29 | 1.7.25 30 | 31 | 32 | 33 | 34 | junit 35 | junit 36 | RELEASE 37 | 38 | 39 | 40 | 41 | 42 | 43 | src/main/resources 44 | true 45 | 46 | 47 | 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-resources-plugin 52 | 2.7 53 | 54 | UTF-8 55 | 56 | md 57 | 58 | 59 | 60 | 61 | 62 | 63 | org.apache.maven.plugins 64 | maven-compiler-plugin 65 | 3.0 66 | 67 | 1.8 68 | 1.8 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/src/main/java/com/github/bjlhx15/security/jasypt/JasyptUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.jasypt; 2 | 3 | import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; 4 | import org.jasypt.encryption.pbe.StandardPBEByteEncryptor; 5 | import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; 6 | 7 | public class JasyptUtils { 8 | /** 9 | * Jasypt生成加密结果 10 | * 11 | * @param password 配置文件中设定的加密密码 jasypt.encryptor.password 12 | * @param value 待加密值 13 | * @return 14 | */ 15 | public static String encryptPwd(String password, String value) { 16 | PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor(); 17 | encryptOr.setConfig(cryptOr(password)); 18 | String result = encryptOr.encrypt(value); 19 | return result; 20 | } 21 | 22 | /** 23 | * 解密 24 | * 25 | * @param password 配置文件中设定的加密密码 jasypt.encryptor.password 26 | * @param value 待解密密文 27 | * @return 28 | */ 29 | public static String decyptPwd(String password, String value) { 30 | PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor(); 31 | encryptOr.setConfig(cryptOr(password)); 32 | String result = encryptOr.decrypt(value); 33 | return result; 34 | } 35 | 36 | /** 37 | * @param password salt 38 | * @return 39 | */ 40 | public static SimpleStringPBEConfig cryptOr(String password) { 41 | SimpleStringPBEConfig config = new SimpleStringPBEConfig(); 42 | config.setPassword(password); 43 | config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM); 44 | config.setKeyObtentionIterations("1000"); 45 | config.setPoolSize("1"); 46 | config.setProviderName(null); 47 | config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); 48 | config.setStringOutputType("base64"); 49 | return config; 50 | } 51 | 52 | public static void main(String[] args) { 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/src/main/java/com/github/bjlhx15/security/jasypt/RC128TextEncryptor.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.jasypt; 2 | 3 | import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; 4 | import org.jasypt.util.text.TextEncryptor; 5 | 6 | public class RC128TextEncryptor implements TextEncryptor { 7 | private final StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); 8 | 9 | public RC128TextEncryptor() { 10 | this.encryptor.setAlgorithm("PBEWITHSHA1ANDRC4_128"); 11 | } 12 | 13 | public void setPassword(String password) { 14 | this.encryptor.setPassword(password); 15 | } 16 | 17 | public void setPasswordCharArray(char[] password) { 18 | this.encryptor.setPasswordCharArray(password); 19 | } 20 | 21 | public String encrypt(String message) { 22 | return this.encryptor.encrypt(message); 23 | } 24 | 25 | public String decrypt(String encryptedMessage) { 26 | return this.encryptor.decrypt(encryptedMessage); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/src/test/java/com/github/bjlhx15/security/jasypt/AES256TextEncryptorTest.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.common.help.jasypt; 2 | 3 | import org.jasypt.util.text.AES256TextEncryptor; 4 | import org.jasypt.util.text.StrongTextEncryptor; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | public class AES256TextEncryptorTest { 9 | 10 | AES256TextEncryptor textEncryptor; 11 | 12 | @Before 13 | public void setUp() { 14 | textEncryptor = new AES256TextEncryptor(); 15 | textEncryptor.setPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7"); 16 | } 17 | 18 | @Test 19 | public void encrypt() { 20 | // 加密 21 | System.out.println(textEncryptor.encrypt("root@1234")); 22 | //n6T6UM9+hLKg6QMl+r9snFvXULR3YAK2rhqQpxiAh+f0dkXVhcP6ak/Soy+9gAs8 23 | } 24 | 25 | @Test 26 | public void decyptPwd() { 27 | // 解密 28 | // root@1234 29 | System.out.println(textEncryptor.decrypt("n6T6UM9+hLKg6QMl+r9snFvXULR3YAK2rhqQpxiAh+f0dkXVhcP6ak/Soy+9gAs8")); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/src/test/java/com/github/bjlhx15/security/jasypt/BasicPasswordEncryptorTest.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.common.help.jasypt; 2 | 3 | import org.jasypt.util.password.BasicPasswordEncryptor; 4 | import org.jasypt.util.text.BasicTextEncryptor; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | public class BasicPasswordEncryptorTest { 9 | 10 | @Before 11 | public void setUp() { 12 | // textEncryptor = new BasicPasswordEncryptor(); 13 | } 14 | 15 | @Test 16 | public void encrypt() { 17 | BasicPasswordEncryptor textEncryptor = new BasicPasswordEncryptor(); 18 | String encryptPassword = textEncryptor.encryptPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7"); 19 | System.out.println(encryptPassword); 20 | boolean checkPassword = textEncryptor.checkPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7", encryptPassword); 21 | System.out.println(checkPassword); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/src/test/java/com/github/bjlhx15/security/jasypt/BasicTextEncryptorTest.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.common.help.jasypt; 2 | 3 | import org.jasypt.util.text.BasicTextEncryptor; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | public class BasicTextEncryptorTest { 8 | 9 | BasicTextEncryptor textEncryptor; 10 | 11 | @Before 12 | public void setUp() { 13 | textEncryptor = new BasicTextEncryptor(); 14 | textEncryptor.setPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7"); 15 | } 16 | 17 | @Test 18 | public void encrypt() { 19 | // 加密 20 | System.out.println(textEncryptor.encrypt("root@1234")); 21 | //TJetNWzmC4os1CCb+gHtz+5MpL9NFMML 22 | //KCTSu/Dv1elE1A/ZyppCHgJAAwKiez/p 23 | } 24 | 25 | @Test 26 | public void decyptPwd() { 27 | // 解密 28 | // root@1234 29 | System.out.println(textEncryptor.decrypt("TJetNWzmC4os1CCb+gHtz+5MpL9NFMML")); 30 | 31 | // root@1234 32 | System.out.println(textEncryptor.decrypt("KCTSu/Dv1elE1A/ZyppCHgJAAwKiez/p")); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/src/test/java/com/github/bjlhx15/security/jasypt/JasyptUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.common.help.jasypt; 2 | 3 | import org.jasypt.util.text.StrongTextEncryptor; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class JasyptUtilsTest { 9 | 10 | @Test 11 | public void encryptPwd() { 12 | // 加密 13 | System.out.println(JasyptUtils.encryptPwd("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7", "root@1234")); 14 | //TJetNWzmC4os1CCb+gHtz+5MpL9NFMML 15 | //KCTSu/Dv1elE1A/ZyppCHgJAAwKiez/p 16 | 17 | // StrongTextEncryptor 18 | } 19 | 20 | @Test 21 | public void decyptPwd() { 22 | 23 | // 解密 24 | // root@1234 25 | System.out.println(JasyptUtils.decyptPwd("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7", "TJetNWzmC4os1CCb+gHtz+5MpL9NFMML")); 26 | 27 | // root@1234 28 | System.out.println(JasyptUtils.decyptPwd("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7", "KCTSu/Dv1elE1A/ZyppCHgJAAwKiez/p")); 29 | } 30 | } -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/src/test/java/com/github/bjlhx15/security/jasypt/RC128TextEncryptorTest.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.common.help.jasypt; 2 | 3 | import org.jasypt.util.text.BasicTextEncryptor; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | public class RC128TextEncryptorTest { 10 | 11 | RC128TextEncryptor textEncryptor; 12 | 13 | @Before 14 | public void setUp() { 15 | textEncryptor = new RC128TextEncryptor(); 16 | textEncryptor.setPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7"); 17 | } 18 | 19 | @Test 20 | public void encrypt() { 21 | // 加密 22 | System.out.println(textEncryptor.encrypt("root@1234")); 23 | //zjhmIP38jmvob56qyNevHjs= 24 | //iMX2aR70CkLGdtlAdhe2XKI= 25 | } 26 | 27 | @Test 28 | public void decyptPwd() { 29 | // 解密 30 | // root@1234 31 | System.out.println(textEncryptor.decrypt("zjhmIP38jmvob56qyNevHjs=")); 32 | 33 | // root@1234 34 | System.out.println(textEncryptor.decrypt("iMX2aR70CkLGdtlAdhe2XKI=")); 35 | } 36 | } -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/src/test/java/com/github/bjlhx15/security/jasypt/StandardPBEStringEncryptorTest.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.common.help.jasypt; 2 | 3 | import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; 4 | import org.jasypt.util.text.StrongTextEncryptor; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | public class StandardPBEStringEncryptorTest { 9 | StandardPBEStringEncryptor textEncryptor; 10 | 11 | @Before 12 | public void setUp() { 13 | textEncryptor = new StandardPBEStringEncryptor(); 14 | // textEncryptor.setAlgorithm("");//自行指定 15 | textEncryptor.setPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7"); 16 | } 17 | 18 | @Test 19 | public void encrypt() { 20 | // 加密 21 | System.out.println(textEncryptor.encrypt("root@1234")); 22 | //Han0rFt6K2jhvrK5swPpD/ctoUMPckIO 23 | //upkr4Rc6bhmpUXhdRoT9qqkhiSfEhTvS 24 | } 25 | 26 | @Test 27 | public void decyptPwd() { 28 | // 解密 29 | // root@1234 30 | System.out.println(textEncryptor.decrypt("Han0rFt6K2jhvrK5swPpD/ctoUMPckIO")); 31 | // root@1234 32 | System.out.println(textEncryptor.decrypt("upkr4Rc6bhmpUXhdRoT9qqkhiSfEhTvS")); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/src/test/java/com/github/bjlhx15/security/jasypt/StrongTextEncryptorTest.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.common.help.jasypt; 2 | 3 | import org.jasypt.util.text.BasicTextEncryptor; 4 | import org.jasypt.util.text.StrongTextEncryptor; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | public class StrongTextEncryptorTest { 9 | 10 | StrongTextEncryptor textEncryptor; 11 | 12 | @Before 13 | public void setUp() { 14 | textEncryptor = new StrongTextEncryptor(); 15 | textEncryptor.setPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7"); 16 | } 17 | 18 | @Test 19 | public void encrypt() { 20 | // 加密 21 | System.out.println(textEncryptor.encrypt("root@1234")); 22 | //KU6h0PmBBJ2mZa1zq3PkaXmhmxnTT9pL 23 | //DOiGEcatVuYitcWFkdS5MzeA2W3ZttN0 24 | } 25 | 26 | @Test 27 | public void decyptPwd() { 28 | 29 | // 解密 30 | // root@1234 31 | System.out.println(textEncryptor.decrypt("KU6h0PmBBJ2mZa1zq3PkaXmhmxnTT9pL")); 32 | 33 | // root@1234 34 | System.out.println(textEncryptor.decrypt("DOiGEcatVuYitcWFkdS5MzeA2W3ZttN0")); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /algorithm-sign-001-jasypt/说明.md: -------------------------------------------------------------------------------- 1 | 一、简介 2 | Java Simplified Encryption 3 | jasypt由于其使用的是PBEWithMD5AndDES加密方式,所以每次加密出来的结果都不一样,所以很适合对数据进行加密 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /algorithm-sign-002-jasypt-springboot/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /bin/ 27 | -------------------------------------------------------------------------------- /algorithm-sign-002-jasypt-springboot/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.github.bjlhx15 7 | algorithm-sign-002-jasypt-springboot 8 | 0.0.13 9 | 4.0.0 10 | jar 11 | 12 | 13 | UTF-8 14 | UTF-8 15 | 1.8 16 | 1.8 17 | 1.8 18 | 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-dependencies 25 | 2.0.2.RELEASE 26 | pom 27 | import 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-autoconfigure 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-web 42 | 43 | 44 | com.github.ulisesbocchio 45 | jasypt-spring-boot-starter 46 | 3.0.1 47 | 48 | 49 | junit 50 | junit 51 | RELEASE 52 | 53 | 54 | 55 | 56 | 57 | 58 | src/main/resources 59 | true 60 | 61 | 62 | 63 | 64 | 65 | org.springframework.boot 66 | spring-boot-maven-plugin 67 | 68 | 69 | 70 | org.apache.maven.plugins 71 | maven-resources-plugin 72 | 2.7 73 | 74 | UTF-8 75 | 76 | md 77 | 78 | 79 | 80 | 81 | 82 | 83 | org.apache.maven.plugins 84 | maven-compiler-plugin 85 | 3.0 86 | 87 | 1.8 88 | 1.8 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /algorithm-sign-002-jasypt-springboot/src/main/java/com/github/bjlhx15/security/jasypt/SpringRunnerMain.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.jasypt; 2 | 3 | import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.annotation.PropertySource; 7 | 8 | @SpringBootApplication 9 | @EnableEncryptableProperties 10 | @PropertySource(value = {"classpath:security.properties"},ignoreResourceNotFound = false) 11 | public class SpringRunnerMain { 12 | public static void main(String[] args) { 13 | SpringApplication.run(SpringRunnerMain.class, args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /algorithm-sign-002-jasypt-springboot/src/main/java/com/github/bjlhx15/security/jasypt/configuration/JasyptConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.jasypt.configuration; 2 | 3 | import org.jasypt.encryption.StringEncryptor; 4 | import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; 5 | import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | @Configuration 10 | public class JasyptConfig { 11 | 12 | @Bean("jasyptStringEncryptor") 13 | public StringEncryptor stringEncryptor() { 14 | PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); 15 | SimpleStringPBEConfig config = new SimpleStringPBEConfig(); 16 | config.setPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7"); 17 | // config.setAlgorithm("PBEWithMD5AndDES");//默认配置 18 | // config.setKeyObtentionIterations("1000");//默认配置 19 | config.setPoolSize("4"); 20 | // config.setProviderName("SunJCE");//默认配置 21 | // config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");//默认配置 22 | // config.setStringOutputType("base64");//默认配置 23 | encryptor.setConfig(config); 24 | return encryptor; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /algorithm-sign-002-jasypt-springboot/src/main/java/com/github/bjlhx15/security/jasypt/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.jasypt.controller; 2 | 3 | 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import java.util.Enumeration; 9 | import java.util.Map; 10 | import java.util.Properties; 11 | import java.util.Set; 12 | 13 | @RestController 14 | public class TestController { 15 | 16 | @GetMapping("test") 17 | public Object testProperties(@Value("${jdbc.password}") String pwd, @Value("${spring.datasource.password}") String jbbc) { 18 | System.out.println(pwd); 19 | System.out.println(jbbc); 20 | Properties properties = System.getProperties(); 21 | Set objects = properties.keySet(); 22 | for (Object object : objects) { 23 | System.out.println("key:" + object + "---:" + properties.get(object)); 24 | } 25 | 26 | Map getenv = System.getenv(); 27 | for (Map.Entry entry : getenv.entrySet()) { 28 | System.out.println(entry.getKey() + "---:" + entry.getValue()); 29 | } 30 | return ""; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /algorithm-sign-002-jasypt-springboot/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | #jdbc.password=ENC(TJetNWzmC4os1CCb+gHtz+5MpL9NFMML) 3 | 4 | spring.datasource.url=jdbc:mysql://localhost:3306/abc?useSSL=false 5 | spring.datasource.username=root 6 | spring.datasource.password=${jdbc.password} -------------------------------------------------------------------------------- /algorithm-sign-002-jasypt-springboot/src/main/resources/security.properties: -------------------------------------------------------------------------------- 1 | 2 | 3 | jdbc.password=ENC(TJetNWzmC4os1CCb+gHtz+5MpL9NFMML) -------------------------------------------------------------------------------- /algorithm-sign-002-jasypt-springboot/说明.md: -------------------------------------------------------------------------------- 1 | 一、简介 2 | Java Simplified Encryption 3 | jasypt由于其使用的是PBEWithMD5AndDES加密方式,所以每次加密出来的结果都不一样,所以很适合对数据进行加密 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /algorithm-sign-impl/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /algorithm-sign-impl/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | algorithm-sign-impl 6 | jar 7 | 8 | algorithm-sign 9 | http://maven.apache.org 10 | 11 | 12 | com.alibaba 13 | fastjson 14 | 1.2.70 15 | test 16 | 17 | 18 | 19 | 20 | com.github.bjlhx15 21 | algorithm-sign 22 | 0.0.1-SNAPSHOT 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/cert/CertificateCoderTest.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.cert; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class CertificateCoderTest { 7 | private String password = "123456"; 8 | private String alias = "www.lhx.org"; 9 | private String certificatePath = "d:/lhx.cer"; 10 | private String keyStorePath = "d:/lhx.keystore"; 11 | 12 | @Test 13 | public void test() throws Exception { 14 | System.err.println("公钥加密——私钥解密"); 15 | String inputStr = "Ceritifcate"; 16 | byte[] data = inputStr.getBytes(); 17 | 18 | byte[] encrypt = CertificateCoder.encryptByPublicKey(data, this.certificatePath); 19 | 20 | byte[] decrypt = CertificateCoder.decryptByPrivateKey(encrypt, this.keyStorePath, this.alias, this.password); 21 | String outputStr = new String(decrypt); 22 | 23 | System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr); 24 | 25 | // 验证数据一致 26 | Assert.assertArrayEquals(data, decrypt); 27 | 28 | // 验证证书有效 29 | Assert.assertTrue(CertificateCoder.verifyCertificate(this.certificatePath)); 30 | 31 | } 32 | 33 | @Test 34 | public void testSign() throws Exception { 35 | System.err.println("私钥加密——公钥解密"); 36 | 37 | String inputStr = "sign"; 38 | byte[] data = inputStr.getBytes(); 39 | 40 | byte[] encodedData = CertificateCoder.encryptByPrivateKey(data, this.keyStorePath, this.alias, this.password); 41 | 42 | byte[] decodedData = CertificateCoder.decryptByPublicKey(encodedData, this.certificatePath); 43 | 44 | String outputStr = new String(decodedData); 45 | System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr); 46 | Assert.assertEquals(inputStr, outputStr); 47 | 48 | System.err.println("私钥签名——公钥验证签名"); 49 | // 产生签名 50 | String sign = CertificateCoder.sign(encodedData, this.keyStorePath, this.alias, this.password); 51 | System.err.println("签名:\r" + sign); 52 | 53 | // 验证签名 54 | boolean status = CertificateCoder.verify(encodedData, sign, this.certificatePath); 55 | System.err.println("状态:\r" + status); 56 | Assert.assertTrue(status); 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/asymmetric001rsa/JdkRsa.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.asymmetric001rsa; 2 | 3 | import org.apache.commons.codec.binary.Base64; 4 | 5 | import javax.crypto.Cipher; 6 | import java.security.*; 7 | import java.security.interfaces.RSAPrivateKey; 8 | import java.security.interfaces.RSAPublicKey; 9 | import java.security.spec.PKCS8EncodedKeySpec; 10 | import java.security.spec.X509EncodedKeySpec; 11 | import java.util.AbstractMap; 12 | import java.util.Map; 13 | 14 | public class JdkRsa { 15 | public final static String RSA = "RSA"; 16 | 17 | /** 18 | * // 1.初始化密钥 19 | * 20 | * @param algorithm 21 | * @param keySize 22 | * @return 23 | * @throws NoSuchAlgorithmException 24 | */ 25 | public static Map.Entry initKeyPair(String algorithm, Integer keySize) throws NoSuchAlgorithmException { 26 | KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm); 27 | keyPairGenerator.initialize(keySize); 28 | KeyPair keyPair = keyPairGenerator.generateKeyPair(); 29 | RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); 30 | RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); 31 | return new AbstractMap.SimpleEntry<>(rsaPublicKey.getEncoded(), rsaPrivateKey.getEncoded()); 32 | } 33 | 34 | /** 35 | * 公钥加密、私钥解密——加密 36 | * 37 | * @param algorithm 38 | * @param pubKey 39 | * @param data 40 | * @return 41 | * @throws Exception 42 | */ 43 | public static byte[] pubKeyEncrypt(String algorithm, byte[] pubKey, byte[] data) throws Exception { 44 | X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey); 45 | KeyFactory keyFactory = KeyFactory.getInstance(algorithm); 46 | PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); 47 | Cipher cipher = Cipher.getInstance(algorithm); 48 | cipher.init(Cipher.ENCRYPT_MODE, publicKey); 49 | return cipher.doFinal(data); 50 | } 51 | 52 | /** 53 | * 公钥加密、私钥解密——加密 54 | * 55 | * @param algorithm 56 | * @param priKey 57 | * @param data 58 | * @return 59 | * @throws Exception 60 | */ 61 | public static byte[] priKeyDecrypt(String algorithm, byte[] priKey, byte[] data) throws Exception { 62 | PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(priKey); 63 | KeyFactory keyFactory = KeyFactory.getInstance(algorithm); 64 | PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); 65 | Cipher cipher = Cipher.getInstance(algorithm); 66 | cipher.init(Cipher.DECRYPT_MODE, privateKey); 67 | return cipher.doFinal(data); 68 | } 69 | 70 | /** 71 | * 私钥加密、公钥解密——加密 72 | * 73 | * @param algorithm 74 | * @param priKey 75 | * @param data 76 | * @return 77 | * @throws Exception 78 | */ 79 | public static byte[] priKeyEncrypt(String algorithm, byte[] priKey, byte[] data) throws Exception { 80 | PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(priKey); 81 | KeyFactory keyFactory = KeyFactory.getInstance(algorithm); 82 | PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); 83 | Cipher cipher = Cipher.getInstance(algorithm); 84 | cipher.init(Cipher.ENCRYPT_MODE, privateKey); 85 | return cipher.doFinal(data); 86 | } 87 | 88 | 89 | /** 90 | * 私钥加密、公钥解密——解密 91 | * 92 | * @param algorithm 93 | * @param pubKey 94 | * @param data 95 | * @return 96 | * @throws Exception 97 | */ 98 | public static byte[] pubKeyDecrypt(String algorithm, byte[] pubKey, byte[] data) throws Exception { 99 | X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey); 100 | KeyFactory keyFactory = KeyFactory.getInstance(algorithm); 101 | PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); 102 | Cipher cipher = Cipher.getInstance(algorithm); 103 | cipher.init(Cipher.DECRYPT_MODE, publicKey); 104 | return cipher.doFinal(data); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/asymmetric002ecc/BcEcc.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.asymmetric002ecc; 2 | 3 | import javax.crypto.BadPaddingException; 4 | import javax.crypto.Cipher; 5 | import javax.crypto.IllegalBlockSizeException; 6 | import javax.crypto.NoSuchPaddingException; 7 | import java.security.*; 8 | import java.security.interfaces.ECPrivateKey; 9 | import java.security.interfaces.ECPublicKey; 10 | import java.security.interfaces.RSAPrivateKey; 11 | import java.security.interfaces.RSAPublicKey; 12 | import java.util.AbstractMap; 13 | import java.util.Map; 14 | 15 | public class BcEcc { 16 | public static KeyPair initKeyPair(String algorithm, Integer keySize) throws Exception { 17 | Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 18 | KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC","BC"); 19 | keyPairGenerator.initialize(keySize, new SecureRandom()); 20 | KeyPair keyPair = keyPairGenerator.generateKeyPair(); 21 | return keyPair; 22 | } 23 | 24 | public static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception { 25 | Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 26 | Cipher cipher = Cipher.getInstance("ECIES","BC");//写不写 BC都可以,都是会选择BC实现来做 27 | cipher.init(Cipher.ENCRYPT_MODE, publicKey); 28 | return cipher.doFinal(content); 29 | } 30 | 31 | public static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception { 32 | Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 33 | Cipher cipher = Cipher.getInstance("ECIES","BC"); 34 | cipher.init(Cipher.DECRYPT_MODE, privateKey); 35 | return cipher.doFinal(content); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/asymmetric002ecc/JdkEcc.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.asymmetric002ecc; 2 | 3 | import javax.crypto.Cipher; 4 | import java.security.*; 5 | 6 | /** 7 | * 8 | */ 9 | public class JdkEcc { 10 | public static KeyPair initKeyPair(String algorithm, Integer keySize) throws Exception { 11 | KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC"); 12 | keyPairGenerator.initialize(keySize, new SecureRandom()); 13 | 14 | KeyPair keyPair = keyPairGenerator.generateKeyPair(); 15 | return keyPair; 16 | } 17 | 18 | public static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception { 19 | throw new RuntimeException("暂不支持"); 20 | } 21 | 22 | public static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception { 23 | throw new RuntimeException("暂不支持"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/base001base64/BouncyCastleBase64.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.base001base64; 2 | 3 | import org.bouncycastle.util.encoders.Base64; 4 | 5 | public class BouncyCastleBase64 { 6 | 7 | public String base64Encoder(String msg, String charsetName) throws Exception { 8 | return new String(Base64.encode(msg.getBytes(charsetName)), charsetName); 9 | } 10 | 11 | public String base64Decoder(String msg, String charsetName) throws Exception { 12 | return new String(Base64.decode(msg), charsetName); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/base001base64/CommonsCodecBase64.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.base001base64; 2 | 3 | import org.apache.commons.codec.binary.Base64; 4 | 5 | public class CommonsCodecBase64 { 6 | 7 | public String base64Encoder(String encodeMsg, String charsetName) throws Exception { 8 | byte[] base64 = Base64.encodeBase64(encodeMsg.getBytes(charsetName)); 9 | return new String(base64); 10 | } 11 | 12 | public String base64Decoder(String base64String, String charsetName) throws Exception { 13 | byte[] base64 = Base64.decodeBase64(base64String); 14 | return new String(base64, charsetName); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/base001base64/Jdk8Base64.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.base001base64; 2 | 3 | import java.util.Base64; 4 | 5 | public class Jdk8Base64 { 6 | 7 | public String base64Encoder(String encodeMsg, String charsetName) throws Exception { 8 | //默认 ISO-8859-1 9 | return Base64.getEncoder().encodeToString(encodeMsg.getBytes(charsetName)); 10 | } 11 | 12 | public String base64Decoder(String decodeMsg, String charsetName) throws Exception { 13 | return new String(Base64.getDecoder().decode(decodeMsg), charsetName); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/base001base64/SunJdkBase64.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.base001base64; 2 | 3 | import org.junit.Test; 4 | import sun.misc.BASE64Decoder; 5 | import sun.misc.BASE64Encoder; 6 | 7 | @SuppressWarnings("restriction") 8 | public class SunJdkBase64 { 9 | 10 | public String base64Encoder(String encodeMsg, String charsetName) throws Exception { 11 | // JDK里sun.misc套件下的BASE64Encoder和BASE64Decoder这两个类 12 | return new BASE64Encoder().encode(encodeMsg.getBytes(charsetName)); 13 | } 14 | 15 | public String base64Decoder(String decodeMsg, String charsetName) throws Exception { 16 | // JDK里sun.misc套件下的BASE64Encoder和BASE64Decoder这两个类 17 | return new String(new BASE64Decoder().decodeBuffer(decodeMsg), charsetName); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/base003md5AndSha/BcMD5AndSha.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.base003md5AndSha; 2 | 3 | import org.apache.commons.codec.binary.Hex; 4 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 5 | 6 | import java.security.MessageDigest; 7 | import java.security.Security; 8 | 9 | public class BcMD5AndSha { 10 | public static final String MD2 = "MD2"; 11 | public static final String MD3 = "MD3"; 12 | public static final String MD4 = "MD4"; 13 | public static final String MD5 = "MD5"; 14 | public static final String SHA1 = "SHA-1"; 15 | public static final String SHA224 = "SHA-224"; 16 | public static final String SHA256 = "SHA-256"; 17 | public static final String SHA384 = "SHA-384"; 18 | public static final String SHA512 = "SHA-512"; 19 | 20 | public static String msgSafeBase(String msg, String algorithmName) throws Exception { 21 | 22 | // 注册BouncyCastle: 23 | Security.addProvider(new BouncyCastleProvider()); 24 | 25 | MessageDigest m = MessageDigest.getInstance(algorithmName); 26 | m.update(msg.getBytes("UTF8")); 27 | byte s[] = m.digest(); 28 | return Hex.encodeHexString(s); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/base003md5AndSha/JdkMD5AndSha.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.base003md5AndSha; 2 | 3 | import org.apache.commons.codec.binary.Hex; 4 | 5 | import java.security.MessageDigest; 6 | 7 | public class JdkMD5AndSha { 8 | public static final String MD2 = "MD2"; 9 | public static final String MD3 = "MD3"; 10 | public static final String MD4 = "MD4"; 11 | public static final String MD5 = "MD5"; 12 | public static final String SHA1 = "SHA-1"; 13 | public static final String SHA224 = "SHA-224"; 14 | public static final String SHA256 = "SHA-256"; 15 | public static final String SHA384 = "SHA-384"; 16 | public static final String SHA512 = "SHA-512"; 17 | 18 | public static String msgSafeBase(String msg, String algorithmName) throws Exception { 19 | MessageDigest m = MessageDigest.getInstance(algorithmName); 20 | m.update(msg.getBytes("UTF8")); 21 | byte s[] = m.digest(); 22 | return Hex.encodeHexString(s); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/base004hmac/HmacUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.base004hmac; 2 | 3 | import org.apache.commons.codec.binary.Hex; 4 | 5 | import javax.crypto.KeyGenerator; 6 | import javax.crypto.Mac; 7 | import javax.crypto.SecretKey; 8 | import javax.crypto.spec.SecretKeySpec; 9 | import java.util.Base64; 10 | 11 | public class HmacUtils { 12 | public static final String HmacMD5 = "HmacMD5"; 13 | public static final String HmacSHA1 = "HmacSHA1"; 14 | public static final String HmacSHA224 = "HmacSHA224"; 15 | public static final String HmacSHA256 = "HmacSHA256"; 16 | public static final String HmacSHA384 = "HmacSHA384"; 17 | public static final String HmacSHA512 = "HmacSHA512"; 18 | 19 | public static String initMacKey(String algorithm) throws Exception { 20 | // HmacMD5,HmacSHA1,HmacSHA256,HmacSHA384,HmacSHA512 21 | KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);// 初始化KeyGenerator 22 | SecretKey secretKey = keyGenerator.generateKey();// 产生密钥 23 | return Base64.getEncoder().encodeToString(secretKey.getEncoded());// 获得密钥 24 | } 25 | 26 | public static String hashMsgCode(String algorithm, String key, byte[] data) throws Exception { 27 | // 还原密钥 28 | SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), algorithm); 29 | Mac mac = Mac.getInstance(secretKey.getAlgorithm());// 实例化MAC 30 | mac.init(secretKey);// 初始化Mac 31 | return new String(Hex.encodeHex(mac.doFinal(data)));// 执行摘要 32 | } 33 | 34 | public static boolean check(String algorithm, String key, String hash, String data) throws Exception { 35 | String hash2 = hashMsgCode(algorithm, key, data.getBytes("utf-8")); 36 | return hash.equals(hash2); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/base005ripemd/HmacRipeMDCoder.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.base005ripemd; 2 | 3 | import org.apache.commons.codec.binary.Hex; 4 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 5 | 6 | import javax.crypto.KeyGenerator; 7 | import javax.crypto.Mac; 8 | import javax.crypto.SecretKey; 9 | import javax.crypto.spec.SecretKeySpec; 10 | import java.security.Security; 11 | import java.util.Base64; 12 | 13 | /** 14 | * HmacRipeMD128,HmacRipeMD160 15 | * 16 | * @author Administrator 17 | */ 18 | public class HmacRipeMDCoder { 19 | public static final String HmacRipeMD128 = "HmacRipeMD128"; 20 | public static final String HmacRipeMD160 = "HmacRipeMD160"; 21 | 22 | /** 23 | * 初始化HmacRipeMD128的密钥 24 | * 25 | * @return byte[] 密钥 26 | */ 27 | public static String initHmacRipeMDKey(String algorithm) throws Exception { 28 | // 加入BouncyCastleProvider的支持 29 | Security.addProvider(new BouncyCastleProvider()); 30 | // 初始化KeyGenerator 31 | KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm); 32 | // 产生密钥 33 | SecretKey secretKey = keyGenerator.generateKey(); 34 | // 获取密钥 35 | return Base64.getEncoder().encodeToString(secretKey.getEncoded()); 36 | } 37 | 38 | /** 39 | * HmacRipeMD128消息摘要 40 | * 41 | * @param data 待做摘要处理的数据 42 | * @param key 密钥 43 | * @return byte[] 消息摘要 44 | */ 45 | public static byte[] encodeHmacRipeMD(String algorithm, String key, byte[] data) throws Exception { 46 | // 加入BouncyCastleProvider的支持 47 | Security.addProvider(new BouncyCastleProvider()); 48 | // 还原密钥,因为密钥是以byte形式为消息传递算法所拥有 49 | SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), algorithm); 50 | // 实例化Mac 51 | Mac mac = Mac.getInstance(secretKey.getAlgorithm()); 52 | // 初始化Mac 53 | mac.init(secretKey); 54 | // 执行消息摘要处理 55 | return mac.doFinal(data); 56 | } 57 | 58 | /** 59 | * HmacRipeMDHex消息摘要 60 | * 61 | * @param data 待做消息摘要处理的数据 62 | * @param key 密钥 63 | * @return byte[] 消息摘要 64 | */ 65 | public static String encodeHmacRipeMDHex(String algorithm, String key, byte[] data) throws Exception { 66 | // 执行消息摘要处理 67 | byte[] b = HmacRipeMDCoder.encodeHmacRipeMD(algorithm, key, data); 68 | // 做十六进制转换 69 | return new String(Hex.encodeHex(b)); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/base005ripemd/RipeMDCoder.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.base005ripemd; 2 | 3 | import org.apache.commons.codec.binary.Hex; 4 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 5 | 6 | import java.security.MessageDigest; 7 | import java.security.Security; 8 | 9 | /** 10 | * RipeMD128,RipeMD160,RipeMD256,RipeMD320 11 | * 12 | * @author Administrator 13 | */ 14 | public class RipeMDCoder { 15 | public static final String RipeMD128 = "RipeMD128"; 16 | public static final String RipeMD160 = "RipeMD160"; 17 | public static final String RipeMD256 = "RipeMD256"; 18 | public static final String RipeMD320 = "RipeMD320"; 19 | 20 | /** 21 | * RipeMD消息摘要 22 | * 23 | * @param data 待处理的消息摘要数据 24 | * @return byte[] 消息摘要 25 | */ 26 | public static byte[] encodeRipeMD(String algorithm, byte[] data) throws Exception { 27 | // 加入BouncyCastleProvider的支持 28 | Security.addProvider(new BouncyCastleProvider()); 29 | // 初始化MessageDigest 30 | MessageDigest md = MessageDigest.getInstance(algorithm); 31 | // 执行消息摘要 32 | return md.digest(data); 33 | } 34 | 35 | /** 36 | * RipeMDHex消息摘要 37 | * 38 | * @param data 待处理的消息摘要数据 39 | * @return String 消息摘要 40 | **/ 41 | public static String encodeRipeMDHex(String algorithm, byte[] data) throws Exception { 42 | // 执行消息摘要 43 | byte[] b = RipeMDCoder.encodeRipeMD(algorithm, data); 44 | // 做十六进制的编码处理 45 | return new String(Hex.encodeHex(b)); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/base006others/BcExtHashUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.base006others; 2 | 3 | import com.github.bjlhx15.security.base005ripemd.RipeMDCoder; 4 | import org.apache.commons.codec.binary.Hex; 5 | import org.bouncycastle.jcajce.provider.digest.Whirlpool; 6 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 7 | 8 | import java.security.MessageDigest; 9 | import java.security.Security; 10 | 11 | public class BcExtHashUtil { 12 | public static final String RipeMD128 = "RipeMD128"; 13 | public static final String RipeMD160 = "RipeMD160"; 14 | public static final String RipeMD256 = "RipeMD256"; 15 | public static final String RipeMD320 = "RipeMD320"; 16 | 17 | public static final String Tiger = "Tiger"; 18 | public static final String Whirlpool = "Whirlpool"; 19 | public static final String Gost3411 = "Gost3411"; 20 | 21 | 22 | /** 23 | * RipeMD消息摘要 24 | * 25 | * @param data 待处理的消息摘要数据 26 | * @return byte[] 消息摘要 27 | */ 28 | public static byte[] encodeExtHash(String algorithm, byte[] data) throws Exception { 29 | // 加入BouncyCastleProvider的支持 30 | Security.addProvider(new BouncyCastleProvider()); 31 | // 初始化MessageDigest 32 | MessageDigest md = MessageDigest.getInstance(algorithm); 33 | // 执行消息摘要 34 | return md.digest(data); 35 | } 36 | 37 | /** 38 | * RipeMDHex消息摘要 39 | * 40 | * @param data 待处理的消息摘要数据 41 | * @return String 消息摘要 42 | **/ 43 | public static String encodeExtHashHex(String algorithm, byte[] data) throws Exception { 44 | // 执行消息摘要 45 | byte[] b = RipeMDCoder.encodeRipeMD(algorithm, data); 46 | // 做十六进制的编码处理 47 | return new String(Hex.encodeHex(b)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/changekey001DH/BcDHCoder.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.changekey001DH; 2 | 3 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 4 | 5 | import javax.crypto.Cipher; 6 | import javax.crypto.KeyAgreement; 7 | import javax.crypto.NoSuchPaddingException; 8 | import javax.crypto.SecretKey; 9 | import javax.crypto.interfaces.DHPrivateKey; 10 | import javax.crypto.interfaces.DHPublicKey; 11 | import javax.crypto.spec.DHParameterSpec; 12 | import java.security.*; 13 | import java.security.spec.PKCS8EncodedKeySpec; 14 | import java.security.spec.X509EncodedKeySpec; 15 | import java.util.AbstractMap; 16 | import java.util.Map; 17 | 18 | /** 19 | * DH密码交换协议
20 | * DH加密流程:
21 | * 1.初始化DH算法密钥对:
22 | * 1.1.发送方—>构建发送方密钥-》公布发送方密钥给接收方-》使用接收者公钥构建发送方本地密钥
23 | * 1.2.接收方—》使用发送方密钥密钥构建接收方密钥-》公布接收者公钥给发送方—》构建接收方本地密钥
24 | * 2.DH算法加密消息传递:
25 | * 2.1.发送方—>使用本地密钥加密消息—》发送加密消息给接收方
26 | * 2.2.接收方—》使用本地密钥解密消息
27 | * 28 | * @author 木子旭 29 | * @version %I%,%G% 30 | * @since 2017年3月17日上午9:18:14 31 | */ 32 | public class BcDHCoder extends AbstractDHCoder { 33 | @Override 34 | public void encryptBefore(String secretAlgorithm) { 35 | 36 | Security.addProvider(new BouncyCastleProvider()); 37 | } 38 | 39 | @Override 40 | public Cipher getCipher(String secretAlgorithm) throws Exception { 41 | return Cipher.getInstance(secretAlgorithm,"BC"); 42 | } 43 | 44 | @Override 45 | public void decryptBefore(String secretAlgorithm) { 46 | Security.addProvider(new BouncyCastleProvider()); 47 | 48 | } 49 | 50 | @Override 51 | public void getSecretKeyBefore(String secretAlgorithm) { 52 | Security.addProvider(new BouncyCastleProvider()); 53 | 54 | } 55 | } -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/changekey001DH/JdkDHCoder.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.changekey001DH; 2 | 3 | import org.apache.commons.codec.binary.Base64; 4 | 5 | import javax.crypto.Cipher; 6 | import javax.crypto.KeyAgreement; 7 | import javax.crypto.SecretKey; 8 | import javax.crypto.interfaces.DHPrivateKey; 9 | import javax.crypto.interfaces.DHPublicKey; 10 | import javax.crypto.spec.DHParameterSpec; 11 | import java.security.*; 12 | import java.security.spec.PKCS8EncodedKeySpec; 13 | import java.security.spec.X509EncodedKeySpec; 14 | import java.util.AbstractMap; 15 | import java.util.HashMap; 16 | import java.util.Map; 17 | 18 | /** 19 | * DH密码交换协议
20 | * DH加密流程:
21 | * 1.初始化DH算法密钥对:
22 | * 1.1.发送方—>构建发送方密钥-》公布发送方密钥给接收方-》使用接收者公钥构建发送方本地密钥
23 | * 1.2.接收方—》使用发送方密钥密钥构建接收方密钥-》公布接收者公钥给发送方—》构建接收方本地密钥
24 | * 2.DH算法加密消息传递:
25 | * 2.1.发送方—>使用本地密钥加密消息—》发送加密消息给接收方
26 | * 2.2.接收方—》使用本地密钥解密消息
27 | * 28 | * @author 木子旭 29 | * @version %I%,%G% 30 | * @since 2017年3月17日上午9:18:14 31 | */ 32 | public class JdkDHCoder extends AbstractDHCoder { 33 | 34 | @Override 35 | public void encryptBefore(String secretAlgorithm) { 36 | 37 | } 38 | 39 | @Override 40 | public Cipher getCipher(String secretAlgorithm) throws Exception { 41 | return Cipher.getInstance(secretAlgorithm); 42 | } 43 | 44 | @Override 45 | public void decryptBefore(String secretAlgorithm) { 46 | 47 | } 48 | 49 | @Override 50 | public void getSecretKeyBefore(String secretAlgorithm) { 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/changekey001DH/注意: -------------------------------------------------------------------------------- 1 |   由于JDK版本不同,在Java 8 update 161版本以后就会出现此问题,根本原因还是DH密钥长度至少为512位,而DES算法密钥没有这么长,密钥长度不一致引起的。 2 | 3 |   解决方法:配置JVM的系统变量:-Djdk.crypto.KeyAgreement.legacyKDF=true -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/changekey002ECDH/注意: -------------------------------------------------------------------------------- 1 |   由于JDK版本不同,在Java 8 update 161版本以后就会出现此问题,根本原因还是DH密钥长度至少为512位,而DES算法密钥没有这么长,密钥长度不一致引起的。 2 | 3 |   解决方法:配置JVM的系统变量:-Djdk.crypto.KeyAgreement.legacyKDF=true -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/changekey003DigitalEnvelopeDH/DigitalEnvelope.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.changekey003DigitalEnvelopeDH; 2 | 3 | import javax.crypto.Cipher; 4 | import javax.crypto.KeyGenerator; 5 | import javax.crypto.SecretKey; 6 | import javax.crypto.SecretKeyFactory; 7 | import javax.crypto.spec.DESKeySpec; 8 | import java.security.*; 9 | import java.security.spec.KeySpec; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | import java.util.concurrent.BlockingQueue; 13 | import java.util.concurrent.LinkedBlockingQueue; 14 | 15 | public final class DigitalEnvelope { 16 | 17 | /** Client端与Server端交换数据的队列, 模拟两者之间通讯的通道, 现实中两者可能是通过Socket通讯的. */ 18 | private static final BlockingQueue> CHANNEL = new LinkedBlockingQueue<>(1); 19 | /** 20 | * 生成RSA算法的公私密钥对. 21 | * 22 | * @return 生成RSA算法的公私密钥对. 23 | */ 24 | public static final KeyPair generatorKeyPair() { 25 | KeyPairGenerator keyGen = null; 26 | try { 27 | keyGen = KeyPairGenerator.getInstance("RSA"); 28 | } catch (NoSuchAlgorithmException e) { 29 | throw new RuntimeException(e); 30 | } 31 | SecureRandom random = null; 32 | try { 33 | random = SecureRandom.getInstance("SHA1PRNG"); 34 | } catch (Exception e) { 35 | throw new RuntimeException(e); 36 | } 37 | random.setSeed(53); 38 | keyGen.initialize(1024, random); 39 | KeyPair pair = keyGen.generateKeyPair(); 40 | return pair; 41 | } 42 | 43 | /** 44 | * 模拟密钥交换的服务器端, 服务器端与客户端通过共享内存来交换Digital Envelope. 45 | */ 46 | static class Server extends Thread { 47 | 48 | /** 49 | * 实际中有可能是客户端在请求服务器端时上送了自己的公钥, 也有可能是在注册 50 | * 时就在服务器端登记了公钥. 51 | * 52 | * @param clientPublicKey 客户端的公钥. 53 | */ 54 | public Server(PublicKey clientPublicKey) { 55 | this.clientPublicKey = clientPublicKey; 56 | } 57 | 58 | /* (non-Javadoc) 59 | * @see java.lang.Thread#run() 60 | */ 61 | @Override 62 | public void run() { 63 | try { 64 | String msg = "Legend of AK47"; 65 | KeyGenerator generator = KeyGenerator.getInstance("DES"); 66 | SecretKey sessionKey = generator.generateKey(); 67 | // Key sessionKey = KeyGeneratorDemo.generatePlainDES(); 68 | Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 69 | cipher.init(Cipher.ENCRYPT_MODE, sessionKey); 70 | byte[] p = msg.getBytes("UTF-8"); 71 | byte[] msgCipher = cipher.doFinal(p); 72 | cipher = Cipher.getInstance("RSA"); 73 | cipher.init(Cipher.ENCRYPT_MODE, clientPublicKey); 74 | byte[] keyCipher = cipher.doFinal(sessionKey.getEncoded()); 75 | Map result = new HashMap(); 76 | result.put("msg", msgCipher); 77 | result.put("key", keyCipher); 78 | CHANNEL.offer(result); 79 | } catch (Exception e) { 80 | // TODO Exception handling... 81 | } 82 | } 83 | 84 | /** 客户端的公钥. */ 85 | private final PublicKey clientPublicKey; 86 | 87 | } 88 | 89 | /** 90 | * 模拟密钥交换的客户端, 服务器端与客户端通过共享内存来交换Digital Envelope. 91 | * 92 | * @author Rich, 2012-6-14. 93 | * @version 1.0 94 | * @since 1.0 95 | */ 96 | static class Client extends Thread { 97 | 98 | /** 99 | * 密钥对应该在客户端内部产生, 然后客户端在请求服务器端时上送了自己的公钥, 也有可能是在注册时就在服务器端登记了公钥. 100 | * 101 | * @param keyPair 客户端的公私密钥对. 102 | */ 103 | public Client(KeyPair keyPair) { 104 | this.keyPair = keyPair; 105 | } 106 | @Override 107 | public void run() { 108 | try { 109 | Map received = CHANNEL.take(); 110 | byte[] msgCipher = received.get("msg"); 111 | byte[] keyCipher = received.get("key"); 112 | PrivateKey privateKey = keyPair.getPrivate(); 113 | Cipher cipher = Cipher.getInstance("RSA"); 114 | cipher.init(Cipher.DECRYPT_MODE, privateKey); 115 | byte[] encoded = cipher.doFinal(keyCipher); 116 | KeySpec keySpec = new DESKeySpec(encoded); 117 | SecretKeyFactory fac = SecretKeyFactory.getInstance("DES"); 118 | Key key = fac.generateSecret(keySpec); 119 | cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 120 | cipher.init(Cipher.DECRYPT_MODE, key); 121 | byte[] msg = cipher.doFinal(msgCipher); 122 | String plainText = new String(msg, "UTF-8"); 123 | System.out.println(plainText); 124 | } catch (Exception e) { 125 | // TODO Exception handling... 126 | } 127 | } 128 | /** 客户端的公私密钥对. */ 129 | private final KeyPair keyPair; 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /algorithm-sign-impl/src/main/java/com/github/bjlhx15/security/encryptSign001BcEcc/BcEccAlgorithmUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.bjlhx15.security.encryptSign001BcEcc; 2 | 3 | import java.util.Map; 4 | 5 | public class BcEccAlgorithmUtil extends AbstractBcEccAlgorithmUtil{ 6 | 7 | public static Map.Entry 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 | --------------------------------------------------------------------------------