It probably already exists but still did not find the right way.

What i am trying to do is encrypt a string to a hash with a key associated and this hash does not have specialy characters, only numbers and characters, after that with the same key and the output of the hash i can get the the string back (decrypt)


Example:

Encrypt("user1",123)= b3daa77b4c04a9551b8781d03191fe098f325e67

Decrypt("b3daa77b4c04a9551b8781d03191fe098f325e67",123)=user1


I have read that there is a type or encryption called sha1 that has the right output but what is missing is the key

I checked CryptoAPI Extention 

Also i know this is not the best way to secure such data, if this wasn't the problem i would just use CryptoAPI Extention  


Any ideas?

Hello Henrique,

SHA1 is a hashing algorithm, not an encryption algorithm.  This means that it takes some data, and turns it into a representation of that data.  You cannot decrypt a hash.  For example:

SHA1("Henrique") = 2c1273b92c104dc2acbce8914abb6b6a451f5e5d
SHA1(one of my Windows virtual machine disks, which is around 60GB in size) = dfd847c6f24275afcd002c4959a78be1064f3bec

If you want to hash with a key, that is called HMAC, or hash message authentication code.  With HMAC, you still are only reproducing a representation of the data, however you also use a key to prove that the message is authentic and provided by the individual/system you are expecting.

If you want to encrypt data such that it can be decrypted, there are many algorithms such as AES, RSA, etc.  CryptoAPI uses the built-in .NET cryptography extensions, and I would recommend using that.  There is a YouTube video that shows how you can use CryptoAPI to protect site properties (https://www.youtube.com/watch?v=SiyK-JAGpw0) - while it is not exactly what you are looking for, it would be a good starting place.

Let me know how I can help further!

Hello Craig,

it helped, but not much sense i still need a SHA1 format or similar (with no special characters) and a way to decrypt after,

here is my thought then, by using Crypto in encrypt:

encrypt("henrique","123")="UhQxFPh0JznITpZWeJUKS6biDueNHZdDSitt+Ym+iZE="

MD5 (UhQxFPh0JznITpZWeJUKS6biDueNHZdDSitt+Ym+iZE=) = "7A3BA169D18B0F9363F1CCB8BDBF0DA9"

Save in database the hash and encryption(if hash doesn't exist in database).


In decrypt by passing the hash and key:

SearchDatabase(7A3BA169D18B0F9363F1CCB8BDBF0DA9)

If exists decrypt(SearchDatabase.encrypt, "123")= henrique


This seems a bit too much but it works

Hey Henrique,

What is it that you are actually trying to accomplish here?  I am a bit confused by your mention of storing encrypted data alongside with a hash, and then searching for that hash later.  It sounds like you are trying to build a rainbow table, which maps hashes to their original text.

Also, in your example, you can certainly replace your MD5 with SHA1 or any other hashing algorithm.

Hey Craig,

What i am trieng to accomplish is send a text with a key and encrypt it without special characters and later decrypt it using the output from the encrypt and the same key

The hashing yes i can replace it with any other, no problem there.

Solution

It sounds to me like your best option would be to use AES to encrypt (from CryptoAPI), and then store either the Hex value of the result (which contains only letters A-F and numbers), or the Base64 value of the result (A-Z, a-z, 0-9, +, /, and =).  You do not need to do any hashing to accomplish this.

See this example, hopefully it will help.  If the +, /, and = in the base64 result will not work for you, you can always use Base64ToBinary (from BinaryData) and then get the Hex value

Solution