国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程之MD5加密算法實例分析

2020-04-11 11:15:33
字體:
來源:轉載
供稿:網友

本文實例分析了Android編程之MD5加密算法。分享給大家供大家參考,具體如下:

Android MD5加密算與J2SE平臺一模一樣,因為Android 平臺支持 java.security.MessageDigest這個包。實際上與J2SE平臺一模一樣。

算法簽名:

復制代碼 代碼如下:
String getMD5(String val) throws NoSuchAlgorithmException

輸入一個String(需要加密的文本),得到一個加密輸出String(加密后的文本)

package com.tencent.utils;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/**  * 對外提供getMD5(String)方法  * @author randyjia  *  */ public class MD5 { public static String getMD5(String val) throws NoSuchAlgorithmException{  MessageDigest md5 = MessageDigest.getInstance("MD5");  md5.update(val.getBytes());  byte[] m = md5.digest();//加密  return getString(m);} private static String getString(byte[] b){  StringBuffer sb = new StringBuffer();   for(int i = 0; i < b.length; i ++){   sb.append(b[i]);   }   return sb.toString();}}

結束

/* * MD5加密 */ private String getMD5Str(String str) {  MessageDigest messageDigest = null;  try {   messageDigest = MessageDigest.getInstance("MD5");   messageDigest.reset();   messageDigest.update(str.getBytes("UTF-8"));  } catch (NoSuchAlgorithmException e) {   System.out.println("NoSuchAlgorithmException caught!");   System.exit(-1);  } 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]));  } //16位加密,從第9位到25位  return md5StrBuff.substring(8, 24).toString().toUpperCase();}

補充:Android MD5加密字符串示例

這里將字符串進行MD5加密,返回加密后的字符串(實際上是該字符串的報文摘要)。

public static String md5(String string) { byte[] hash; try {  hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) {  throw new RuntimeException("Huh, MD5 should be supported?", e); } catch (UnsupportedEncodingException e) {  throw new RuntimeException("Huh, UTF-8 should be supported?", e); } StringBuilder hex = new StringBuilder(hash.length * 2); for (byte b : hash) {  if ((b & 0xFF) < 0x10) hex.append("0");  hex.append(Integer.toHexString(b & 0xFF)); } return hex.toString();}

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 措美县| 夹江县| 孝义市| 柞水县| 叙永县| 抚州市| 拉萨市| 怀来县| 平南县| 永州市| 靖江市| 珲春市| 东台市| 遵化市| 靖宇县| 汉阴县| 临海市| 宁城县| 汕尾市| 乌海市| 固始县| 开江县| 阜新市| 贞丰县| 三河市| 扎囊县| 黄龙县| 平山县| 嘉定区| 嘉祥县| 汨罗市| 邓州市| 海安县| 辽中县| 客服| 鹤庆县| 浦江县| 怀化市| 石河子市| 蒙山县| 治多县|