Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. decrypting a string question [modified]

decrypting a string question [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
cryptographyquestionannouncementlearningcsharp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    monsieur_jj
    wrote on last edited by
    #1

    Hi all, Is there a c++ version of this or at least can a c++ version of this algorithm can be made? Please advise. Can anyone give me a sample on how to use windows crypto api decryption just by inputting an encrypted string? Thanks, Jayjay btw this is made of c#

    public static string Decrypt(string cipherString, bool useHashing)
    {
    byte[] keyArray;
    //get the byte code of the string

        byte\[\] toEncryptArray = Convert.FromBase64String(cipherString);
    
        System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
        //Get your key from config file to open the lock!
        string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
    
        if (useHashing)
        {
            //if hashing was used get the hash code with regards to your key
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            //release any resource held by the MD5CryptoServiceProvider
    
            hashmd5.Clear();
        }
        else
        {
            //if hashing was not implemented get the byte code of the key
            keyArray = UTF8Encoding.UTF8.GetBytes(key);
         }
    
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        //mode of operation. there are other 4 modes.
        //We choose ECB(Electronic code Book)
    
        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)
        tdes.Padding = PaddingMode.PKCS7;
    
        ICryptoTransform cTransform = tdes.CreateDecryptor();
        byte\[\] resultArray = cTransform.TransformFinalBlock
                (toEncryptArray, 0, toEncryptArray.Length);
        //Release resources held by TripleDes Encryptor
        tdes.Clear();
        //return the Clear decrypted TEXT
        return UTF8Encoding.UTF8.GetString(resultArray);
    }
    

    modified on Thursday, November 20, 2008 3:28 AM

    L CPalliniC 2 Replies Last reply
    0
    • M monsieur_jj

      Hi all, Is there a c++ version of this or at least can a c++ version of this algorithm can be made? Please advise. Can anyone give me a sample on how to use windows crypto api decryption just by inputting an encrypted string? Thanks, Jayjay btw this is made of c#

      public static string Decrypt(string cipherString, bool useHashing)
      {
      byte[] keyArray;
      //get the byte code of the string

          byte\[\] toEncryptArray = Convert.FromBase64String(cipherString);
      
          System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
          //Get your key from config file to open the lock!
          string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
      
          if (useHashing)
          {
              //if hashing was used get the hash code with regards to your key
              MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
              keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
              //release any resource held by the MD5CryptoServiceProvider
      
              hashmd5.Clear();
          }
          else
          {
              //if hashing was not implemented get the byte code of the key
              keyArray = UTF8Encoding.UTF8.GetBytes(key);
           }
      
          TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
          //set the secret key for the tripleDES algorithm
          tdes.Key = keyArray;
          //mode of operation. there are other 4 modes.
          //We choose ECB(Electronic code Book)
      
          tdes.Mode = CipherMode.ECB;
          //padding mode(if any extra byte added)
          tdes.Padding = PaddingMode.PKCS7;
      
          ICryptoTransform cTransform = tdes.CreateDecryptor();
          byte\[\] resultArray = cTransform.TransformFinalBlock
                  (toEncryptArray, 0, toEncryptArray.Length);
          //Release resources held by TripleDes Encryptor
          tdes.Clear();
          //return the Clear decrypted TEXT
          return UTF8Encoding.UTF8.GetString(resultArray);
      }
      

      modified on Thursday, November 20, 2008 3:28 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Bonjour JayJay, Yes that function can be implemented in C++ using Microsoft Cryptographic Service Providers.[^] From looking at the source code you have provided it appears that the function works as follows: 1.) Function is passed a base64 encoded bytes and a boolean. 2.) Decode base64 encoded bytes to a byte array. 3.) Open a "settings reader" and retrieve a key/pair value from key 'SecurityKey'. 4.) if useHashing boolean is true then create a MD5 hash from the "SecurityKey"value. 5.) if useHashing boolean is false then cast the "SecurityKey"value to a UTF8 encoded unicode string. 6.) Decrypt the byte array using the 'Security' key with TripleDES algorithm. You should research the following funtions from the Microsoft Cryptographic Service. For Initializing Microsoft Cryptographic Service Providers: CryptAcquireContext Function[^] For base64 decoding: CryptBinaryToString Function [^] For computing MD5 hash: CryptCreateHash Function[^] CryptDestroyHash Function[^] For TripleDES: CryptDeriveKey Function[^] CryptDecrypt Function[^] CryptDestroyKey Function[

      M CPalliniC 3 Replies Last reply
      0
      • M monsieur_jj

        Hi all, Is there a c++ version of this or at least can a c++ version of this algorithm can be made? Please advise. Can anyone give me a sample on how to use windows crypto api decryption just by inputting an encrypted string? Thanks, Jayjay btw this is made of c#

        public static string Decrypt(string cipherString, bool useHashing)
        {
        byte[] keyArray;
        //get the byte code of the string

            byte\[\] toEncryptArray = Convert.FromBase64String(cipherString);
        
            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            //Get your key from config file to open the lock!
            string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
        
            if (useHashing)
            {
                //if hashing was used get the hash code with regards to your key
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                //release any resource held by the MD5CryptoServiceProvider
        
                hashmd5.Clear();
            }
            else
            {
                //if hashing was not implemented get the byte code of the key
                keyArray = UTF8Encoding.UTF8.GetBytes(key);
             }
        
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            //set the secret key for the tripleDES algorithm
            tdes.Key = keyArray;
            //mode of operation. there are other 4 modes.
            //We choose ECB(Electronic code Book)
        
            tdes.Mode = CipherMode.ECB;
            //padding mode(if any extra byte added)
            tdes.Padding = PaddingMode.PKCS7;
        
            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte\[\] resultArray = cTransform.TransformFinalBlock
                    (toEncryptArray, 0, toEncryptArray.Length);
            //Release resources held by TripleDes Encryptor
            tdes.Clear();
            //return the Clear decrypted TEXT
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
        

        modified on Thursday, November 20, 2008 3:28 AM

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        monsieur_jj wrote:

        Is there a c++ version of this

        Possibly, yes.

        monsieur_jj wrote:

        or at least can a c++ version of this algorithm can be made?

        This for sure, even the C# one is, at the end, done in C++. :rolleyes: Reference about Crypto API here. There are also articles about, here at CodeProject [^]. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        M 1 Reply Last reply
        0
        • L Lost User

          Bonjour JayJay, Yes that function can be implemented in C++ using Microsoft Cryptographic Service Providers.[^] From looking at the source code you have provided it appears that the function works as follows: 1.) Function is passed a base64 encoded bytes and a boolean. 2.) Decode base64 encoded bytes to a byte array. 3.) Open a "settings reader" and retrieve a key/pair value from key 'SecurityKey'. 4.) if useHashing boolean is true then create a MD5 hash from the "SecurityKey"value. 5.) if useHashing boolean is false then cast the "SecurityKey"value to a UTF8 encoded unicode string. 6.) Decrypt the byte array using the 'Security' key with TripleDES algorithm. You should research the following funtions from the Microsoft Cryptographic Service. For Initializing Microsoft Cryptographic Service Providers: CryptAcquireContext Function[^] For base64 decoding: CryptBinaryToString Function [^] For computing MD5 hash: CryptCreateHash Function[^] CryptDestroyHash Function[^] For TripleDES: CryptDeriveKey Function[^] CryptDecrypt Function[^] CryptDestroyKey Function[

          M Offline
          M Offline
          monsieur_jj
          wrote on last edited by
          #4

          Hi Randor, Thanks btw this is the encrypt code from where the string passed is derived:

          public static string Encrypt(string toEncrypt, bool useHashing)
          {
          byte[] keyArray;
          byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

                  System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
                  // Get the key from config file
                  string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
                  //System.Windows.Forms.MessageBox.Show(key);
                  if (useHashing)
                  {
                      MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                      keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                      hashmd5.Clear();
                  }
                  else
                      keyArray = UTF8Encoding.UTF8.GetBytes(key);
          
                  TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                  tdes.Key = keyArray;
                  tdes.Mode = CipherMode.ECB;
                  tdes.Padding = PaddingMode.PKCS7;
          
                  ICryptoTransform cTransform = tdes.CreateEncryptor();
                  byte\[\] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                  tdes.Clear();
                  return Convert.ToBase64String(resultArray, 0, resultArray.Length);
              }
          
          1 Reply Last reply
          0
          • L Lost User

            Bonjour JayJay, Yes that function can be implemented in C++ using Microsoft Cryptographic Service Providers.[^] From looking at the source code you have provided it appears that the function works as follows: 1.) Function is passed a base64 encoded bytes and a boolean. 2.) Decode base64 encoded bytes to a byte array. 3.) Open a "settings reader" and retrieve a key/pair value from key 'SecurityKey'. 4.) if useHashing boolean is true then create a MD5 hash from the "SecurityKey"value. 5.) if useHashing boolean is false then cast the "SecurityKey"value to a UTF8 encoded unicode string. 6.) Decrypt the byte array using the 'Security' key with TripleDES algorithm. You should research the following funtions from the Microsoft Cryptographic Service. For Initializing Microsoft Cryptographic Service Providers: CryptAcquireContext Function[^] For base64 decoding: CryptBinaryToString Function [^] For computing MD5 hash: CryptCreateHash Function[^] CryptDestroyHash Function[^] For TripleDES: CryptDeriveKey Function[^] CryptDecrypt Function[^] CryptDestroyKey Function[

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Randor wrote:

            Hope it helps!

            If it doesn't, it is not your fault, for sure. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • L Lost User

              Bonjour JayJay, Yes that function can be implemented in C++ using Microsoft Cryptographic Service Providers.[^] From looking at the source code you have provided it appears that the function works as follows: 1.) Function is passed a base64 encoded bytes and a boolean. 2.) Decode base64 encoded bytes to a byte array. 3.) Open a "settings reader" and retrieve a key/pair value from key 'SecurityKey'. 4.) if useHashing boolean is true then create a MD5 hash from the "SecurityKey"value. 5.) if useHashing boolean is false then cast the "SecurityKey"value to a UTF8 encoded unicode string. 6.) Decrypt the byte array using the 'Security' key with TripleDES algorithm. You should research the following funtions from the Microsoft Cryptographic Service. For Initializing Microsoft Cryptographic Service Providers: CryptAcquireContext Function[^] For base64 decoding: CryptBinaryToString Function [^] For computing MD5 hash: CryptCreateHash Function[^] CryptDestroyHash Function[^] For TripleDES: CryptDeriveKey Function[^] CryptDecrypt Function[^] CryptDestroyKey Function[

              M Offline
              M Offline
              monsieur_jj
              wrote on last edited by
              #6

              Bonjour David, I am a bit confused should I use CryptBinaryToString or CryptStringtoBinary? Thanks, Jayjay

              L 1 Reply Last reply
              0
              • M monsieur_jj

                Bonjour David, I am a bit confused should I use CryptBinaryToString or CryptStringtoBinary? Thanks, Jayjay

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Use CryptStringToBinary to decode from base64. Use CryptBinaryToString to encode to base64. Best Wishes, -David Delaune

                M 1 Reply Last reply
                0
                • CPalliniC CPallini

                  monsieur_jj wrote:

                  Is there a c++ version of this

                  Possibly, yes.

                  monsieur_jj wrote:

                  or at least can a c++ version of this algorithm can be made?

                  This for sure, even the C# one is, at the end, done in C++. :rolleyes: Reference about Crypto API here. There are also articles about, here at CodeProject [^]. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  M Offline
                  M Offline
                  monsieur_jj
                  wrote on last edited by
                  #8

                  message deleted

                  1 Reply Last reply
                  0
                  • L Lost User

                    Use CryptStringToBinary to decode from base64. Use CryptBinaryToString to encode to base64. Best Wishes, -David Delaune

                    M Offline
                    M Offline
                    monsieur_jj
                    wrote on last edited by
                    #9

                    Hi, I did a translation test and here it is unfortunately it ends up garbage:

                    void ThreeDESdecrypt(unsigned char *cyphertext,unsigned long ctlen,unsigned char *passwd,unsigned long pwlen,unsigned char *plaintext,unsigned long *ptlen)
                    {
                    HCRYPTPROV hProv = NULL;
                    HCRYPTHASH hHash = NULL;
                    HCRYPTKEY hKey = NULL;

                    BYTE * value2 = {0};
                    DWORD lul_len, lul_buflen, lul_skip, lul_pflags;
                    bool lb_rtn;
                    BLOB lblob_data;
                    // value = reinterpret_cast<const BYTE *>(cyphertext);
                    TCHAR convertedStr[13];
                    convertedStr[0] = ('\0');
                    BYTE key1[24] = {0};

                    lul\_len = 13;
                    lul\_buflen = (lul\_len \* 2);
                    std::string value = "Zed5OmjUWs8=";
                    hProv = InitializeCrypt(); 
                    bool result = CryptStringToBinary(value.c\_str(), lul\_len, CRYPT\_STRING\_BASE64, key1, &lul\_buflen, &lul\_skip, NULL);
                    

                    //bool result = CryptAcquireContext(&hProv,SecurityKey,NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
                    //bool result = CryptBinaryToString(value, lul_len, CRYPT_STRING_BASE64, convertedStr, &lul_buflen);
                    result = CryptCreateHash(hProv,CALG_MD5,0,0,&hHash);
                    result = CryptDeriveKey(hProv,CALG_3DES,hHash,0,&hKey);
                    memcpy(plaintext,key1,*ptlen);
                    result = CryptDecrypt(hKey,NULL,1,0,plaintext,&ctlen);
                    *ptlen=ctlen;
                    result = CryptDestroyKey(hKey);
                    result = CryptDestroyHash(hHash);
                    result = CryptReleaseContext(hProv,0);
                    }

                    int main() {

                    HCRYPTPROV hCryptProv;// = InitializeCrypt();

                    HCRYPTHASH hHash = 0;
                    HCRYPTKEY hKey = 0;
                    PBYTE pbBuffer = NULL;
                    DWORD dwCount;

                    const BYTE \* value;
                    DWORD lul\_len, lul\_buflen, lul\_skip, lul\_pflags, ptlen;
                    bool lb\_rtn;
                    BLOB lblob\_data;
                    
                    std::string key = "h3bmull3r";
                    const std::string s = "Zed5OmjUWs8=" ;
                    value = reinterpret\_cast&lt;const BYTE \*&gt;(s.c\_str());
                    int stringSize = s.size();
                    TCHAR convertedStr\[12\];
                    convertedStr\[0\] = ('\\0');
                    
                    lul\_len = s.size();
                    lul\_buflen = (lul\_len \* 2);
                    
                    //hCryptProv = InitializeCrypt();
                    
                    char CypherText\[128\]="Zed5OmjUWs8="; 
                    char MyPassword\[\]="h3bmull3r"; 
                    unsigned char MyString\[128\];
                    unsigned long len1,len2,len3;
                    
                    len1=strlen(CypherText);
                    len2=strlen(MyPassword);
                    len3=128; //size of the cypehrtext buffer above
                    
                    memset(MyString,0,128); //clear the buffer
                    len1=128; //size of the plaintext buffer
                    lb\_rtn = CryptBinaryToString(value, lul\_len, CRYPT\_STRING\_BASE64, convertedStr, &amp;lul\_buflen);
                    
                    ThreeDESdecrypt((unsigned char \*)CypherText,len3,(unsigned char \*)MyPassword,len2,(unsigned char \*)MyString,&amp;len1);
                    

                    ret

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups