C# SHA-256 vs. Java SHA-256. Different results?

前端 未结 5 1686
无人共我
无人共我 2021-02-10 09:16

I want to convert a some code which is in Java to C#.

Java Code:

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

  public         


        
5条回答
  •  情话喂你
    2021-02-10 09:49

    public static String getEncryptedPassword(String clearTextPassword) throws NoSuchAlgorithmException{
    
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(clearTextPassword.getBytes(StandardCharsets.UTF_8));
        byte[] digest = md.digest();
        String hex = String.format("%064x", new BigInteger(1, digest));
        String st = new String(hex.toUpperCase());
        for (int i = 2; i < (hex.length() + hex.length() / 2) - 1 ;) {
            st = new StringBuffer(st).insert(i, "-").toString();
                i = i + 3;        
        }
        return st ; 
    
    }
    

    You can use the following java to match that of C#

提交回复
热议问题