HMACSHA256 Hash key generated by C++/CLI is different from Java Generated Hash Key

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



HMACSHA256 Hash key generated by C++/CLI is different from Java Generated Hash Key



C++/CLI Function



I am generating Hash key using C++/CLI and Sending Data and Hashkey Over the network to Other Application which is coded in Java.



But Java Application generates a different HashKey.



Is it due to different applications on different servers that the hashkeys are different ?



Any Idea where am i going wrong ?



Thanking in Advance.


char* EncodeData(char* ap_key, char* ap_sourceData)

char* lp_data_to_send = NULL;
int key_len = strlen(ap_key);

String^ lv_data = gcnew String(ap_sourceData);//Getting Data in System String
array<Byte>^ lv_main_data = Encoding::UTF8->GetBytes(lv_data);//Encoding to UTF-8

array<Byte>^key = gcnew array< Byte >(key_len + 2);
Marshal::Copy((IntPtr)ap_key, key, 0, key_len); //Copy key in System Array Byte

// Initialize the keyed hash object.
HMACSHA256^ myhmacsha256 = gcnew HMACSHA256(key);

// Compute the hash of the input file.
array<Byte>^hashValue = myhmacsha256->ComputeHash(lv_main_data);

String^ lv_hex_convert = BitConverter::ToString(hashValue)->Replace("-",""); //Converted to Hexadecimal and replacing '-' with ""
Console::WriteLine(lv_hex_convert);//Converted Hexadecimal Hashkey

//Converting to Char*
lp_data_to_send = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(lv_hex_convert);//Converting again to char* to be send to Calling Function

myhmacsha256->Clear(); //myhmacsha256 clear Instance
return lp_data_to_send;//Return Char*



int main()

//Secret Key shared by C++/CLi application and Java Application
String ^ lv_key_g = " My Secret Key";
char lv_sourceData = "My data" ;
char lv_destinationData[512] = "" ;
char* lp_ret = NULL;

array<Byte>^secretkey = gcnew array<Byte>(65); //Declaring Array
//Converting to UTF-8
secretkey = Encoding::UTF8->GetBytes(lv_key_g);

/*Converting to char* */

pin_ptr<System::Byte> p = &secretkey[0];
unsigned char* pby = p;
//Converting to Char* to send
char* lp_key = reinterpret_cast<char*>(pby);//Converting data to char*

/*End converting to Byte Array*/

lp_ret = EncodeData(lp_key, lv_sourceData);//calling Function



JAVA-FUNCTION


String key = "My Key"; //Hash Key Shared by Both Application
String hashKey = "My Data"; //Data Shared by both Application
Mac sha256_HMAC = null;
try
//Creating Instance
sha256_HMAC = Mac.getInstance("HmacSHA256");
catch (NoSuchAlgorithmException e)
e.printStackTrace();

SecretKeySpec secret_key = null;
try

secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");//UTF-8 Secret Key
catch (UnsupportedEncodingException e)
e.printStackTrace();

try
sha256_HMAC.init(secret_key); //Init Secret Key
catch (InvalidKeyException e)
e.printStackTrace();

final byte mac_data = sha256_HMAC.doFinal(hashKey.getBytes()); //Get Data in Bytes

String result = "";
for (final byte element : mac_data)
//Using Radix 16 to convert to String
result += Integer.toString((element & 0xff) + 0x100, 16).substring(1); //Converting to Hexadecimal

System.out.print(result);//Hashkey Print





Please note that C# and C++ are very different languages. I've removed that tag for you :-)
– John
Aug 8 at 7:44




1 Answer
1



return lp_data_to_send;//Return Char* will return a dangling pointer, you should probably return a reference-counted string lv_hex_convert instead. Another suspicious thing is that key is 2 bytes longer than required.


return lp_data_to_send;//Return Char*


lv_hex_convert





Thank You for your response. I have noted your point will do the changes. but when i print lv_hex_convert System String value is different over there only. It is not that due to conversion to char* it is changing.
– Devious
Aug 8 at 8:04





@Devious Another suspicious thing is that key is 2 bytes longer then required.
– VTT
Aug 8 at 8:06





As you said removed that + 2 bytes and it worked. Only thing is my character variables are coming in Capital whereas as that of Java it is in Small.
– Devious
Aug 8 at 8:14






add your comment as Answer i will mark it Correct. Only thing is I am not sure why characters are coming in Capital in C++ whereas in Java it is in Small.
– Devious
Aug 8 at 8:18







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS