• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

Need to encrypt a string in a C++ program...

I need to encryt a string in a C++ program.  The inputs should be the string and the key and the output should be the encrypted string.  So I'm looking for an ActiveX control or some standard windows API that I can call like:
 
encrypt("My String to be encrypted", "MyKey") and it would come out SsKLekjskmv<D or whatever.  

Thanks in advance!
0
Ike23
Asked:
Ike23
2 Solutions
 
AxterCommented:
You can look at the windows CryptoAPI functions.
Example code:
DWORD EZ_Crypto::EncryptString(const CString& szPassword,CString& szEncryptPwd,const CString& PublicKey)
{    
    LOGFUNCTION("EZ_Crypto::EncryptString");
    const CString szKey = PublicKey + PrivateKey;
    int bResult = -1;    
    HKEY hRegKey = NULL;    
    HCRYPTPROV hProv = NULL;    
    HCRYPTKEY hKey = NULL;
    HCRYPTKEY hXchgKey = NULL;    
    HCRYPTHASH hHash = NULL;    
    DWORD dwLength;
    // Get handle to user default provider.
    if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))    
    {
        // Create hash object.        
        if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))        
        {
            // Hash password string.            
            if (CryptHashData(hHash, (BYTE *)(LPCTSTR)szKey, szKey.GetLength(), 0))            
            {
                // Create block cipher session key based on hash of the password.
                if (CryptDeriveKey(hProv, MY_ENCRYPT, hHash, CRYPT_EXPORTABLE, &hKey))                
                {
                    // Determine number of bytes to encrypt at a time.
                    dwLength = szPassword.GetLength();    
                    szEncryptPwd = szPassword;
                    BYTE *pbBuffer = (BYTE *)szEncryptPwd.GetBuffer(dwLength*2);                    
                    // Encrypt data
                    if (CryptEncrypt(hKey, 0, TRUE, 0, pbBuffer, &dwLength, dwLength*2))                        
                    {
                        bResult = dwLength;
                        szEncryptPwd.ReleaseBuffer(dwLength);
                    }    
                    else                        
                    {
                        szEncryptPwd.ReleaseBuffer();
                        szEncryptPwd = "FAILED";
                        bResult = -1;                        
                    }                        
                    CryptDestroyKey(hKey);  // Release provider handle.                
                }                
                else                
                {
                    // Error during CryptDeriveKey!                    
                    bResult = -1;                
                }            
            }            
            else            
            {
                // Error during CryptHashData!                
                bResult = -1;            
            }
            CryptDestroyHash(hHash);
            // Destroy session key.        
        }        
        else        
        {
            // Error during CryptCreateHash!            
            bResult = -1;        
        }
        CryptReleaseContext(hProv, 0);    
    }    
    return bResult;
}

0
 
jkrCommented:
There are a lot of classes/code out there that can do that for you, e.g.

http://www.codeproject.com/cpp/aracrypt.asp ("ARACrypt - a crypto class"):

ARACrypt Crypt;
CString Key = "MyKey";
CString sSecretString = "My info";

Crypt.TransformString( Key, sSecretString);

http://www.codeproject.com/cpp/crypt_routine.asp ("Fast Simple Secure Encryption Routine")

//to encrypt a text
TCHAR HelloCrypt[] = "Hello Crypt";

//we do not enter the key so we will be using default key provide by the crypt
//routine
Crypt(HelloCrypt, _tcslen(HelloCrypt));

//now to decrypt
Crypt(HelloCrypt, _tcslen(HelloCrypt));
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now