Java utiliza cifrado y descifrado AES

package com.lzw.aes;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/**
 * 此处使用AES-128-ECB加密模式,key需要为16位。
 * @author lzw
 * @Date 2019年10月18日
 */
public class Aes {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		//密钥
		String cKey = "1234567890defghi";
		// 需要加密的字串
		String cSrc = "Abc123";
		System.out.println(cSrc);
		// 加密
		String enString = Aes.Encrypt(cSrc, cKey);
		System.out.println("加密后的字串是:" + enString);

		// 解密
		String DeString = Aes.Decrypt(enString, cKey);
		System.out.println("解密后的字串是:" + DeString);

	}

	/**
	 * 二进制转换成十六进制
	 * 
	 * @param buf
	 * @return
	 */
	private static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 	加密
	 * @param sSrc  待加密的密码
	 * @param sKey	解密密钥
	 * @return
	 * @throws Exception
	 */
    public static String Encrypt(String sSrc, String sKey) {
    	
		try {
			byte[] raw = sKey.getBytes("ASCII");
		    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		    Cipher cipher  = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		    byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
			return parseByte2HexStr(encrypted);
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
      return null;
    }

	/**
	 * 解密
	 * 
	 * @param sSrc
	 *            待解密的密码
	 * @param sKey
	 *            解密密钥
	 * @return
	 * @throws Exception
	 */
	public static String Decrypt(String sSrc, String sKey) {
		try {
			byte[] raw = sKey.getBytes("utf-8");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			byte[] encrypted = parseHexStr2Byte(sSrc);
			byte[] original = cipher.doFinal(encrypted);
			return new String(original, "utf-8");
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 十六进制转换成二进制
	 * 
	 * @param hexStr
	 * @return
	 */
	private static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}

}

 

Supongo que te gusta

Origin blog.csdn.net/weixin_42228950/article/details/102625674
Recomendado
Clasificación