maven
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
完整demo代码
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.alibaba.fastjson.JSONObject;
import javax.crypto.*;
import java.io.File;
import java.util.Base64;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
public class AesUtil {
private static String storeid;
private static String ip;
private static String num;
private static String pwd;
public static String getStoreid() {
return storeid;
}
public static void setStoreid(String storeid) {
AesUtil.storeid = storeid;
}
public static String getIp() {
return ip;
}
public static void setIp(String ip) {
AesUtil.ip = ip;
}
public static String getNum() {
return num;
}
public static void setNum(String num) {
AesUtil.num = num;
}
public static String getPwd() {
return pwd;
}
public static void setPwd(String pwd) {
AesUtil.pwd = pwd;
}
public static void main(String[] args) throws Exception {
setXml(Encrypt("ip123456"), Encrypt("num789"), Encrypt("pwe123456"), Encrypt("33"));
init();
}
public static void init() throws Exception {
JSONObject obj = getXml();
if (obj.containsKey("ip")&&obj.getString("ip").length()>0) {
setIp(obj.getString("ip"));
} else {
throw new NullPointerException("ip");
}
if (obj.containsKey("num")&&obj.getString("num").length()>0) {
setNum(obj.getString("num"));
} else {
throw new NullPointerException("num");
}
if (obj.containsKey("pwd")&&obj.getString("pwd").length()>0) {
setPwd(obj.getString("pwd"));
} else {
throw new NullPointerException("pwd");
}
if (obj.containsKey("storeid")&&obj.getString("storeid").length()>0) {
setStoreid(obj.getString("storeid"));
} else {
throw new NullPointerException("storeid");
}
}
public static JSONObject getXml() throws Exception {
JSONObject obj = new JSONObject();
try {
SAXReader sax = new SAXReader();
Document doc = sax.read(new File("config.xml"));
Element root = doc.getRootElement();
List<Element> firstChild = root.elements();
for (Element firstEle : firstChild) {
List<Attribute> afirstEle = firstEle.attributes();
for (Attribute attr : afirstEle) {
obj.put(attr.getName(), Decrpyt(attr.getText()));
}
}
} catch (DocumentException ex) {
ex.printStackTrace();
}
System.out.println(obj);
return obj;
}
public static void setXml(String ip, String num, String pwd, String storeid) throws Exception {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("Datebase");
Element user = root.addElement("user");
user.addAttribute("ip", ip);
user.addAttribute("num", num);
user.addAttribute("pwd", pwd);
user.addAttribute("storeid", storeid);
OutputStream os = new FileOutputStream("config.xml");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter xw = new XMLWriter(os, format);
xw.write(doc);
xw.flush();
xw.close();
}
private static final String ALGORITHM = "AES";
private static SecretKey geneKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom random = new SecureRandom();
random.setSeed("lxd123456".getBytes());
keyGenerator.init(random);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey;
}
public static String Encrypt(String content) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
SecretKey secretKey = AesUtil.geneKey();
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
cipher.update(content.getBytes());
byte[] result = cipher.doFinal();
String base64Result = Base64.getEncoder().encodeToString(result);
return base64Result;
}
public static String Decrpyt(String content) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
SecretKey secretKey = AesUtil.geneKey();
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] encodedBytes = Base64.getDecoder().decode(content.getBytes());
byte[] result = cipher.doFinal(encodedBytes);
return new String(result);
}
}