java编写的AES数据加密

来源:互联网 发布:mysql root password 编辑:程序博客网 时间:2024/05/16 12:06

主要是aes加密后的密文,会变的奇大,便想把数据压缩和加密结合到一起,于是写了一下这个工具类!

不过如果加密的数据过短,数据量反而会变大,只有数据量比较大的时候才会体现出来!


csdn下载地址:http://download.csdn.net/detail/yuanfen7650/7816033

希望赚点资源分,谢谢大家!


测试函数:

public static void main(String[] args) {
String key = "abdawqe";
String content = "alsdlsalfdlflaslelwqlewqlals";
String miwen = encrypt(key, content);
System.out.println("密文:"+miwen);
System.out.println("原文:"+decrypt(key, miwen));
System.out.println("原文长度:"+content.length() + "   密文长度:" + miwen.length());
}


源代码:

public static boolean isCompress=true;//是否进行压缩

// 压缩
private static String compress(String str) {
if(!isCompress)return str;
if (str == null || str.length() == 0) {
return str;
}
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
// return out.toString("ISO-8859-1");
return Base64.encode(out.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


// 解压�?
private static String uncompress(String str) {
if(!isCompress)return str;
if (str == null || str.length() == 0) {
return str;
}
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(Base64.decode(str));
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// toString()使用平台默认编码,也可以显式的指定如toString("GBK")
return out.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


/**
* 加密

* @param key
*            密钥
* @param content
*            内容
*/
public static String encrypt(String key, String content) {
if(content==null)return null;
content=compress(content);
byte[] encryptResult = encryptByte(content, key);
if (encryptResult == null)
return null;
String miwen = Base64.encode(encryptResult);
return compress(miwen);
}


/**
* 解密

* @param key
*            密钥
* @param content
*            密文
*/
public static String decrypt(String key, String content) {
byte[] decryptResult = decryptByte(Base64.decode(uncompress(content)),
key);
if (decryptResult == null)
return null;
return uncompress(new String(decryptResult));
}


/**
* 加密

* @param content
*            �?��加密的内�?
* @param password
*            加密密码
* @return
*/
public static byte[] encryptByte(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码�?
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始�?
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}


/**
* 解密

* @param content
*            待解密内�?
* @param password
*            解密密钥
* @return
*/
public static byte[] decryptByte(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码�?
cipher.init(Cipher.DECRYPT_MODE, key);// 初始�?
byte[] result = cipher.doFinal(content);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}

0 0
原创粉丝点击