android md5加密與rsa加解密實(shí)現(xiàn)代碼
更新時(shí)間:2012年12月05日 15:20:13 作者:
本文將詳細(xì)介紹android上的MD5和RSA的加解密實(shí)現(xiàn)代碼分享,需要了解更多的朋友可以參考下
復(fù)制代碼 代碼如下:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
/*
* MD5加密
*/
public static String getDigest(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString().toUpperCase();
}
}
復(fù)制代碼 代碼如下:
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class RSAUtil {
/**
* 加密
*
* @param message
* @return
*/
public static String encrypt(String message) {
byte[] result = null;
try {
result = encrypt(message, getPublicKey());
} catch (Exception e) {
e.printStackTrace();
}
return toHexString(result);
}
/**
* 解密
*
* @param message
* @return
*/
public static String decrypt(String message) {
byte[] result = null;
try {
result = decrypt(message, getPublicKey());
} catch (Exception e) {
e.printStackTrace();
}
return new String(result);
}
/**
* 加密(公鑰加密、私鑰加密)
*
* @param message 待加密的消息
* @param key 公鑰或私鑰
* @return
* @throws Exception
*/
private static byte[] encrypt(String message, Key key) throws Exception {
Cipher cipher = Cipher.getInstance("RSA", new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, key);
// 注意中文的處理
return cipher.doFinal(message.getBytes("gb2312"));
}
/**
* 解密(如果公鑰加密,則用私鑰解密;如果私鑰加密,則用公鑰解密)
*
* @param message 待解密的消息
* @param key 公鑰或私鑰
* @return
* @throws Exception
*/
private static byte[] decrypt(String message, Key key) throws Exception {
Cipher cipher = Cipher.getInstance("RSA", new BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(toBytes(message));
}
/**
* 通過模長和公鑰指數(shù)獲取公鑰
*
* @param modulus 模長
* @param publicExponent 公鑰指數(shù)
* @return
* @throws Exception
*/
public static PublicKey getPublicKey() {
PublicKey publicKey = null;
String modulus = "140865665237544398577638791993321201143991791099370252934699963963887058026979531275917645451893685346013654333931757603593193739776986525943697469996693704995753266331593233395038088698299308180612215713544577462613426793519824197226393059683065343801412208205295045502348474411031999124137863144916358656019";
String publicExponent = "65537";
BigInteger m = new BigInteger(modulus);
BigInteger e = new BigInteger(publicExponent);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA", new BouncyCastleProvider());
publicKey = keyFactory.generatePublic(keySpec);
} catch (Exception e1) {
e1.printStackTrace();
}
return publicKey;
}
private static final byte[] toBytes(String s) {
byte[] bytes;
bytes = new byte[s.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
}
return bytes;
}
public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(HEXCHAR[(b[i] & 0xf0) >>> 4]);
sb.append(HEXCHAR[b[i] & 0x0f]);
}
return sb.toString();
}
private static char[] HEXCHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}
復(fù)制代碼 代碼如下:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String info = "不知道什么時(shí)候,開始喜歡這里,每個(gè)夜里都會(huì)來這里看你。";
Log.d("zhangxy",MD5.getDigest(info));
// 公鑰加密
Log.d("zhangxy",RSAUtil.encrypt(info));
// 公鑰解密(經(jīng)私鑰加密)
Log.d("zhangxy", RSAUtil.decrypt("94d5ffca913465785714348f10c57c8a0226aca2c8a5294d3a32f398c4791bee8bb37873e16a7b71ed64e40ac121ec4f4bf375b881421a17a3f10789dc543ab41c26c11ba1184b2e0328ef6d354e191f7d978bd9b984e76d310e028b3412093f7296d58d9adb7f9e4b5eb6427c369ae5e919f848c7a21b7794d5985e4d3ad10a"));
}
}
相關(guān)文章
安卓(android)怎么實(shí)現(xiàn)下拉刷新
這里我們將采取的方案是使用組合View的方式,先自定義一個(gè)布局繼承自LinearLayout,然后在這個(gè)布局中加入下拉頭和ListView這兩個(gè)子元素,并讓這兩個(gè)子元素縱向排列。對(duì)安卓(android)怎么實(shí)現(xiàn)下拉刷新的相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-04-04
Android開源項(xiàng)目PullToRefresh下拉刷新功能詳解2
這篇文章主要為大家進(jìn)一步的介紹了Android開源項(xiàng)目PullToRefresh下拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android手機(jī)衛(wèi)士之確認(rèn)密碼對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)衛(wèi)士之確認(rèn)密碼對(duì)話框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android 打開相冊(cè)選擇單張圖片實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 打開相冊(cè)選擇單張圖片實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android流式布局實(shí)現(xiàn)歷史搜索記錄功能
最近在開發(fā)項(xiàng)目的時(shí)候,有一個(gè)需求是展示歷史搜索記錄 ,展示的樣式是流式布局(就是根據(jù)內(nèi)容自動(dòng)換行)。接下來通過本文給大家分享android流式布局實(shí)現(xiàn)歷史搜索記錄功能,感興趣的的小伙伴參考下2017-05-05
Android入門之使用eclipse進(jìn)行源碼開發(fā)的方法
這篇文章主要介紹了Android入門之使用eclipse進(jìn)行源碼開發(fā)的方法,較為詳細(xì)的分析了使用eclipse進(jìn)行Android源碼開發(fā)的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-02-02
Android 詳解ThreadLocal及InheritableThreadLocal
這篇文章主要介紹了Android 詳解ThreadLocal及InheritableThreadLocal的相關(guān)資料,需要的朋友可以參考下2017-01-01

