Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Encrypting and Dycrypting string using SHA-512 function (C++)


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 8 - Last Post: Feb 10, 2018 10:08 AM Last Post By: Antonio Estevez
nilesh shinde

Posts: 47
Registered: 10/5/13
Encrypting and Dycrypting string using SHA-512 function (C++)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 19, 2017 3:53 AM
Hi,

Could you please share the sample C++ code to encrypt and decrypt string using SHA-512 function. I am using Indy components in my application, so if you could use TIdHashSHA512 instead direct openssl references will be great.

Thanks,
Nilesh

Edited by: nilesh shinde on Jul 19, 2017 3:53 AM

Edited by: nilesh shinde on Jul 19, 2017 4:07 AM
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Encrypting and Dycrypting string using SHA-512 function (C++)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 19, 2017 9:22 AM   in response to: nilesh shinde in response to: nilesh shinde
El 19/07/2017 a las 13:08, nilesh shinde escribió:
Hi,

Could you please share the sample C++ code to encrypt and decrypt string using SHA-512 function. I am using Indy

You can't decrypt, that is, get the original data from the hash returned by the SHA algorithm

components in my application, so if you could use TIdHashSHA512 instead direct openssl references will be great.

TIdHashSHA512 is not fully implemented: TIdHashSHA512::IsAvailable() returns false

I think that only TIdHashSHA1 if available at this time.

#include <IdHashSHA.hpp>
...
    if (TIdHashSHA1::IsAvailable())
    {
       TIdHash *Encoder= new TIdHashSHA1();
       try
       {
          String Result= Encoder->HashStringAsHex(Text);
          // Use Result as needed
          Edit1->Text= Result;
       }
       __finally
       {
          delete Encoder;
       }
    }
nilesh shinde

Posts: 47
Registered: 10/5/13
Re: Encrypting and Dycrypting string using SHA-512 function (C++)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 19, 2017 10:29 AM   in response to: Antonio Estevez in response to: Antonio Estevez
I am specifically looking for encrypting my application password and want to use it when my application tries to connect server. Currently I send password as plain text to the server. As you said encrypted string can not decrypted to its original (text form) form. So how can I send my encrypted stored password to the server in text form.

Thanks,
Nilesh
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Encrypting and Dycrypting string using SHA-512 function (C++)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 19, 2017 10:51 AM   in response to: nilesh shinde in response to: nilesh shinde
El 19/07/2017 a las 19:29, nilesh shinde escribió:
I am specifically looking for encrypting my application password and want to use it when my application tries to connect server. Currently I send password as plain text to the server. As you said encrypted string can not decrypted to its original (text form) form. So how can I send my encrypted stored password to the server in text form.

You must send the password encrypted using the same algorithm that the password is encrypted in the server.
The server must compare both hashes

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Encrypting and Dycrypting string using SHA-512 function (C++)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 24, 2017 9:55 AM   in response to: nilesh shinde in response to: nilesh shinde
nilesh shinde wrote:

I am specifically looking for encrypting my application password and
want to use it when my application tries to connect server. Currently
I send password as plain text to the server. As you said encrypted
string can not decrypted to its original (text form) form. So how can
I send my encrypted stored password to the server in text form.

Don't send sensitive data in plain text, unless the connection itself
is encrypted, such as with SSL/TLS. SHA-512 is a hashing algorithm,
not an encryption algorithm. Sending a hash of the password will only
work if the server contains a similarly hashed version of the same
password for comparison.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Encrypting and Dycrypting string using SHA-512 function (C++) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 24, 2017 9:53 AM   in response to: Antonio Estevez in response to: Antonio Estevez
Antonio Estevez wrote:

TIdHashSHA512 is not fully implemented: TIdHashSHA512::IsAvailable()
returns false

Most (not all) of Indy's hashing classes, including SHA-512, are
currently dependant on external hashing functions being assigned to
public function pointers in the IdFIPS unit. The only hashing
algorithms that Indy implements natively (when the function pointers
are not assigned) are:

Adler32
CRC-16, CRC-32
ELF
MD-2, MD-4, MD-5
SHA-1

You can use any hashing library you want, you would just have to make
sure the IdFIPS function pointers are assigned accordingly.

By default, hashing functions are provided by the OpenSSL library (the
IdSSLOpenSSLHeaders unit assigns OpenSSL hashing functions to the
IdFIPS function pointers), eg:

#include <IdSSLOpenSSLHeaders.hpp> // <-- add this
#include <IdSSLOpenSSL.hpp> // <-- add this
#include <IdHashSHA.hpp>
 
...
 
LoadOpenSSLLibrary(); // <-- add this
 
if (TIdHashSHA1::IsAvailable()) // <-- if OpenSSL loaded successfully,
this should return true now...
{
    TIdHash *Encoder = new TIdHashSHA1();
    try
    {
        String Result= Encoder->HashStringAsHex(Text);
        // Use Result as needed
        Edit1->Text = Result;
    }
    __finally
    {
        delete Encoder;
    }
}


In the case of TIdHashSHA512, it depends on the following IdFIPS
function pointers:

IsHashingIntfAvail
IsSHA512HashIntfAvail
GetSHA512HashInst
UpdateHashInst
FinalHashInst

I think that only TIdHashSHA1 if available at this time.

SHA-1 is natively implemented by Indy, yes. It can also use a hashing
library, if the appropriate SHA-1 function pointers are assigned in the
IdFIPS unit.

--
Remy Lebeau (TeamB)
Michael Morelli

Posts: 6
Registered: 6/26/14
Re: Encrypting and Dycrypting string using SHA-512 function (C++) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 9, 2018 1:15 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
How can I decrypt the 'Result' HexString to origin string?
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Encrypting and Dycrypting string using SHA-512 function (C++) [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 10, 2018 10:08 AM   in response to: Michael Morelli in response to: Michael Morelli
El 09/02/2018 a las 10:15, Michael Morelli escribió:
How can I decrypt the 'Result' HexString to origin string?

You can't. Like any other function that calculates a hash value the algorithm works in one direction only

bernard roussely

Posts: 106
Registered: 2/8/05
Re: Encrypting and Dycrypting string using SHA-512 function (C++)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 26, 2017 11:53 AM   in response to: nilesh shinde in response to: nilesh shinde
Hi,
Try TMS Cryptography Pack: https://www.tmssoftware.com/site/tmscrypto.asp
You'll get everything to encrypt/decrypt/hash/sign and derive keys from passwords.

Regards,
bernard
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02