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)); } }
相关文章推荐
- JAVA中断机制详解
- 为什么我新建的maven项目没有java目录?
- Struts2——debug标签
- 错误,这个如何解决呢?内存溢出的问提。把JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128m 还是不行
- Java中的位操作
- 自定义java泛型
- 线程的创建方式问题
- java运行时异常与一般异常有何异同?
- Eclipse的使用
- ns-3 的下载、编译以及 Eclipse 的相关配置
- JDK1.8源码学习之Map.java
- java 图片缩放和截取
- java Split 特殊符号处理
- 公共技术点之 Java 反射 Reflection
- 【项目管理和构建】十分钟教程,eclipse配置maven + 创建maven项目(三)
- java 第三方登录之新浪微博登录
- JAVA基础知识点
- SpringMVC源码剖析(三)- DispatcherServlet的初始化流
- [疯狂Java]I/O:File(文件类,也是文件流的节点)、FilenameFilter(文件过滤器)
- 基于JAVA线程池读取单个SQL数据库表