|
@@ -0,0 +1,192 @@
|
|
|
+package cn.com.qmth.examcloud.starters.crypto.test;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.starters.crypto.common.CryptoGroup;
|
|
|
+import cn.com.qmth.examcloud.starters.crypto.common.CryptoHelper;
|
|
|
+import cn.com.qmth.examcloud.starters.crypto.common.CryptoItem;
|
|
|
+import cn.com.qmth.examcloud.starters.crypto.common.FieldPair;
|
|
|
+import cn.com.qmth.examcloud.starters.crypto.service.CryptoFactory;
|
|
|
+import cn.com.qmth.examcloud.starters.crypto.utils.*;
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
+import org.apache.commons.codec.binary.Hex;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.logging.log4j.Level;
|
|
|
+import org.apache.logging.log4j.core.config.Configurator;
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.interfaces.RSAPrivateKey;
|
|
|
+import java.security.interfaces.RSAPublicKey;
|
|
|
+
|
|
|
+public class CryptoTest {
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void init() {
|
|
|
+ Configurator.setRootLevel(Level.ALL);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void demoTest() {
|
|
|
+ String str = "hello world, O(∩_∩)O哈哈~";
|
|
|
+
|
|
|
+ for (int n = 0; n < 1; n++) {
|
|
|
+ // str = str + "*";
|
|
|
+
|
|
|
+ // aesTest(str);
|
|
|
+ // desTest(str);
|
|
|
+ // tripleDesTest(str);
|
|
|
+ // rc4Test(str);
|
|
|
+ // rsaTest(str);
|
|
|
+ // otherTest(str);
|
|
|
+ cryptoGroupTest(str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void cryptoGroupTest(String str) {
|
|
|
+ String key = CryptoHelper.buildKey(
|
|
|
+ new FieldPair("key", "U_S_0_2"),
|
|
|
+ new FieldPair("token", "98ede2940ab84f448cb39568298b3d06"),
|
|
|
+ new FieldPair("timestamp", "1650364516803")
|
|
|
+ );
|
|
|
+
|
|
|
+ String combination = "ABCDE";
|
|
|
+ CryptoGroup cryptoGroup = new CryptoGroup(combination).matchKeys(key);
|
|
|
+ CryptoFactory cryptoFactory = new CryptoFactory();
|
|
|
+
|
|
|
+ // str = cryptoFactory.encrypt(cryptoGroup, str);
|
|
|
+ // System.out.println("加密结果:" + str);
|
|
|
+ // str = cryptoFactory.decrypt(cryptoGroup, str, true);
|
|
|
+ // System.out.println("解密结果:" + str);
|
|
|
+
|
|
|
+ System.out.println("\n---------- 按加密方案组合“顺序”依次加密 ----------");
|
|
|
+ for (CryptoItem item : cryptoGroup.getItems()) {
|
|
|
+ str = cryptoFactory.getCryptoService(item.getType()).encrypt(str, item.getEncryptKey());
|
|
|
+ System.out.println(String.format("%s-%s加密:%s\n", item.getType(), item.getType().getAlgorithm(), str));
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("\n---------- 按加密方案组合“倒序”依次解密 ----------");
|
|
|
+ for (CryptoItem item : cryptoGroup.getReverseItems()) {
|
|
|
+ str = cryptoFactory.getCryptoService(item.getType()).decrypt(str, item.getDecryptKey());
|
|
|
+ System.out.println(String.format("%s-%s解密:%s\n", item.getType(), item.getType().getAlgorithm(), str));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void aesTest(String str) {
|
|
|
+ System.out.println("---------- AES ----------");
|
|
|
+
|
|
|
+ String key = "1234567890ABCDEF1234567890ABCDEF";
|
|
|
+ byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
|
|
+ // byte[] keyBytes = AesUtil.randomKey();
|
|
|
+
|
|
|
+ System.out.println("密钥:" + Hex.encodeHexString(AesUtil.initSecretKey(keyBytes).getEncoded()));
|
|
|
+ System.out.println("偏移向量:" + Hex.encodeHexString(AesUtil.initIv(keyBytes).getIV()));
|
|
|
+
|
|
|
+ String encryptData = AesUtil.encrypt(str, keyBytes, true);
|
|
|
+ System.out.println("加密串:" + encryptData);
|
|
|
+ String decryptData = AesUtil.decrypt(encryptData, keyBytes, true);
|
|
|
+ System.out.println("解密串:" + decryptData);
|
|
|
+
|
|
|
+ System.out.println("---------- AES ----------");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void desTest(String str) {
|
|
|
+ System.out.println("---------- DES ----------");
|
|
|
+
|
|
|
+ String key = "12345678";
|
|
|
+ byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
|
|
+ // byte[] keyBytes = DesUtil.randomKey();
|
|
|
+
|
|
|
+ System.out.println("密钥:" + Hex.encodeHexString(DesUtil.initSecretKey(keyBytes).getEncoded()));
|
|
|
+ System.out.println("偏移向量:" + Hex.encodeHexString(DesUtil.initIv(keyBytes).getIV()));
|
|
|
+
|
|
|
+ String encryptData = DesUtil.encrypt(str, keyBytes, true);
|
|
|
+ System.out.println("加密串:" + encryptData);
|
|
|
+ String decryptData = DesUtil.decrypt(encryptData, keyBytes, true);
|
|
|
+ System.out.println("解密串:" + decryptData);
|
|
|
+
|
|
|
+ System.out.println("---------- DES ----------");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void tripleDesTest(String str) {
|
|
|
+ System.out.println("---------- TripleDes ----------");
|
|
|
+
|
|
|
+ String key = "1234567890ABCDEF12345678";
|
|
|
+ byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
|
|
+ // byte[] keyBytes = TripleDesUtil.randomKey();
|
|
|
+
|
|
|
+ System.out.println("密钥:" + Hex.encodeHexString(TripleDesUtil.initSecretKey(keyBytes).getEncoded()));
|
|
|
+ System.out.println("偏移向量:" + Hex.encodeHexString(TripleDesUtil.initIv(keyBytes).getIV()));
|
|
|
+
|
|
|
+ String encryptData = TripleDesUtil.encrypt(str, keyBytes, true);
|
|
|
+ System.out.println("加密串:" + encryptData);
|
|
|
+ String decryptData = TripleDesUtil.decrypt(encryptData, keyBytes, true);
|
|
|
+ System.out.println("解密串:" + decryptData);
|
|
|
+
|
|
|
+ System.out.println("---------- TripleDes ----------");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void rc4Test(String str) {
|
|
|
+ System.out.println("---------- RC4 ----------");
|
|
|
+
|
|
|
+ String key = "1234567890ABCDEF";
|
|
|
+ System.out.println("密钥:" + Hex.encodeHexString(Rc4Util.initSecretKey(key).getEncoded()));
|
|
|
+
|
|
|
+ String encryptData = Rc4Util.encrypt(str, key);
|
|
|
+ System.out.println("加密串:" + encryptData);
|
|
|
+ String decryptData = Rc4Util.decrypt(encryptData, key);
|
|
|
+ System.out.println("解密串:" + decryptData);
|
|
|
+
|
|
|
+ System.out.println("---------- RC4 ----------");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void rsaTest(String str) {
|
|
|
+ System.out.println("---------- RSA ----------");
|
|
|
+
|
|
|
+ // KeyPair keyPair = RsaUtil.randomKey();
|
|
|
+ // PublicKey publicKey = keyPair.getPublic();
|
|
|
+ // PrivateKey privateKey = keyPair.getPrivate();
|
|
|
+ // String publicKeyStr = Base64.encodeBase64String(publicKey.getEncoded());
|
|
|
+ // String privateKeyStr = Base64.encodeBase64String(privateKey.getEncoded());
|
|
|
+
|
|
|
+ System.out.println("公钥:" + publicKeyStr);
|
|
|
+ System.out.println("私钥:" + privateKeyStr);
|
|
|
+
|
|
|
+ RSAPublicKey rsaPublicKey = RsaUtil.getPublicKey(publicKeyStr);
|
|
|
+ RSAPrivateKey rsaPrivateKey = RsaUtil.getPrivateKey(privateKeyStr);
|
|
|
+
|
|
|
+ String encryptData1 = RsaUtil.publicEncrypt(str, rsaPublicKey);
|
|
|
+ System.out.println("公钥加密串:" + encryptData1);
|
|
|
+ String decryptData1 = RsaUtil.privateDecrypt(encryptData1, rsaPrivateKey);
|
|
|
+ System.out.println("私钥解密串: " + decryptData1);
|
|
|
+
|
|
|
+ String encryptData2 = RsaUtil.privateEncrypt(str, rsaPrivateKey);
|
|
|
+ System.out.println("私钥加密串:" + encryptData2);
|
|
|
+ String decryptData2 = RsaUtil.publicDecrypt(encryptData2, rsaPublicKey);
|
|
|
+ System.out.println("公钥解密串: " + decryptData2);
|
|
|
+
|
|
|
+ String signStr = RsaUtil.sign(str, rsaPrivateKey);
|
|
|
+ System.out.println("私钥签名结果:" + signStr);
|
|
|
+ boolean signVerifyResult = RsaUtil.verifySign(str, signStr, rsaPublicKey);
|
|
|
+ System.out.println("公钥验证签名结果:" + signVerifyResult);
|
|
|
+
|
|
|
+ System.out.println("---------- RSA ----------");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void otherTest(String str) {
|
|
|
+ System.out.println("---------- Base64 ----------");
|
|
|
+ String encode = Base64.encodeBase64String(str.getBytes(StandardCharsets.UTF_8));
|
|
|
+ System.out.println("Base64 编码: " + encode);
|
|
|
+ byte[] decode = Base64.decodeBase64(encode);
|
|
|
+ System.out.println("Base64 解码: " + new String(decode, StandardCharsets.UTF_8));
|
|
|
+ System.out.println("---------- Base64 ----------");
|
|
|
+
|
|
|
+ System.out.println("MD5: " + DigestUtils.md5Hex(str));
|
|
|
+ System.out.println("SHA1: " + DigestUtils.sha1Hex(str));
|
|
|
+ System.out.println("SHA256: " + DigestUtils.sha256Hex(str));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDKWMG6iQOZpDBtLxk0qzFHwi+627vUcitGMqthtEMjLZvHbDovtRHpnNs1y9f0PO7LNZOE8LBY3CdWZaQfTg1pPJqV4DD0p4HicR0/5mbX+qqSTYqCbxF4s2nuIAgH2Z1rzlteRvbK6QxNZ+6UxtIqEdpIibBHPapimpwe6optRQIDAQAB";
|
|
|
+
|
|
|
+ private static final String privateKeyStr = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAMpYwbqJA5mkMG0vGTSrMUfCL7rbu9RyK0Yyq2G0QyMtm8dsOi+1Eemc2zXL1/Q87ss1k4TwsFjcJ1ZlpB9ODWk8mpXgMPSngeJ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