You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					
					
						
							71 lines
						
					
					
						
							3.0 KiB
						
					
					
				
			
		
		
	
	
							71 lines
						
					
					
						
							3.0 KiB
						
					
					
				package com.ynxbd.common.helper.common;
 | 
						|
 | 
						|
 | 
						|
import lombok.extern.slf4j.Slf4j;
 | 
						|
import org.apache.commons.codec.binary.Hex;
 | 
						|
 | 
						|
import javax.crypto.Cipher;
 | 
						|
import javax.crypto.spec.IvParameterSpec;
 | 
						|
import javax.crypto.spec.SecretKeySpec;
 | 
						|
import java.nio.charset.StandardCharsets;
 | 
						|
 | 
						|
/**
 | 
						|
 * AES加密算法
 | 
						|
 */
 | 
						|
@Slf4j
 | 
						|
public class AesHelper {
 | 
						|
 | 
						|
    public static String decryptHex(String data, String key, String iv) {
 | 
						|
        try {
 | 
						|
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
 | 
						|
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
 | 
						|
            cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)));
 | 
						|
 | 
						|
            return new String(cipher.doFinal(Hex.decodeHex(data.toCharArray())));
 | 
						|
        } catch (Exception e) {
 | 
						|
            log.warn(e.getMessage());
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Aes加密算法( Hex)
 | 
						|
     *
 | 
						|
     * @param data 加密数据
 | 
						|
     * @param key  密钥(16位)
 | 
						|
     * @param iv   密钥偏移量
 | 
						|
     * @return 加密后的数据
 | 
						|
     */
 | 
						|
    public static String encryptHex(String data, String key, String iv) {
 | 
						|
        try {
 | 
						|
            if (key == null || key.length() != 16) {
 | 
						|
                throw new Exception("Key长度不是16位");
 | 
						|
            }
 | 
						|
 | 
						|
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //"算法/模式/补码方式"
 | 
						|
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"), new IvParameterSpec(iv.getBytes()));
 | 
						|
            byte[] encrypted = cipher.doFinal(data.getBytes());
 | 
						|
            return Hex.encodeHexString(encrypted).toUpperCase();
 | 
						|
        } catch (Exception e) {
 | 
						|
            log.warn(e.getMessage());
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public static void main(String[] args) throws Exception {
 | 
						|
        //PACS 那边的 加解密密钥 key:cqmygysdssjtwmyd   iv:llyscysykryhsbjc
 | 
						|
 | 
						|
//        long beg = System.currentTimeMillis();
 | 
						|
//        String encrypt = encryptHex("文字123", "wsqysqsdssjtwmyd", "xbzynydykkxxkkll");
 | 
						|
//        System.out.println(encrypt);
 | 
						|
//        long end = System.currentTimeMillis();
 | 
						|
//
 | 
						|
//        System.out.println(end - beg);
 | 
						|
//        String a = decryptHex("111123", "wsqysqsdssjtwmyd", "xbzynydykkxxkkll");
 | 
						|
//        System.out.println(a);
 | 
						|
 | 
						|
 | 
						|
        String encrypt = decryptHex("A95C0ACF523B396D48857B85B82D3A47CA3DC877FEAAE4C6AB1A462C0F18D6BA537CE71511A651344B566B9CC65A06E04977EA167558E29B3AB2E7CF1ED993C62CA454E9566133712B92344B64C88B5B9260C287EA3712F6314F02738AD888C864271129DD5D83119509F2380ECACF0A2B8202C3F367711123FEADC6AEEE24A11AF79816F33E9A64DF10DDB0E2070B8204A8BC0D70C162515C1EA6FF69CABEED3ABFC3609B0EBC36FF8BA7868BE9B6767F23076876BB549829370DBD98DE52BD14E015F53F3E44574EA2F80B30B14C27414994DC852528BDC711F42379E0399C62A1983627A0765E7B51AB880B10BAE824C9A7F6DE096040A24D3C8ABD394E32C04EB78E8F05D95A122B447D91B65FCB738E41291F088FDD259DFE6B8E94D51F", "cqmygysdssjtwmyd", "llyscysykryhsbjc");
 | 
						|
        System.out.println(encrypt);
 | 
						|
    }
 | 
						|
} |