本文实例讲述了java基于aes对称加密算法实现的加密与解密功能。分享给大家供大家参考,具体如下:
package com.soufun.com;
import java.io.unsupportedencodingexception;
import java.security.invalidkeyexception;
import java.security.nosuchalgorithmexception;
import java.security.securerandom;
import java.util.date;
import javax.crypto.badpaddingexception;
import javax.crypto.cipher;
import javax.crypto.illegalblocksizeexception;
import javax.crypto.keygenerator;
import javax.crypto.nosuchpaddingexception;
import javax.crypto.secretkey;
import javax.crypto.spec.secretkeyspec;
/**
* @author whd
*/
public class aesutil {
private static final string aes="aes";
private static final string utf8="utf-8";
static keygenerator kgen =null;
static{
try {
kgen= keygenerator.getinstance(aes);
} catch (nosuchalgorithmexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
/*
* @param content:
* @param password:
*/
private static byte[] encrypt(string content, string password) {
try {
// 使用静态代码块来生成keygenerator对象
//keygenerator kgen = keygenerator.getinstance(aes);
// 使用128 位
kgen.init(128, new securerandom(password.getbytes()));
secretkey secretkey = kgen.generatekey();
byte[] encodeformat = secretkey.getencoded();
secretkeyspec key = new secretkeyspec(encodeformat, aes);
// cipher对象实际完成加密操作
cipher cipher = cipher.getinstance(aes);
// 加密内容进行编码
byte[] bytecontent = content.getbytes(utf8);
// 用密匙初始化cipher对象
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:
*/
private static byte[] decrypt(byte[] content, string password) {
try {// 使用静态代码块来生成keygenerator对象
//keygenerator kgen = keygenerator.getinstance(aes);
// 使用128 位
kgen.init(128, new securerandom(password.getbytes()));
secretkey secretkey = kgen.generatekey();
byte[] encodeformat = secretkey.getencoded();
secretkeyspec key = new secretkeyspec(encodeformat, aes);
// cipher对象实际完成加密操作
cipher cipher = cipher.getinstance(aes);
// 用密匙初始化cipher对象
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;
}
/**
* 二进制--》十六进制转化
* @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 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;
}
public static void main(string[] args) throws unsupportedencodingexception {
long begin=new date().gettime();
string content = "aaades加密测试";
string password = "12345678dd";
// 加密
system.out.println("加密前:" + content);
byte[] encryptresult = encrypt(content, password);
string encryptresultstr = parsebyte2hexstr(encryptresult);
system.out.println("加密后:" + encryptresultstr);
// 解密
byte[] decryptfrom = parsehexstr2byte(encryptresultstr);
byte[] decryptresult = decrypt(decryptfrom, password);
// 解密内容进行解码
string result = new string(decryptresult, utf8);
system.out.println("解密后:" + result);
long end= new date().gettime();
system.out.println(end-begin);
}
}
注:securerandom是生成安全随机数序列,password.getbytes()是种子,只要种子相同,序列就一样,所以解密只要有password就行,可以复原这个序列。
ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:
密码安全性在线检测:
迅雷、快车、旋风url加密/解密工具:
在线散列/哈希算法加密工具:
在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:
在线sha1/sha224/sha256/sha384/sha512加密工具:
希望本文所述对大家java程序设计有所帮助。
希望与广大网友互动??
点此进行留言吧!