android 关键字高亮
/**
* 多个关键字高亮变色
*
* @param color 变化的色值
* @param text 文字
* @param keyword 文字中的关键字
* @return
*/
public static SpannableString matcherSearchTitle(int color, String text,
String keyword) {
List<String> strings = new ArrayList();
SpannableString s = new SpannableString(text);
if (keyword.length() == 1) {
Pattern p = Pattern.compile(escapeExprSpecialWord(keyword));
Matcher m = p.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new ForegroundColorSpan(color), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else {
for (int i = 0; i < keyword.length(); i++) {
if (i == keyword.length()) {
String substring = keyword.substring(i);
strings.add(substring);
} else {
String substring = keyword.substring(i, i + 1);
strings.add(substring);
}
}
for (int i = 0; i < keyword.length(); i++) {
Pattern p = Pattern.compile(escapeExprSpecialWord(strings.get(i)));
Matcher m = p.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
s.setSpan(new ForegroundColorSpan(color), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return s;
}
/**
* 转义正则特殊字符 ($()*+.[]?\^{},|)
*
* @param keyword
* @return
*/
private static String escapeExprSpecialWord(String keyword) {
String[] fbsArr = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|"};
for (String key : fbsArr) {
if (keyword.contains(key)) {
keyword = keyword.replace(key, "\\" + key);
}
}
return keyword;
}
使用方式
KeywordUtil.matcherSearchTitle(Color.RED, "所有字", "需要高亮的关键字");
EditTextUtils 工具类
public class EditTextUtils {
/**
* Judge what situation hide the soft keyboard,click EditText view should
* show soft keyboard
*
* @param v
* Incident event
* @param event
* @return
*/
public static boolean isShouldHideSoftKeyBoard(View view, MotionEvent event) {
if (view != null && (view instanceof EditText)) {
int[] l = { 0, 0 };
view.getLocationInWindow(l);
int left = l[0], top = l[1], bottom = top + view.getHeight(), right = left + view.getWidth();
if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
// If click the EditText event ,ignore it
return false;
} else {
return true;
}
}
// if the focus is EditText,ignore it;
return false;
}
/**
* hide soft keyboard
*
* @param token
*/
public static void hideSoftKeyBoard(IBinder token, Context context) {
if (token != null) {
InputMethodManager im = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
//禁止EditText输入空格
public static InputFilter filter = (source, start, end, dest, dstart, dend) -> {
if (source.equals(" ")){
return "";
} else return null;
};
base64加密
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64并不是安全领域的加密算法,其实Base64只能算是一个编码算法,对数据内容进行编码来适合传输。标准Base64编码解码无需额外信息即完全可逆,即使你自己自定义字符集设计一种类Base64的编码方式用于数据加密,在多数场景下也较容易破解。
//加密
public static String toBase64(String str){
String result = "";
if (str != null) {
try {
result = new String(Base64.encode(str.getBytes("utf-8"), Base64.NO_WRAP), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result;
}
//解密
public static String getBase64(String str){
String result = "";
if (str != null) {
try {
result = new String(Base64.decode(str, Base64.NO_WRAP), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result;
}
使用方法
"base64加密"+ Base64Utils.toBase64(string)+"\n"+
"base64解密后"+Base64Utils.getBase64(Base64Utils.toBase64(string))
MD5加密:
MD5加密有哪些特点?
-
压缩性:任意长度的数据,算出的MD5值长度都是固定的。
-
容易计算:从原数据计算出MD5值很容易。
-
抗修改性:对原数据进行任何改动,哪怕只修改1个字节,所得到的MD5值都有很大区别。
-
强抗碰撞:已知原数据和其MD5值,想找到一个具有相同MD5值的数据(即伪造数据)是非常困难的。
MD5应用场景:
-
一致性验证
-
数字签名
-
安全访问认证
public class MD5Utils {
/***
* MD5加码 生成32位md5码
*/
public static String string2MD5(String inStr){
MessageDigest md5 = null;
try{
md5 = MessageDigest.getInstance("MD5");
}catch (Exception e){
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++){
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/**
* 加密解密算法 执行一次加密,两次解密
*/
public static String convertMD5(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 't');
}
String s = new String(a);
return s;
}
}
使用方法:
Log.d("TAG", "MD5后:" + MD5Utils.string2MD5(s));
Log.d("TAG", "MD5加密"+MD5Utils.convertMD5(s));
Log.d("TAG", "MD5解密的:" + MD5Utils.convertMD5(MD5Utils.convertMD5(s)));
aes加密
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),是一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。
那么为什么原来的DES会被取代呢,,原因就在于其使用56位密钥,比较容易被破解。而AES可以使用128、192、和256位密钥,并且用128位分组加密和解密数据,相对来说安全很多。完善的加密算法在理论上是无法破解的,除非使用穷尽法。使用穷尽法破解密钥长度在128位以上的加密数据是不现实的,仅存在理论上的可能性。统计显示,即使使用目前世界上运算速度最快的计算机,穷尽128位密钥也要花上几十亿年的时间,更不用说去破解采用256位密钥长度的AES算法了。
目前世界上还有组织在研究如何攻破AES这堵坚厚的墙,但是因为破解时间太长,AES得到保障,但是所用的时间不断缩小。随着计算机计算速度的增快,新算法的出现,AES遭到的攻击只会越来越猛烈,不会停止的。
public class AesUtils {
// /** 算法/模式/填充 **/
private static final String CipherMode = "AES/ECB/PKCS5Padding";
///** 创建密钥 **/
private static SecretKeySpec createKey(String pass) {
byte[] data = null;
if (pass == null) {
pass = "";
}
StringBuffer sb = new StringBuffer(32);
sb.append(pass);
while (sb.length() < 32) {
sb.append("0");
}
if (sb.length() > 32) {
sb.setLength(32);
}
try {
data = sb.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new SecretKeySpec(data, "AES");
}
// /** 加密字节数据 **/
public static byte[] encrypt(byte[] content, String pass) {
try {
SecretKeySpec key = createKey(pass);
System.out.println(key);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
///** 加密(结果为16进制字符串) **/
public static String encrypt(String content, String pass) {
byte[] data = null;
try {
data = content.getBytes("UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
data = encrypt(data, pass);
String result = byte2hex(data);
return result;
}
// /** 解密字节数组 **/
public static byte[] decrypt(byte[] content, String pass) {
try {
SecretKeySpec key = createKey(pass);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
///** 解密16进制的字符串为字符串 **/
public static String decrypt(String content, String pass) {
byte[] data = null;
try {
data = hex2byte(content);
} catch (Exception e) {
e.printStackTrace();
}
data = decrypt(data, pass);
if (data == null)
return null;
String result = null;
try {
result = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
// /** 字节数组转成16进制字符串 **/
public static String byte2hex(byte[] b) { // 一个字节的数,
StringBuffer sb = new StringBuffer(b.length * 2);
String tmp = "";
for (int n = 0; n < b.length; n++) {
// 整数转成十六进制表示
tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
}
return sb.toString().toUpperCase(); // 转成大写
}
// /** 将hex字符串转换成字节数组 **/
private static byte[] hex2byte(String inputString) {
if (inputString == null || inputString.length() < 2) {
return new byte[0];
}
inputString = inputString.toLowerCase();
int l = inputString.length() / 2;
byte[] result = new byte[l];
for (int i = 0; i < l; ++i) {
String tmp = inputString.substring(2 * i, 2 * i + 2);
result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
}
return result;
}
}
使用方法:
"aes加密前:"+ AesUtils.encrypt(mEdiText.getText().toString(),"android");
"aes加密后"+AesUtils.decrypt(AesUtils.encrypt(mEdiText.getText().toString(),"android"),"android"))