Android 加密解密字符串詳解
更新時間:2013年06月17日 09:17:10 作者:
本篇文章是對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));
}
}
復制代碼 代碼如下:
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字符串和十六進制相互轉(zhuǎn)化出現(xiàn)的中文亂碼問題
- Android中判斷字符串中必須包含字母或者數(shù)字
- TextVie獲取顯示字符串的寬度之Android開發(fā)
- Android獲取手機屏幕寬高、狀態(tài)欄高度以及字符串寬高信息的方法
- android開發(fā)教程之framework增加字符串資源和圖片等resource資源
- Android字符串轉(zhuǎn)Ascii碼實例代碼
- Android字符串資源文件format方法使用實例
- Android TextView字體顏色設(shè)置方法小結(jié)
- Android編程實現(xiàn)TextView字體顏色設(shè)置的方法小結(jié)
- Android 字符串中某個字段可點擊和設(shè)置顏色的方法
相關(guān)文章
Android實現(xiàn)輪播圖無限循環(huán)效果
這篇文章主要為大家詳細介紹了Android實現(xiàn)輪播圖無限循環(huán)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android自定義ViewFlipper實現(xiàn)滾動效果
這篇文章主要為大家詳細介紹了Android自定義ViewFlipper實現(xiàn)滾動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08淺析AndroidStudio3.0最新 Android Profiler分析器(cpu memory network
Android Profiler分為三大模塊: cpu、內(nèi)存 、網(wǎng)絡(luò)。本文給大家介紹AndroidStudio3.0最新 Android Profiler分析器(cpu memory network 分析器)的相關(guān)知識,他們的基本使用方法,在文中都給大家提到,具體內(nèi)容詳情大家通過本文一起學習吧2017-12-12Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法
這篇文章主要介紹了Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法,結(jié)合具體實例形式分析了Android創(chuàng)建內(nèi)容提供器的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Android開發(fā)之TabActivity用法實例詳解
這篇文章主要介紹了Android開發(fā)之TabActivity用法,結(jié)合實例形式較為詳細的分析了Android擴展Activity實現(xiàn)標簽頁效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-03-03Android Kotlin開發(fā)實例(Hello World!)及語法詳解
這篇文章主要介紹了Android Kotlin開發(fā)實例及語法詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05Android CountDownTimer實現(xiàn)倒計時器
這篇文章主要為大家詳細介紹了Android CountDownTimer實現(xiàn)倒計時效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02