⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aes.java

📁 自己实现的AES和DES的加密算法。自己可以随意组成加密数据结构
💻 JAVA
字号:
package com.cmcc.common.util;

import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.cmcc.bean.EncryptedDataV2;

public class AES {

	private static byte[] iv = new byte[] { 85, 60, 12, 116, 99, -67, -83, 19,
			-118, -73, -24, -8, 82, -24, -56, -14 };

	public static byte[] decrypt(byte[] sSrc, String sKey) throws Exception {
		try {

			// 判断Key是否正确
			if (sKey == null) {
				return null;
			}
			// 判断Key是否为16位
			if (sKey.length() != 16) {

				return null;
			}

			Security.addProvider(new BouncyCastleProvider());
			Key key = new SecretKeySpec(sKey.getBytes("ASCII"), "AES");
			Cipher out = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
			out.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));

			return out.doFinal(sSrc);
		} catch (Exception ex) {
			return null;
		}
	}

	public static byte[] encrypt(byte[] sSrc, String sKey) throws Exception {
		if (sKey == null) {
			return null;
		}

		// 判断Key是否为16位
		if (sKey.length() != 16) {
			return null;
		}

		Security.addProvider(new BouncyCastleProvider());
		Key key = new SecretKeySpec(sKey.getBytes("ASCII"), "AES");

		Cipher in = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
		in.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
		byte[] enc = in.doFinal(sSrc);
		return enc;
	}

	public EncryptedDataV2 byte2date(byte buff[]) {
		EncryptedDataV2 encryptedDataV2 = new EncryptedDataV2();
		encryptedDataV2.setNonce(ByteOprs.getint(ByteOprs.subBuff(buff, 0, 4),
				0));
		encryptedDataV2.setCreateTime(ByteOprs.getlong(ByteOprs.subBuff(buff,
				4, 8), 0));
		encryptedDataV2.setExpireTime(ByteOprs.getlong(ByteOprs.subBuff(buff,
				12, 8), 0));
		encryptedDataV2.setUserTypeV2(ByteOprs.subBuff(buff, 20, 1)[0]);
		encryptedDataV2.setMobile(ByteOprs.getlong(ByteOprs
				.subBuff(buff, 21, 8), 0));
		encryptedDataV2.setSid(ByteOprs
				.getint(ByteOprs.subBuff(buff, 29, 4), 0));
		encryptedDataV2.setLogicalPoolId(ByteOprs.getshort(ByteOprs.subBuff(
				buff, 33, 2), 0));
		encryptedDataV2.setUserStatusV2(ByteOprs.getint(ByteOprs.subBuff(buff,
				35, 4), 0));
		encryptedDataV2.setUserIp(ByteOprs.getlong(ByteOprs
				.subBuff(buff, 39, 8), 0));
		return encryptedDataV2;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -