TestAesEncryptUtils.java 3.07 KB
package com.glsx.smart.service;

import cn.com.glsx.SmartServiceApplication;
import cn.com.glsx.smart.common.utils.AesEncryptUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.security.SecureRandom;
import java.util.Random;

/**
 * @author fengzhi
 * @version 1.0.0
 * @description: 车钥匙AES加密解密测试
 * @createTime 2021年05月08日 15:58:00
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SmartServiceApplication.class)
@Slf4j
public class TestAesEncryptUtils {
    @Autowired
    private AesEncryptUtils aesEncryptUtils;

    @Test
    public void testEncryptCrep() {
        String encrypt = null;
        try {
            encrypt = aesEncryptUtils.encrypt("370342948446986240110");
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("crep加密后:{}",encrypt);
    }

    @Test
    public void testDecryptCrep() {
        String decrypt = null;
        try {
            decrypt = aesEncryptUtils.decrypt("tZvuOJsVI6B8x5Yx3CBOH2MZn/3YhKbUtiVlkP9c+iU=");
        } catch (Exception e) {
            e.printStackTrace();
        }

        log.info("crep解密后:{}",decrypt);
    }

    @Test
    public void testEncryptSecretKey() {
        String encrypt = null;
        try {
            encrypt = aesEncryptUtils.encryptSecret("37061517396835532890027292");
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("SecretKey加密后:{}",encrypt);
    }

    @Test
    public void testDecryptSecretKey() {
        String decrypt = null;
        try {
            decrypt = aesEncryptUtils.decryptSecret("pZz3WfhsWOJSxpky6q8r84EHTSL/rmlkHhcNeWR9bys=");
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("SecretKey解密后:{}",decrypt);
    }

    @Test
    public void testUUID() {
        StringBuilder uid = new StringBuilder();
        //产生16位的强随机数
        Random rd = new SecureRandom();
        for (int i = 0; i < 16; i++) {
            //产生0-2的3位随机数
            int type = rd.nextInt(3);
            switch (type) {
                case 0:
                    //0-9的随机数
                    uid.append(rd.nextInt(10));
         /*int random = ThreadLocalRandom.current().ints(0, 10)
         .distinct().limit(1).findFirst().getAsInt();*/
                    break;
                case 1:
                    //ASCII在65-90之间为大写,获取大写随机
                    uid.append((char) (rd.nextInt(25) + 65));
                    break;
                case 2:
                    //ASCII在97-122之间为小写,获取小写随机
                    uid.append((char) (rd.nextInt(25) + 97));
                    break;
                default:
                    break;
            }
        }
        String uuid =  uid.toString();
        log.info("UUID:{}",uuid);
    }
}