以下内容会包含Android 如何获取剪切板内容,和MD5加密,Java自带Base64加密等的内容,如果您没有这些需求,那建议您不要往下翻了.......
Java 获取剪切板。
ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData data = cm.getPrimaryClip();
ClipData.Item item = data.getItemAt(0);
String content = item.getText().toString();
附常用工具方法:
1、MD5加密算法1
/**
* MD5,32位morning大写,16位默认小写
*
* @param sourceStr 需要加密的字符串
* @param flag 支持16,32
* @return 加密字符串
*/
public static String MD5(String sourceStr, int flag) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sourceStr.getBytes(StandardCharsets.UTF_8));
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));// 转换成16进制编码
}
if (flag == 32) {
return buf.toString().toUpperCase();// 转换成字符串;
} else if (flag == 16) {
return buf.toString().substring(8, 24);
} else {
return sourceStr;
}
} catch (NoSuchAlgorithmException e) {
}
return "";// 返回结果
}
2、MD5加密算法2
public static String MD5Upcase(String data) {
String result = "";
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
try {
byte[] btInput = data.getBytes("utf-8");
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
}
return result;
}
3、加解密工具类,注意:慎用,该工具类性能较差
EncrypUtils.java
代码:
package *********;
import android.text.TextUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;
public class EncrypUtils {
private final static String DES = "DES";
private final static String ENCODE = "GBK";
private final static String defaultKey = "abc123456";
/**
* 使用 默认key 加密
*
* @return String
*/
public static String encrypt(String data) throws Exception {
byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE));
String strs = new BASE64Encoder().encode(bt);
return strs;
}
/**
* 使用 默认key 解密
*
* @return String
*/
public static String decrypt(String data) throws IOException, Exception {
if (data == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf, defaultKey.getBytes(ENCODE));
return new String(bt, ENCODE);
}
/**
* Description 根据键值进行加密
*
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
public static String encrypt(String data, String key) throws Exception {
byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE));
String strs = new BASE64Encoder().encode(bt);
return strs;
}
/**
* Description 根据键值进行解密
*
* @param data
* @param key 加密键byte数组
* @return
* @throws IOException
* @throws Exception
*/
public static String decrypt(String data, String key) throws IOException,
Exception {
if (data == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf, key.getBytes(ENCODE));
return new String(bt, ENCODE);
}
/**
* Description 根据键值进行加密
*
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密钥初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
/**
* Description 根据键值进行解密
*
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
}
import Decoder.BASE64Encoder;报错怎么办?
1、Java eclipse或idea,点击https://www.cnblogs.com/liuyangfirst/p/6429913.html
2、Android 点击下载地址