Click here to Skip to main content
16,022,056 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi We have encryption logic where we are encoding using UTF8 but when using in Java we are using the same methodology to encode but the output byte stream is different.

Please let us know how to resolve the issue if we need to get the same output from both C# .Net and Java

following is the C# code that we have used for encoding
byte[] baPwd = Encoding.UTF8.GetBytes(password);


value for
baPwd
is different when encrypted using Java.

Please let us know what can be done

What I have tried:

We have tried to get the logs for both C# code & java code but the byte code is different.
Posted

Encoding.UTF8 Property (System.Text) | Microsoft Learn[^] is not an encryption operation: it's a text conversion format. It takes a UTF16 string, and returns an UTF8 version of it.

Whatever you are doing with the Java code that we can't see, we can't tell - but if encryption is your intent then this is not going to work at all!
 
Share this answer
 
As Griff said, encoding is not the same as encryption.

And when it comes to storing passwords, encryption is almost always the wrong thing to do. Unless you're writing a "password manager" app, or need to pass the original passwords to some third-party system, you should be hashing the passwords:

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

As to why the bytes are different, perhaps it's something to do with endian-ness?
Little-Endian and Big-Endian in Java[^]
 
Share this answer
 
Comments
OriginalGriff 4-Oct-24 8:17am    
Coming soon from the same poster: "How do I reverse the hashing so I can compare passwords?" :D
Rob Philpott 4-Oct-24 11:45am    
I did actually spend an afternoon trying to do that once. Didn't work.
Rob Philpott 4-Oct-24 11:46am    
Actually, I think I need to expand on that and say that I wasn't expecting to get back to the original password, just something which would hash to the same value.
OriginalGriff 5-Oct-24 3:14am    
I know you know this, but for those who don't: Unless you brute force it (i.e. keep trying passwords until you get a match) then when you find a way to do that the hashing algorithm is considered broken - which is why MD5 is not recommended for new projects - a way to find a "matching" password was found in 2008. Similarly, SHA-1 was discontinued in 2013, and is due to be phased out by 2030.

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