我需要在Java中生成哈希,然后在C#中对其进行检查。转换回字符串时如何从这两种算法获得相同的输出?
------------ C#------------
?
------------ Java ------------
public static String encrypt(String value) throws NoSuchAlgorithmException {
private static final String ALGORITHM = "SHA-256";
private static final String[] UPDATES = "goKpRF61ApDDJN9m0OOwHtU9G56psEqJjPUdiH3kZto=";
MessageDigest md = MessageDigest.getInstance(ALGORITHM);
for (int i = 0; i < UPDATES.length; i++) {
md.update(UPDATES[i].getBytes());
}
return Base64.getEncoder().encodeToString(md.digest(value.getBytes()));
}
答案 0 :(得分:0)
让我对您的问题的Java部分发表评论(我不知道C#)。
private static final String[] UPDATES = "goKpRF61ApDDJN9m0OOwHtU9G56psEqJjPUdiH3kZto=";
这看起来不正确。如果只有一个字符串,则声明应为:
private static final String UPDATES = "goKpRF61ApDDJN9m0OOwHtU9G56psEqJjPUdiH3kZto=";
其余的代码:
MessageDigest md = MessageDigest.getInstance(ALGORITHM);
md.update(UPDATES.getBytes());
return Base64.getEncoder().encodeToString(md.digest());
如果您要消化多个字符串:
MessageDigest md = MessageDigest.getInstance(ALGORITHM) ;
String str[] = {"A", "B", "C", "D"} ;
for (String s : str)
md.update(s.getBytes()) ;
return Base64.getEncoder().encodeToString(md.digest());
(我忽略了Base64位,因为有太多的Base64库,我猜您正在使用一个可以正常工作的库)