0

Hi,

I am using bouncy castle API as below.

  • iterations = 10000 password = abcdefghi salt = zyxwvutsrqpo

>Need return value as 8 bytes.

public byte[] getEncodedHash(String password, String salt, int iterations)    throws UnsupportedEncodingException {
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(newSHA256Digest());
try {
gen.init(password.getBytes("UTF-8"), salt.getBytes(), iterations);
} catch (UnsupportedEncodingException ex) {
throw ex;
}
byte[] dk = ((KeyParameter) gen.generateDerivedParameters(64)).getKey();
return dk;        
}

Calling above function as below.
==================================
byte[] hashbyte = hasher.getEncodedHash(pass, salt, iterations);
System.out.println(Arrays.toString(hashbyte));

Output as below:
==================
[44, -6, -117, 7, -30, -68, 4, -9]

>Why am I getting negative bytes and is there anyway to get only positive bytes?

When I have checked with client which they are using Python script and they always get positive integers (I need to develop in Java using bouncy castle). Surprising is, whatever positive numbers I have, they match exactly on their side at respective index (output from Python script).

Appreciate your valuable input on this.

  • bytes are a signed type in java. That fact is irrelevant for your crypto needs. If you want to compare with other languages then print them as hex values, not decimal integers. – President James K. Polk Feb 28 '18 at 00:18
  • Bytes in Java are signed and range from -128 to 127. If you want to see the unsigned value, you'll have to cast it to int: `b & 0xFF`. – shmosel Feb 28 '18 at 00:21
  • I am sorry for heading, bold, etc.. I was posting first time here and found very hard to post with so many restrictions on my above post. Thank you for reading with patience to help me. – Srinivas Reddy Feb 28 '18 at 00:29
  • shmosel: This is not duplicate as I never find an answer to my question. When I search in google, I have found my same question at: http://www.javanews.org/news/return-values-are-negative-bytes-from-bouncy-castle-api-for-sha256#.WpX8Da6nGM8 When I click on More info at the bottom the question, it redirected to here. Not sure, why my question is showing on another site. Please educate me. – Srinivas Reddy Feb 28 '18 at 00:50
  • Thank you Shmosel for pointing out other posts and helped me in fixing my issue. You have also mentioned in this post and will give credit for this answer. System.out.println(b & 0xff); – Srinivas Reddy Feb 28 '18 at 01:31

0 Answers0