Android Java 獲取剪切板的內(nèi)容,MD5加密。

以下內(nèi)容會(huì)包含Android 如何獲取剪切板內(nèi)容,和MD5加密,Java自帶Base64加密等的內(nèi)容,如果您沒(méi)有這些需求,那建議您不要往下翻了.......

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大寫(xiě),16位默認(rèn)小寫(xiě)

? ? *

? ? * @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));// 轉(zhuǎn)換成16進(jìn)制編碼

? ? ? ? ? ? }

? ? ? ? ? ? if (flag == 32) {

? ? ? ? ? ? ? ? return buf.toString().toUpperCase();// 轉(zhuǎn)換成字符串;

? ? ? ? ? ? } else if (flag == 16) {

? ? ? ? ? ? ? ? return buf.toString().substring(8, 24);

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? return sourceStr;

? ? ? ? ? ? }

? ? ? ? } catch (NoSuchAlgorithmException e) {

? ? ? ? }

? ? ? ? return "";// 返回結(jié)果

? ? }

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";

? ? /**

? ? * 使用 默認(rèn)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;

? ? }

? ? /**

? ? * 使用 默認(rèn)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 根據(jù)鍵值進(jìn)行加密

? ? *

? ? * @param data

? ? * @param key? 加密鍵byte數(shù)組

? ? * @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 根據(jù)鍵值進(jìn)行解密

? ? *

? ? * @param data

? ? * @param key? 加密鍵byte數(shù)組

? ? * @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 根據(jù)鍵值進(jìn)行加密

? ? *

? ? * @param data

? ? * @param key? 加密鍵byte數(shù)組

? ? * @return

? ? * @throws Exception

? ? */

? ? private static byte[] encrypt(byte[] data, byte[] key) throws Exception {

? ? ? ? // 生成一個(gè)可信任的隨機(jī)數(shù)源

? ? ? ? SecureRandom sr = new SecureRandom();

? ? ? ? // 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象

? ? ? ? DESKeySpec dks = new DESKeySpec(key);

? ? ? ? // 創(chuàng)建一個(gè)密鑰工廠,然后用它把DESKeySpec轉(zhuǎn)換成SecretKey對(duì)象

? ? ? ? SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

? ? ? ? SecretKey securekey = keyFactory.generateSecret(dks);

? ? ? ? // Cipher對(duì)象實(shí)際完成加密操作

? ? ? ? Cipher cipher = Cipher.getInstance(DES);

? ? ? ? // 用密鑰初始化Cipher對(duì)象

? ? ? ? cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

? ? ? ? return cipher.doFinal(data);

? ? }

? ? /**

? ? * Description 根據(jù)鍵值進(jìn)行解密

? ? *

? ? * @param data

? ? * @param key? 加密鍵byte數(shù)組

? ? * @return

? ? * @throws Exception

? ? */

? ? private static byte[] decrypt(byte[] data, byte[] key) throws Exception {

? ? ? ? // 生成一個(gè)可信任的隨機(jī)數(shù)源

? ? ? ? SecureRandom sr = new SecureRandom();

? ? ? ? // 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象

? ? ? ? DESKeySpec dks = new DESKeySpec(key);

? ? ? ? // 創(chuàng)建一個(gè)密鑰工廠,然后用它把DESKeySpec轉(zhuǎn)換成SecretKey對(duì)象

? ? ? ? SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

? ? ? ? SecretKey securekey = keyFactory.generateSecret(dks);

? ? ? ? // Cipher對(duì)象實(shí)際完成解密操作

? ? ? ? Cipher cipher = Cipher.getInstance(DES);

? ? ? ? // 用密鑰初始化Cipher對(duì)象

? ? ? ? cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

? ? ? ? return cipher.doFinal(data);

? ? }

}

import Decoder.BASE64Encoder;報(bào)錯(cuò)怎么辦?

1、Java eclipse或idea,點(diǎn)擊https://www.cnblogs.com/liuyangfirst/p/6429913.html

2、Android 點(diǎn)擊下載地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容