-2

I needed to generate the hash in Java and then check it in C#. How to get the same output from these two algorithms when converting back to a string?

------------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()));
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Anh Trung
  • 21
  • 7
  • Are you sure you wanto to compute a hash of encoded input, not raw? As well try to put some effort of your own into c# code, then maybe someone will help you further. Next - what do you mean by `converting back to a string?`? – gusto2 Apr 07 '19 at 09:22

1 Answers1

0

Let me comment on the Java part of your question (I don't know C#).

private static final String[] UPDATES = "goKpRF61ApDDJN9m0OOwHtU9G56psEqJjPUdiH3kZto=";

This does not look right. If you have just one string, the declaration should be:

private static final String UPDATES = "goKpRF61ApDDJN9m0OOwHtU9G56psEqJjPUdiH3kZto=";

And the rest of the code:

MessageDigest md = MessageDigest.getInstance(ALGORITHM);

md.update(UPDATES.getBytes());

return Base64.getEncoder().encodeToString(md.digest());

If you have multiple strings to 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());

(I ignored the Base64 bit because there are way too many Base64 libraries and I am guessing that you are using one that works alright)

  • Thank you for your comment and I will edit it to my java code. I want to change from Java Code -> C# Code – Anh Trung Apr 04 '19 at 04:10
  • Hi . Do you found your c# code for decryption.I think between these two must be have a unique token inserted in web config.If you found your answer please say us.I have this problem – maniaq Jan 14 '20 at 06:03