小编Ily*_*s I的帖子

C#SHA-256与Java SHA-256.结果不同?

我想将一些Java中的代码转换为C#.

Java代码:

  private static final byte[] SALT = "NJui8*&N823bVvy03^4N".getBytes();

  public static final String getSHA256Hash(String secret)
  {
    try {
      MessageDigest digest = MessageDigest.getInstance("SHA-256");
      digest.update(secret.getBytes());
      byte[] hash = digest.digest(SALT);
      StringBuffer hexString = new StringBuffer();
      for (int i = 0; i < hash.length; i++) {
        hexString.append(Integer.toHexString(0xFF & hash[i]));
      }
      return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } 
    throw new RuntimeException("SHA-256 realization algorithm not found in JDK!");
  }
Run Code Online (Sandbox Code Playgroud)

当我尝试使用SimpleHash 类时,我得到了不同的哈希值

更新:

例如:

Java:byte [] hash = digest.digest(SALT); 生成(前6个字节):

[0] = 9
[1] …
Run Code Online (Sandbox Code Playgroud)

.net c# java cryptography sha256

3
推荐指数
1
解决办法
8030
查看次数

标签 统计

.net ×1

c# ×1

cryptography ×1

java ×1

sha256 ×1