有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVA中的hmacsha1 hmacsha1

我正在尝试用Java创建一个代码,以获取带有密钥的消息的HMAC-SHA1,结果必须与UBUNTU中的以下openssl命令行匹配: 十六进制文件名中的openssl dgst-sha1-hmac密钥。fil

我有以下Java代码:

private static MessageDigest md;
private static byte[] XorKeyIpad, XorKeyOpad;

private static final byte ipad = 0x36;
private static final byte opad = 0x5c;
private static final byte maxsize = 64;

public static void HMAC_SHA1(String msg, String keyString) throws NoSuchAlgorithmException, UnsupportedEncodingException {

    int i;
    String keyHex = toHex(keyString);
    byte[] keyByte = keyHex.getBytes();
    md = MessageDigest.getInstance("SHA-1");

    //Verify if the Key have the right size;
    if (keyByte.length > maxsize) {
        keyByte = md.digest(keyByte);
        md.reset();
    }
    System.out.println("KeyByte: " + keyByte + " Length: " + keyByte.length);

    //XOR between Key and ipad;
    XorKeyIpad = new byte[maxsize];
    for (i = 0; i < keyByte.length; i++) {
        XorKeyIpad[i] = (byte) (keyByte[i] ^ ipad);
    }
    System.out.println("XorKeyIpad: " + XorKeyIpad + "  Ipad: " + ipad);
    md.update(XorKeyIpad);
    XorKeyIpad = md.digest(XorKeyIpad);       


    //Concat the XOR between Key and ipad with the message;
    String concatXorKIM = concatByteString(XorKeyIpad, msg);


    //SHA1 of the concat the XOR between Key and ipad with the message(which i call X);
    md.update(concatXorKIM.getBytes());
    byte[] hashConcatXorKIM = md.digest();
    md.reset();


    //XOR between Key and opad(which i call Y);
    XorKeyOpad = new byte[maxsize];
    for (i = 0; i < keyByte.length; i++) {
        XorKeyOpad[i] = (byte) (keyByte[i] ^ opad);
    }

    //Concat between X and Y;
    byte[] concatHashXorKI_XorKO = concatByteByte(hashConcatXorKIM, XorKeyOpad);

    //SHA1 of the variable concatHashXorKI_XorKO
    md.update(concatHashXorKI_XorKO);
    byte[] hmac = md.digest();
    md.reset();
}


public static String toHex(String arg) throws UnsupportedEncodingException {
    return String.format("%040x", new BigInteger(1, arg.getBytes("UTF-8")));
}

public static String concatByteString(final byte[] bytes, final String str) {
    final StringBuilder sb = new StringBuilder();
    for (byte b : bytes) {
        sb.append(b);
    }
    sb.append(str);
    return sb.toString();
}

public static byte[] concatByteByte(final byte[] byteOne, final byte[] byteTwo) {
    byte[] concatBytes = new byte[byteOne.length + byteTwo.length];
    System.arraycopy(byteOne, 0, concatBytes, 0, byteOne.length);
    System.arraycopy(byteTwo, 0, concatBytes, byteOne.length, byteTwo.length);

    return concatBytes;
}

final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    int v;
    for (int j = 0; j < bytes.length; j++) {
        v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}`

这两个结果是不同的,这意味着我做错了什么

有人能帮我吗

提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    您可以尝试以下方法:

    @Grapes(
        @Grab(group='commons-codec', module='commons-codec', version='1.10')
    )
    
    import org.apache.commons.codec.digest.HmacUtils
    
    HmacUtils.hmacSha1Hex(key.bytes, message.bytes)
    
  2. # 2 楼答案

    试试这个:

    private static final byte zero = 0x00;
    XorKeyOpad = new byte[maxsize];
    for (i = 0; i < maxsize; i++) 
    {
        XorKeyOpad[i] = (byte) ((keyByte[i] ^ opad):(zero ^ opad));
    }
    

    密钥的长度必须与maxsize相同。 如果它较小,则必须用零填充