Click here to Skip to main content
15,029,206 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
I have an encrypted string (encoded with a key using HMACSHA256). Now how can I decrypt it back using the key and HMACSHA256 algorithm?
Posted

1 solution

The question make no sense at all. This is not encryption. And your string cannot be decrypted, because nothing was encrypted. I don't know what do you call a "key", but you don't have a key. Probably you have only the hash-based message authentication code, only HMAC value itself, which does not contains the string you thought you "encrypted". You did not. You never mentioned you had "encrypted data".

Please see:
http://en.wikipedia.org/wiki/Hash-based_message_authentication_code[^],
https://msdn.microsoft.com/en-us/library/system.security.cryptography.hmacsha256%28v=vs.110%29.aspx[^].

To understand how something can be hash-based and what cryptographic hash does, please see:
http://en.wikipedia.org/wiki/Cryptographic_hash_function[^],
see also http://en.wikipedia.org/wiki/SHA-2[^].

—SA
   
v2
Comments
arunkx 25-Feb-15 0:42am
   
I will explain the process in detail.

public string function(string data,string key)
{
System.Text.UTFEncoding encode=new UTFEncoding ();
byte [] kbyte=encode.GetBytes(key);
byte [] mbyte=encode.GetBytes(data);
HMACSHA256 hmac=new HMACSHA256(kbyte);
byte []hmsg=hmac.ComputeHash(mbyte);
string rslt=Convert.ToBase64String(hmsg,0,hmsg.length);
return rslt;
}


This is how I did my encryption. Now I have the encrypted text and the key. How can I decrypt it back?
   
Are you serious? Do I have to repeat? You did not do encryption. There is nothing to decrypt.
—SA
arunkx 25-Feb-15 6:47am
   
Sorry. Doing this for the first time :)
   
No problem.
Will you accept the answer formally now? I think I explained everything.
—SA
arunkx 25-Feb-15 6:51am
   
Before that, I have hashed some data here. Can you tell me how to retrieve the data back if I have the key?
   
You cannot. The purpose of the cryptographic hash requires it to be irreversible.
Listen, I gave you links explaining it all. Did you read anything at all?
—SA
arunkx 25-Feb-15 7:01am
   
Ah Yes.There lies another of my doubt. What is the purpose of hashing if it cannot be reversed back to meaningful data? Is it like locking some data forever?
   
Well, again: I provided you some links. Why not reading the referenced articles?
It is not "locking data".

How do you think passwords are stored, for example? Cryptographic hash is stored, never a password. To check validity of a password, hash is compared to hash. This way, nobody knows the password itself, except its creator who types it. The party checking for validity of authentication cannot know original password. There are other uses...

—SA
arunkx 25-Feb-15 7:12am
   
Cool. Thanks Mr.SA :)
   
You are welcome.
—SA

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