473,663 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SHA1 C# Encryption to Java.Digest SHA-1 Encryption

1 New Member
Essentially my problem is that I have a java application that uses SHA-1 encryption and I have a .Net 2.0 WebService that needs to encrypt the same way for user authentication (passwords are encrypted in the database)

Take care to notice the wonderful wack in the java code to add 256 to the byte value.

Essentially the problem is that when I create the ASCII byte array the values are the same:

Password = test;
Java:
[116, 101, 115, 116]

C#:
[116, 101, 115, 116]

The problem is that when java calls byte[] digest = algorithm.diges t(); I get a different value in C# when I call:

Expand|Select|Wrap|Line Numbers
  1. byte[] hash = sha1.ComputeHash(Encoding.ASCII.GetBytes(userPassword));
  2.  
java: value hashing user password 'password' = 15baa161e4c9b91 3f13f1068212510 b16cf813311b17e e68fd8

c#: value hashing user password 'password'= 15baa161e4c9b91 3f13f1068212510 b16cf813311b17e e68fd815b1aa161 1e41c91b913f13f 10618212510b16c 1f813311b17e1e6 18f1d8
Any suggestions would be appreciated, I have been racking my brains for quite awhile.....

Java code:
Expand|Select|Wrap|Line Numbers
  1. public static String encryptPassword(String password) {
  2. String returnValue = null;
  3. byte[] buf = password.getBytes();
  4. MessageDigest algorithm=null;
  5. try {
  6. algorithm = MessageDigest.getInstance("SHA-1");
  7.  
  8. } catch (NoSuchAlgorithmException e) {
  9. sClassLog.logException(e);
  10. }
  11. algorithm.reset();
  12. algorithm.update(buf);
  13. byte[] digest = algorithm.digest();
  14. returnValue = "";
  15.  
  16. for (int byteIdx = 0; byteIdx < digest.length; byteIdx++) {
  17.  
  18. returnValue += Integer.toHexString(digest[byteIdx] + 256);
  19.  
  20. }
  21. return returnValue;
  22.  
  23. }
  24.  
  25.  
  26. My C# Code:
  27.  
  28.  
  29. private String encryptPassword(String userPassword)
  30.  
  31. {
  32.  
  33. SHA1 sha1 = SHA1CryptoServiceProvider.Create();
  34. byte[] hash = sha1.ComputeHash(Encoding.ASCII.GetBytes(userPassword));
  35.  
  36. StringBuilder digest = new StringBuilder();
  37.  
  38. foreach (byte n in hash) digest.Append(Convert.ToInt32(n + 256).ToString("x2"));
  39.  
  40. return digest.ToString();
  41.  
  42. }
Jun 2 '10 #1
1 5835
code green
1,726 Recognized Expert Top Contributor
withdrawn post
Jun 2 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

28
3685
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are appreciated.
1
3282
by: trapeze.jsg | last post by:
Hi. I am trying to get through to Microsoft MapPoint Services using ZSI for soap handling. I can generate the service classes and also the soap-requests generated by the service classes seem to be OK. The problem I am facing is that I can't seem to authenticate myself. I have made a small change to ZSI.client so that when I get a "401 Unauthorized" response from the remote server I build up a nice authorization request:
4
2114
by: ruben.de.visscher | last post by:
Hi, I want to make a program that checks the SHA1 message digest of a password from the user with a list of usernames and the corresponding message digests of their passwords. The problem is, that it simply refuses to recognize the SHA1( ) function i use in the program. I looked in the headerfile i included ( <openssl/sha.h> ) and the prototype is clearly in there, but it still does'nt work! Do i have to supply additional compiler flags...
1
1702
by: durumdara | last post by:
Hi ! I have a problem in Windows. I need to get the filelist in HDD-s, with sha-N value for every file (to see what changed in the past). 1. I see that Python supports the sha-1. But I need to make sha value in big files to (swap file, iso-s, etc.). Possible that file size more than 2 GB, so I cannot read as string...
3
2698
by: DurumDara | last post by:
Hi ! I need to speedup my MD5/SHA1 calculator app that working on filesystem's files. I use the Python standard modules, but I think that it can be faster if I use C, or other module for it. I use FSUM before, but I got problems, because I "move" into "DOS area", and the parameterizing of outer process maked me very angry (not working). You will see this in this place:
5
2649
by: EP | last post by:
This inquiry may either turn out to be about the suitability of the SHA-1 (160 bit digest) for file identification, the sha function in Python ... or about some error in my script. Any insight appreciated in advance. I am trying to reduce duplicate files in storage at home - I have a large number files (e.g. MP3s) which have been stored on disk multiple times under different names or on different paths. The using applications will...
0
3241
by: ReeceSeeSharp | last post by:
Having difficulty trying to get this code in .NET (C#) to print out this string EXACTLY as it prints out within Java using a) the MD5 hashing algorithm and B). In Java here's how a string "ididnot" would print out E#øðûÜ+œ_Z*º~ And here's the Java code string pswd; public String getPswd() {return pswd;} public void setPassword(String passwd) {
3
4312
by: mirandacascade | last post by:
Operating system: Win XP Vsn of Python: 2.4 Situation is this: Required to calcluate a message digest. The process for calcluating the digest must use an SHA-256 algorithm. Questions: 1) Is it correct that the sha module comes with python 2.4? 2) Is it correct that the sha module that ships with python 2.4 does NOT have the SHA-256 capability as part of the module?
8
5607
by: sathyashrayan | last post by:
Dear group, For a log-in page I have created a mysql db and user registers with a user name and password. The password field is encrypted with $passwd = sha1($_REQUEST); I insert the $passwd in mysql_insert. The password gets encrypted and stored in mysql. Now I want to check if the user has entered the correct password when he logs in. How can I do that. Any
4
6832
by: mattik | last post by:
Hello. I like to get this kind of signature being created witgh my web services client. http://www.ibm.com/developerworks/webservices/library/ws-security.html But that page showed only one digest in soap message. But if I try to create code like this link:
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.