经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
如何 使 Java、C# md5 加密的值保持一致
来源:cnblogs  作者:VipSoft  时间:2023/12/22 16:20:00  对本文有异议

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 代码如下

  1. package com.vipsoft.core.util;
  2. import java.security.MessageDigest;
  3. public class Md5Helper {
  4. public static final String EMPTY_STRING = "";
  5. private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
  6. private static String byteToHexString(byte b) {
  7. int n = b;
  8. if (n < 0) {
  9. n = 256 + n;
  10. }
  11. int d1 = n / 16;
  12. int d2 = n % 16;
  13. return hexDigits[d1] + hexDigits[d2];
  14. }
  15. private static String byteArrayToHexString(byte[] b) {
  16. StringBuffer sb = new StringBuffer();
  17. for (int i = 0; i < b.length; i++) {
  18. sb.append(byteToHexString(b[i]));
  19. }
  20. return sb.toString();
  21. }
  22. public static String MD5Encode(String origin) {
  23. String result = null;
  24. try {
  25. result = origin;
  26. MessageDigest md = MessageDigest.getInstance("MD5");
  27. result = byteArrayToHexString(md.digest(result.getBytes("UTF-8")));
  28. } catch (Exception ex) {
  29. ex.printStackTrace();
  30. }
  31. return result;
  32. }
  33. }

C# 代码如下

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace VipSoft.Core
  9. {
  10. public class Md5Helper
  11. {
  12. public static string MD5Encode(string origin)
  13. {
  14. var md5 = new MD5CryptoServiceProvider();
  15. byte[] clearBytes = System.Text.Encoding.UTF8.GetBytes(origin);
  16. byte[] targetData = md5.ComputeHash(clearBytes);
  17. string byte2String = BitConverter.ToString(targetData);
  18. return byte2String.Replace("-","").ToLower();
  19. }
  20. }
  21. }

C# => System.Text.Encoding.UTF8
JAVA => StandardCharsets

  1. package java.nio.charset;
  2. /**
  3. * Constant definitions for the standard {@link Charset Charsets}. These
  4. * charsets are guaranteed to be available on every implementation of the Java
  5. * platform.
  6. *
  7. * @see <a href="Charset#standard">Standard Charsets</a>
  8. * @since 1.7
  9. */
  10. public final class StandardCharsets {
  11. private StandardCharsets() {
  12. throw new AssertionError("No java.nio.charset.StandardCharsets instances for you!");
  13. }
  14. /**
  15. * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the
  16. * Unicode character set
  17. */
  18. public static final Charset US_ASCII = Charset.forName("US-ASCII");
  19. /**
  20. * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
  21. */
  22. public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
  23. /**
  24. * Eight-bit UCS Transformation Format
  25. */
  26. public static final Charset UTF_8 = Charset.forName("UTF-8");
  27. /**
  28. * Sixteen-bit UCS Transformation Format, big-endian byte order
  29. */
  30. public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
  31. /**
  32. * Sixteen-bit UCS Transformation Format, little-endian byte order
  33. */
  34. public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
  35. /**
  36. * Sixteen-bit UCS Transformation Format, byte order identified by an
  37. * optional byte-order mark
  38. */
  39. public static final Charset UTF_16 = Charset.forName("UTF-16");
  40. }

Charset 是 Java 中的一个类,它代表了字符集。字符集是一组字符的集合,每个字符都有一个唯一的数字表示。Charset 类提供了一组预定义的常量来表示常见的字符集。

以下是一些常见的 Charset 编码常量:

  1. US-ASCII

    • Charset.forName("US-ASCII")
    • 7位ASCII字符集,包括128个字符。
  2. ISO-8859-1 (Latin-1)

    • Charset.forName("ISO-8859-1")
    • 对西欧语言进行编码,是ASCII的超集。
  3. UTF-8

    • Charset.forName("UTF-8")
    • 一种变长编码,用于表示Unicode字符集。它是目前最常用的编码之一,能够表示世界上几乎所有的书写系统。
  4. UTF-16

    • Charset.forName("UTF-16")
    • 使用16位单元对Unicode字符进行编码。它能够表示所有的Unicode字符。
  5. UTF-16BE 和 UTF-16LE

    • Charset.forName("UTF-16BE")
    • Charset.forName("UTF-16LE")
    • 它们是UTF-16的两种变体,分别代表大端和小端字节序。
  6. 其他
    还有许多其他的字符集和编码,例如 Charset.forName("GB2312")(用于简体中文)和 Charset.forName("Shift_JIS")(用于日语)等。

当处理文本数据时,选择合适的字符集非常重要,因为错误的字符集可能会导致乱码或数据丢失。UTF-8 由于其广泛的兼容性和能够表示几乎所有的Unicode字符,通常是一个很好的选择。

https://www.cnblogs.com/zengguoyu/p/3973483.html

原文链接:https://www.cnblogs.com/vipsoft/p/17921381.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号