Java MD5 hashing not matching C# MD5 hashing

后端 未结 1 1101
傲寒
傲寒 2021-01-06 06:28

I know very little about encryption/hashing.

I have to hash an encryption key. The example in Java is like this...

String encryptionKey = \"test\";

         


        
相关标签:
1条回答
  • 2021-01-06 07:10

    Actually, the results are identical. Like other integral types, a byte value may be interpreted as either signed or unsigned. For example, 10001111 would correspond to 143 (your second C# value) if interpreted as unsigned. However, if interpreted as signed (using two’s complement), its value would be -113 (your second Java value).

    Thus, the disparity seems to be caused by your values being formatted as signed in Java but unsigned in C#. If you want to get signed bytes in C#, you can use:

    sbyte[] encryptionKeyBytesSigned = 
        encryptionKeyBytes.Select(b => (sbyte)b).ToArray();
    

    However, be careful that this is not merely a formatting issue that only arises when you display your values. When saved to file, both results should be identical.

    0 讨论(0)
提交回复
热议问题