Advertisement

12.25.2007 at 11:20PM PST, ID: 23042815
[x]
Attachment Details

HMAC-SHA1 in JAVA and OpenSSL

Asked by TKD in C Programming Language, Java Programming Language, C / C++ / C# Editors and IDEs

Tags: openssl, java

Hi, experts:
I develop a server-client application. At server side, I use Java language. At client side, I use C language.
Key is known between server and client.
At server side,
1) signature <== HMAC_SHA1(plaintext, key)
2) sends plaintext and signature to client
At client side,
1) receives plaintext and signature
2) signature' <== HMAC_SHA1(plaintext, key)
3) compare signature' and signature.
I expect that signatures between server and client are the same. But it doesn't.
So I want to someone to help me check the functions.
Thank you.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
// HMAC-SHA1 in JAVA
public static String getSHA1Digest(String PlainText, String key)
	{
		String sha_string = null;
		try
		{
			SecretKey secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
			Mac messageAuthenticationCode = Mac.getInstance("HmacSHA1");
			messageAuthenticationCode.init(secretKey);
			messageAuthenticationCode.update(PlainText.getBytes());
			byte[] digest = messageAuthenticationCode.doFinal();
			sha_string = new String(HexBin.encode(digest));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			return sha_string;
		}
	}
 
 
// HMAC-SHA1 in OpenSSL
void computeHmac(char* hmac, const char* input, int length, const char* key, int sizeKey)
{
   unsigned int resultSize=0;
   HMAC(EVP_sha1(),
        key, sizeKey,
        (const unsigned char*)(input), length,
        (unsigned char*)(hmac), &resultSize);
   assert(resultSize == 20);
}
[+][-]12.26.2007 at 03:11AM PST, ID: 20528872

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, Java Programming Language, C / C++ / C# Editors and IDEs
Tags: openssl, java
Sign Up Now!
Solution Provided By: CEHJ
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628