您的位置:首页 > 编程语言 > Java开发

java AES128加密压缩 模拟传输数据

2016-04-21 14:28 369 查看
package test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

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

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
*
* @author andy.wang
* @时间 2016-4-21
*/
public class AES128 {
private static final Logger log = LogManager
.getLogger(AES128.class);
/**
* 秘钥长度
*/
private static final int SECURE_KEY_LENGTH = 16;
/**
* 采用AES128解密
*
* @param data
* @param secureKey
* @return
* @throws Exception
*             ,Exception
* @throws Exception
*/
public static byte[] decrypt(byte[] data, String secureKey)
throws Exception {
if (data == null || data.length == 0) {
return data;
}

// 获得密匙数据
byte[] rawKeyData = getAESKey(secureKey); // secureKey.getBytes();
// 从原始密匙数据创建一个KeySpec对象
SecretKeySpec key = new SecretKeySpec(rawKeyData, "AES");
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = new byte[SECURE_KEY_LENGTH];
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
try {
return cipher.doFinal(data);
} catch (Exception e) {
//			log.error(e.getMessage());
//			throw new Exception("ILLEGAL_SIGNATURE");
e.printStackTrace();
throw new Exception(e);
}

}

public static byte[] getAESKey(String key)
throws UnsupportedEncodingException {
byte[] keyBytes;
keyBytes = key.getBytes("UTF-8");
// Use the first 16 bytes (or even less if key is shorter)
byte[] keyBytes16 = new byte[SECURE_KEY_LENGTH];
System.arraycopy(keyBytes, 0, keyBytes16, 0,
Math.min(keyBytes.length, SECURE_KEY_LENGTH));
return keyBytes16;
}

/**
* 采用AES128加密
*
* @param text
* @param k
* @return
*/
public static byte[] encrypt(byte[] data, String secureKey)
throws Exception {
if (data == null || data.length == 0) {
return data;
}

// 获得密匙数据
byte[] rawKeyData = getAESKey(secureKey);
// 从原始密匙数据创建KeySpec对象
SecretKeySpec key = new SecretKeySpec(rawKeyData, "AES");
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = new byte[SECURE_KEY_LENGTH];
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
// 正式执行加密操作

return cipher.doFinal(data);
}

/**
* zlib解压
*
* @param input
* @return
*/
public static byte[] unzip(byte[] input) throws Exception {
if (input == null || input.length == 0) {
return input;
}

byte[] out = null;
ByteArrayOutputStream bao = null;
Inflater decompresser = new Inflater();
try {
bao = new ByteArrayOutputStream(input.length);
decompresser.setInput(input);
byte[] buf = new byte[1024];
int len = 0;
while (!(decompresser.finished() || decompresser.needsInput())) {
len = decompresser.inflate(buf);
if (len == 0) {
break;
}
bao.write(buf, 0, len);
}
out = bao.toByteArray();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("ILLEGAL_COMPRESSION");
} finally {
try {
if (null != bao) {
bao.close();
}
decompresser.end();
} catch (IOException e) {
log.error("close unzip stream error:" + e.getMessage());
}
}
return out;

}

/**
* zlib压缩
*
* @param input
* @return
*/
public static byte[] zip(byte[] input) throws Exception {
if (input == null || input.length == 0) {
return input;
}

byte[] out = null;
ByteArrayOutputStream bao = null;
Deflater compresser = new Deflater();
try {
compresser.setInput(input);
compresser.finish();
bao = new ByteArrayOutputStream(input.length);
byte[] buf = new byte[1024];
int len;
while (!compresser.finished()) {
len = compresser.deflate(buf);
bao.write(buf, 0, len);
}
out = bao.toByteArray();
} finally {
try {
if (null != bao) {
bao.close();
}
compresser.end();
} catch (IOException e) {
log.error("close zip stream error:" + e.getMessage());
}
}
return out;
}

public static void main(String[] args) throws Exception {
String test = "hello 佬油条!";
String key ="3jd92j28ahl9.W2_1";
byte[] data = encrypt(test.getBytes(), key);
data = zip(data);
System.out.println(new String(data));
/*******************网络传输*************************/
String key1 ="3jd92j28ahl9.W2_";//秘钥长度只需要16位 可以不到16位 超过16将会无视
byte[] data1 = unzip(data);
data1 = decrypt(data1, key1);
System.out.println(new String(data1));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: