- 1、誰能告訴我一個簡潔可用的方法用java求一個字符串生成的md5碼。
- 2、java生成的MD5,和c#的生成的不一致,有java代碼,求c#代碼!
- 3、java怎麼把數據轉換成md5
- 4、java /groovy 的MD5類 及怎麼使用
- 5、java怎麼把字符串進行md5加密
- 6、如何使用Java生成MD5代碼
public class MD5 {
public static String crypt(String str) throws NoSuchAlgorithmException {
if (str == null || str.length() == 0) {
throw new IllegalArgumentException();
}
StringBuffer hexString = new StringBuffer();
MessageDigest md = MessageDigest.getInstance(“MD5”);
md.update(str.getBytes());
byte[] hash = md.digest();
for (int i = 0; i hash.length; i++) {
if ((0xff hash[i]) 0x10) {
hexString.append(“0” + Integer.toHexString((0xFF hash[i])));
} else {
hexString.append(Integer.toHexString(0xFF hash[i]));
}
}
return hexString.toString();
}
考慮一下中文字符問題。兩邊使用的編碼是不是一樣的。保持一樣就可以了。
建議使用UTF8編碼
JAVA代碼如下(在你給同的代碼上只做了少量修改):
public class Security {
public static void main(String[] args) {
try {
System.out.println(MD5(“中國”));
} catch(Exception ex) {}
}
public static String MD5(String txt) {
char hexDigits[] = { ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’,
‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ };
try {
byte[] btInput = txt.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) {
e.printStackTrace();
return null;
}
}
}
C#代碼如下:
static void Main(string[] args)
{
Console.WriteLine(Md5(“中國”));
Console.ReadLine();
}
static string Md5(string txt) {
byte[] result = Encoding.UTF8.GetBytes(txt);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] output = md5.ComputeHash(result);
return BitConverter.ToString(output).Replace(“-“, “”);
}
不是數據,而是字符串。
/**利用MD5進行加密
* @param str 待加密的字符串
* @return 加密後的字符串
* @throws NoSuchAlgorithmException 沒有這種產生消息摘要的算法
* @throws UnsupportedEncodingException
*/
public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
//確定計算方法
MessageDigest md5=MessageDigest.getInstance(“MD5”);
BASE64Encoder base64en = new BASE64Encoder();
//加密後的字符串
String newstr=base64en.encode(md5.digest(str.getBytes(“utf-8”)));
return newstr;
}
String dst = “一個待生成
md5值
的字符串”;
MessageDigest md = MessageDigest.
getInstance
(“MD5”);
md.update(dst.getBytes());// 將original傳給md5
byte[] digest = md.digest();// 產生md5序列
StringBuffer sb = new StringBuffer();// 轉換md5值為
16進制
for (byte b : digest) {
sb.append(String.format(“%02x”, b 0xff));
}
System.out.println(“原值:” + dst);
System.out.println(“MD5:” + sb);
給你看源代碼,我自己寫的
public static String md5(String src){
try{
MessageDigest md = MessageDigest.getInstance(“MD5”);
byte[] output = md.digest(src.getBytes());//加密處理
//將加密結果output利用Base64轉換成字符串輸出
String ret = Base64.encodeBase64String(output);
return ret;
}catch(Exception e){
throw new NoteException(“密碼加密失敗”,e);
}
}
public static void main(String[] args) {
System.out.println(md5(“123456”));
}
這是我以前做的一個小項目時用到md5寫的
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//將用戶密碼進行md5加密 並返回加密後的32位十六進制密碼
public class MD5Util {
public static String md5(String password) {
try {
// 獲取md5對象
MessageDigest md = MessageDigest.getInstance(“md5”);
// 獲取加密後的密碼並返回十進制字節數組
byte[] bytes = md.digest(password.getBytes());
// 遍曆數組得到每個十進制數並轉換成十六進制
StringBuffer sb = new StringBuffer();
for (byte b : bytes) {
// 把每個數轉成十六進制 存進字符中
sb.append(toHex(b));
}
String finish = sb.toString();
return finish;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
// 十進制轉十六進制方法
private static String toHex(byte b) {
int target = 0;
if (b 0) {
target = 255 + b;
} else {
target = b;
}
int first = target / 16;
int second = target % 16;
return Hex[first] + Hex[second];
}
static String[] Hex = { “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”,
“a”, “b”, “c”, “d”, “e”, “f” };
/*public static void main(String[] args) {
String a = MD5Util.md5(“1234”);
System.out.println(a);
}*/
}
原創文章,作者:C69TA,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/126543.html