1.static定义的静态方法里面不能引用非静态的变量。
比如以下代码中的变量s在encrypt里面引用的时候会报:
Non-static field 's' can not be referenced from a static context.
public class AESUtil {
private String s = "abc";
/**
* 加密
*
* @param content 需要加密的内容
* @param password 加密密码
* @return
*/
public static String encrypt(String content, String password) {
System.out.println(s);
try {
byte[] raw = password.getBytes("UTF-8");
SecretKeySpec key = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return parseByte2HexStr(result); // 加密
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}