package util;
import java.io.unsupportedencodingexception;
import java.security.invalidkeyexception;
import java.security.nosuchalgorithmexception;
import java.security.securerandom;
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;
import org.apache.tomcat.util.codec.binary.base64;
/**
*
* @author administrator
*
*/
public class aes {
// 加密
public static string encrypt(string ssrc, string skey) throws exception {
if (skey == null) {
system.out.print(key为空null);
return null;
}
// 判断key是否为16位
if (skey.length() != 16) {
system.out.print(key长度不是16位);
return null;
}
byte[] raw = skey.getbytes(utf-8);
secretkeyspec skeyspec = new secretkeyspec(raw, aes);
cipher cipher = cipher.getinstance(aes/ecb/pkcs5padding);//算法/模式/补码方式
cipher.init(cipher.encrypt_mode, skeyspec);
byte[] encrypted = cipher.dofinal(ssrc.getbytes(utf-8));
return new base64().encodetostring(encrypted);//此处使用base64做转码功能,同时能起到2次加密的作用。
}
// 解密
public static string decrypt(string ssrc, string skey) throws exception {
try {
// 判断key是否正确
if (skey == null) {
system.out.print(key为空null);
return null;
}
// 判断key是否为16位
if (skey.length() != 16) {
system.out.print(key长度不是16位);
return null;
}
byte[] raw = skey.getbytes(utf-8);
secretkeyspec skeyspec = new secretkeyspec(raw, aes);
cipher cipher = cipher.getinstance(aes/ecb/pkcs5padding);
cipher.init(cipher.decrypt_mode, skeyspec);
byte[] encrypted1 = new base64().decode(ssrc);//先用base64解密
try {
byte[] original = cipher.dofinal(encrypted1);
string originalstring = new string(original,utf-8);
return originalstring;
} catch (exception e) {
system.out.println(e.tostring());
return null;
}
} catch (exception ex) {
system.out.println(ex.tostring());
return null;
}
}
/**
* 加密
*
* @param content 需要加密的内容
* @param password 加密密码
* @return
*/
public static byte[] encrypt(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[] decrypt(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;
}
/**将二进制转换成16进制
* @param buf
* @return
*/
public 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();
}
/**将16进制转换为二进制
* @param hexstr
* @return
*/
public 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 exception {
/*
* 此处使用aes-128-ecb加密模式,key需要为16位。
*/
string ckey = 1234567890123456;
// 需要加密的字串
string csrc = www.gowhere.so;
system.out.println(csrc);
// 加密
string enstring = aes.encrypt(csrc, ckey);
system.out.println(加密后的字串是: + enstring);
// 解密
string destring = aes.decrypt(enstring, ckey);
system.out.println(解密后的字串是: + destring);
string content = test;
string password = 12345678;
//加密
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);
system.out.println(解密后: + new string(decryptresult));
}
}
以上就是如何实现aes加密?的详细内容。