Android 加密解密字符串詳解
package eoe.demo;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* Usage:
* <pre>
* String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)
* ...
* String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)
* </pre>
* @author ferenc.hechler
*/
public class SimpleCrypto {
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}
- Android字符串和十六進(jìn)制相互轉(zhuǎn)化出現(xiàn)的中文亂碼問(wèn)題
- Android中判斷字符串中必須包含字母或者數(shù)字
- TextVie獲取顯示字符串的寬度之Android開(kāi)發(fā)
- Android獲取手機(jī)屏幕寬高、狀態(tài)欄高度以及字符串寬高信息的方法
- android開(kāi)發(fā)教程之framework增加字符串資源和圖片等resource資源
- Android字符串轉(zhuǎn)Ascii碼實(shí)例代碼
- Android字符串資源文件format方法使用實(shí)例
- Android TextView字體顏色設(shè)置方法小結(jié)
- Android編程實(shí)現(xiàn)TextView字體顏色設(shè)置的方法小結(jié)
- Android 字符串中某個(gè)字段可點(diǎn)擊和設(shè)置顏色的方法
相關(guān)文章
Android下SDL2實(shí)現(xiàn)五子棋游戲
這篇文章主要為大家詳細(xì)介紹了Android下SDL2實(shí)現(xiàn)五子棋游戲的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
Android開(kāi)發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能示例
這篇文章主要介紹了Android開(kāi)發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能,結(jié)合實(shí)例形式分析了Android DatePicker和TimePicker組件的功能、常用函數(shù)、布局及日期時(shí)間選擇相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
AndroidStudio構(gòu)建項(xiàng)目提示錯(cuò)誤信息“unable to find valid certification”的
這篇文章主要介紹了AndroidStudio構(gòu)建項(xiàng)目提示“unable to find valid certification”最新解決方案,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Android中Intent機(jī)制詳解及示例總結(jié)(總結(jié)篇)
本文是小編日常收集整理些有關(guān)Android中Intent機(jī)制詳解及示例總結(jié),對(duì)android中intent相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-04-04
Android DragVideo實(shí)現(xiàn)播放視頻時(shí)任意拖拽的方法
這篇文章主要介紹了Android DragVideo實(shí)現(xiàn)播放視頻時(shí)任意拖拽的方法的相關(guān)資料,一種在播放視頻時(shí),能夠拖拽的方案,需要的朋友可以參考下2016-12-12
android使用SharedPreferences進(jìn)行數(shù)據(jù)存儲(chǔ)
Android平臺(tái)給我們提供了一個(gè)SharedPreferences類,它是一個(gè)輕量級(jí)的存儲(chǔ)類,特別適合用于保存軟件配置參數(shù)。有興趣的可以了解一下。2017-02-02
Android編程實(shí)現(xiàn)手機(jī)拍照的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)手機(jī)拍照的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)手機(jī)拍照的操作步驟與具體細(xì)節(jié),需要的朋友可以參考下2016-11-11
Android中CountDownTimer 實(shí)現(xiàn)倒計(jì)時(shí)功能
本篇文章主要介紹了Android中CountDownTimer 實(shí)現(xiàn)倒計(jì)時(shí)功能,CountDownTimer 是android 自帶的一個(gè)倒計(jì)時(shí)類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

