Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i have a java code whic i need to convert to c#



java code

Java
salt="40be4e59b9a2a2b5dffb918c0e86b3d75727a8f82b898d65cf87017309818b35f5c07a364ee3e7117730058065c1bad8a43491dc6549e17b6b0c85b144ada408ca7a1e8c7505e41f91e5d0f37996fa1dd54e68160fb44631f7d5cfec517bcacf4a8a0b2ed3321d881ce224060b4c22c6d97461595d6e8d604a1c41f646adb19c06f8398c1b8ceeb9d1f090aa67cfe5b31b0741a407e93c92e421f759d475e70cc306bb88f0678921eaaacf10da4f5cdada4d288514aae58c06ae5f752ae814e321d2dcbd5cf6e1621e6f154fc88d6cfe31bdc17d94595f038874ffaebad9656f3278b2694c439cbb263494b76b8b4ed81547aae71a98a722448fe11cd348d4f2e4123b42bac43c64b378914536e8b352";


    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
    messageDigest.update(salt.getBytes());
    result=new BigInteger(messageDigest.digest());

result=-8679331902525424680818339047284551675590913508739318217139721412571178967790


and i tried to convert to c#

What I have tried:

C#
rawData="40be4e59b9a2a2b5dffb918c0e86b3d75727a8f82b898d65cf87017309818b35f5c07a364ee3e7117730058065c1bad8a43491dc6549e17b6b0c85b144ada408ca7a1e8c7505e41f91e5d0f37996fa1dd54e68160fb44631f7d5cfec517bcacf4a8a0b2ed3321d881ce224060b4c22c6d97461595d6e8d604a1c41f646adb19c06f8398c1b8ceeb9d1f090aa67cfe5b31b0741a407e93c92e421f759d475e70cc306bb88f0678921eaaacf10da4f5cdada4d288514aae58c06ae5f752ae814e321d2dcbd5cf6e1621e6f154fc88d6cfe31bdc17d94595f038874ffaebad9656f3278b2694c439cbb263494b76b8b4ed81547aae71a98a722448fe11cd348d4f2e4123b42bac43c64b378914536e8b352";

using ( SHA256 sha256Hash = SHA256.Create())
	{             
	 var Rawdatabytes = System.Text.Encoding.ASCII.GetBytes(rawData);
	 byte[] bytes = sha256Hash.ComputeHash(Rawdatabytes);              
	 var result = new System.Numerics.BigInteger(bytes);
         
	}

result=8306751456257555796593622157661757526221618682565362023319192974333645213676


for both java and c# are getting different result 
can any one help us to solve the issue

also

for java code we are getting negative value and for c# we are getting positive
please can any one know the solution
Posted
Updated 19-Mar-19 0:24am
v5
Comments
Richard MacCutchan 18-Mar-19 6:57am    
Which part of the result is different, the raw bytes or the BigInteger?
sagar.panuganti 18-Mar-19 8:37am    
result was different..

both the cases the input was same

but the result was different
Richard MacCutchan 18-Mar-19 8:58am    
Which result, the ones before converting to BigInteger, or after? You need to explain exactly what values are different, we cannot see your screen or guess what happens in your code.
sagar.panuganti 18-Mar-19 9:02am    
result=new BigInteger(messageDigest.digest()); this is java


var result = new System.Numerics.BigInteger(bytes); in c#

after converting to BIGINTEGER the values are different
Richard MacCutchan 18-Mar-19 9:11am    
What are the hash values before conversion, and what are the BigInteger values after conversion? Please provide full information, just showing a couple of lines of code does not give us any clue as to what you are seeing.

First, make sure you are passing the same input values to both the codes.
Then if the result are still different, validate them against a known working program (e.g. an online one) to find out which one is broken.
 
Share this answer
 
Comments
Richard MacCutchan 19-Mar-19 4:33am    
See my latest comment above.
I have run two simple tests that demonstrate that the same source text produces the same hash value in both C# and Java.
 
Share this answer
 
Comments
sagar.panuganti 19-Mar-19 0:35am    
Please look into the improved question
sagar.panuganti 19-Mar-19 4:44am    
you mean messageDigest.digest() in java
= sha256Hash.ComputeHash(Rawdatabytes) in c#

so the issue might be with Big integer conversion ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900