已解决
密码学Python
Java SHA256生成与Python不同的哈希10
我试图生成OTP,但在我尝试将代码从python重写为java之后,我得到了不同的输出。 我不明白为什么,因为一些输出字符是相同的(当我更改uname或ctr时)。
PYTHON代码:from Crypto.Hash import SHA256
def get_otp(uname, ctr):
inp = uname+str(ctr)
binp = inp.encode('ascii')
hash=SHA256.new()
hash.update(binp)
dgst=bytearray(hash.digest())
out = ''
for x in range(9):
out += chr(ord('a')+int(dgst[x])%26)
if x % 3 == 2 and x != 8:
out += '-'
return out
print(get_otp('78951', 501585052583))
JAVA代码:import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Main
{
public static void main(String[] args) throws NoSuchAlgorithmException
{
System.out.println(get_otp("78951", "501585052583"));
}
public static String get_otp(String uname, String otp) throws NoSuchAlgorithmException
{
String input = uname + otp;
byte[] binInput = input.getBytes(StandardCharsets.US_ASCII);
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(binInput);
String retVal = "";
for(int i = 0; i
谢谢你的帮助。
海贼 king
2019.07.03
5819
收藏