Java C# md5 加密值保持一致,一般是編碼不一致造成的值不同
JAVA (加密:123456) | C#(加密:123456) | ||
---|---|---|---|
UTF-8 | e10adc3949ba59abbe56e057f20f883e | UTF8 | e10adc3949ba59abbe56e057f20f883e |
UTF-16LE | ce0bfd15059b68d67688884d7a3d3e8c | Unicode | ce0bfd15059b68d67688884d7a3d3e8c |
US-ASCII | e10adc3949ba59abbe56e057f20f883e | ASCII | e10adc3949ba59abbe56e057f20f883e |
ISO-8859-1 | e10adc3949ba59abbe56e057f20f883e | --- | --- |
UTF-16BE | ef4dafda494ad517e9823ae7d102a4c8 | BigEndianUnicode | ef4dafda494ad517e9823ae7d102a4c8 |
UTF-16LE | ce0bfd15059b68d67688884d7a3d3e8c | ||
UTF-16 | 5231722c0787fbf7b277a4a136f6e245 | ||
--- | --- | UTF32 | 4fc043750a2441defd8e35d2e23e84f0 |
--- | --- | UTF7 | e10adc3949ba59abbe56e057f20f883e |
Java 程式碼如下
package com.vipsoft.core.util;
import java.security.MessageDigest;
public class Md5Helper {
public static final String EMPTY_STRING = "";
private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
private static String byteToHexString(byte b) {
int n = b;
if (n < 0) {
n = 256 + n;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
private static String byteArrayToHexString(byte[] b) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
sb.append(byteToHexString(b[i]));
}
return sb.toString();
}
public static String MD5Encode(String origin) {
String result = null;
try {
result = origin;
MessageDigest md = MessageDigest.getInstance("MD5");
result = byteArrayToHexString(md.digest(result.getBytes("UTF-8")));
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
C# 程式碼如下
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace VipSoft.Core
{
public class Md5Helper
{
public static string MD5Encode(string origin)
{
var md5 = new MD5CryptoServiceProvider();
byte[] clearBytes = System.Text.Encoding.UTF8.GetBytes(origin);
byte[] targetData = md5.ComputeHash(clearBytes);
string byte2String = BitConverter.ToString(targetData);
return byte2String.Replace("-","").ToLower();
}
}
}
C# => System.Text.Encoding.UTF8
JAVA => StandardCharsets
package java.nio.charset;
/**
* Constant definitions for the standard {@link Charset Charsets}. These
* charsets are guaranteed to be available on every implementation of the Java
* platform.
*
* @see <a href="Charset#standard">Standard Charsets</a>
* @since 1.7
*/
public final class StandardCharsets {
private StandardCharsets() {
throw new AssertionError("No java.nio.charset.StandardCharsets instances for you!");
}
/**
* Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the
* Unicode character set
*/
public static final Charset US_ASCII = Charset.forName("US-ASCII");
/**
* ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
*/
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
/**
* Eight-bit UCS Transformation Format
*/
public static final Charset UTF_8 = Charset.forName("UTF-8");
/**
* Sixteen-bit UCS Transformation Format, big-endian byte order
*/
public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
/**
* Sixteen-bit UCS Transformation Format, little-endian byte order
*/
public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
/**
* Sixteen-bit UCS Transformation Format, byte order identified by an
* optional byte-order mark
*/
public static final Charset UTF_16 = Charset.forName("UTF-16");
}
Charset
是 Java 中的一個類,它代表了字符集。字符集是一組字元的集合,每個字元都有一個唯一的數字表示。Charset
類提供了一組預定義的常量來表示常見的字符集。
以下是一些常見的 Charset
編碼常量:
-
US-ASCII:
Charset.forName("US-ASCII")
- 7位ASCII字符集,包括128個字元。
-
ISO-8859-1 (Latin-1):
Charset.forName("ISO-8859-1")
- 對西歐語言進行編碼,是ASCII的超集。
-
UTF-8:
Charset.forName("UTF-8")
- 一種變長編碼,用於表示Unicode字符集。它是目前最常用的編碼之一,能夠表示世界上幾乎所有的書寫系統。
-
UTF-16:
Charset.forName("UTF-16")
- 使用16位單元對Unicode字元進行編碼。它能夠表示所有的Unicode字元。
-
UTF-16BE 和 UTF-16LE:
Charset.forName("UTF-16BE")
Charset.forName("UTF-16LE")
- 它們是UTF-16的兩種變體,分別代表大端和小端位元組序。
-
其他:
還有許多其他的字符集和編碼,例如Charset.forName("GB2312")
(用於簡體中文)和Charset.forName("Shift_JIS")
(用於日語)等。
當處理文字資料時,選擇合適的字符集非常重要,因為錯誤的字符集可能會導致亂碼或資料丟失。UTF-8 由於其廣泛的相容性和能夠表示幾乎所有的Unicode字元,通常是一個很好的選擇。