liusheng
2024-06-24 3153009257c97b0f0099f787e951dd15472d077e
ruoyi-admin/src/main/java/com/ruoyi/web/test/MQTest.java
@@ -1,13 +1,19 @@
package com.ruoyi.web.test;
import com.smartor.config.RabbitMqConfig;
import com.ruoyi.web.task.PhoneTask;
import org.junit.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.SpringVersion;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//@SpringBootTest
//@RunWith(SpringRunner.class)
@@ -17,14 +23,72 @@
    @Test
    public void testSend() {
        try {
            // 生成RSA密钥对
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(2048);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
        Pattern pattern = Pattern.compile("(?=.*(?:还行|挺好|可以|不错)).*$");
        Matcher matcher = pattern.matcher("还行吧,就是还有点疼");
        System.out.println(matcher.matches());
        System.out.println("Spring Framework版本:" + SpringVersion.getVersion());
//        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME, "phone.001", "测试数据001");
//        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME, "phone.002", "测试数据002");
//        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME, "phone.003", "测试数据003");
            // 获取公钥和私钥
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();
            String publicKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());
            System.out.println(publicKeyString);
            // 明文
            String plainText = "Hello, World!";
            // 加密
            Cipher encryptCipher = Cipher.getInstance("RSA");
            encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedBytes = encryptCipher.doFinal(plainText.getBytes());
            // 将加密后的数据转换为Base64编码的字符串
            String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
            System.out.println("加密后的数据:" + encryptedText);
            // 解密
            Cipher decryptCipher = Cipher.getInstance("RSA");
            decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decryptedBytes = decryptCipher.doFinal(Base64.getDecoder().decode(encryptedBytes));
            // 明文
            String decryptedText = new String(decryptedBytes);
            System.out.println("解密后的数据:" + decryptedText);
        } catch (Exception e) {
        }
//        String str = "absdf";
//        String c = Character.toUpperCase(str.charAt(0)) + str.substring(1);
//        System.out.println(c);
        "您好,我是浙二医院张医生,您是${name}吗?还是家属?".replaceAll("$$*variable", "AA");
//        Pattern pattern = Pattern.compile("^(?!.*(好|太好)).*$");
//        Matcher matcher = pattern.matcher("睡眠不好");
//        System.out.println(matcher.matches());
        // 创建固定大小的线程池
//        ExecutorService executorService = Executors.newFixedThreadPool(10);
//
//        executorService.submit(new PhoneTask());
    }
    @Test
    public void testSend22() {
        try {
            byte[] privateKeyBytes = Base64.getDecoder().decode("");
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
            // 使用私钥解密数据
            Cipher decryptCipher = Cipher.getInstance("RSA");
            decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decryptedBytes = decryptCipher.doFinal(Base64.getDecoder().decode(""));
            // 解密后的数据
            String decryptedData = new String(decryptedBytes);
            System.out.println("解密后的数据:" + decryptedData);
        } catch (Exception e) {
        }
    }
}