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

Posted on 2005-04-25
Last Modified: 2012-08-13
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!
Question by:Ike23
    LVL 30

    Assisted Solution

    You can look at the windows CryptoAPI functions.
    Example code:
    DWORD EZ_Crypto::EncryptString(const CString& szPassword,CString& szEncryptPwd,const CString& PublicKey)
        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 = "FAILED";
                            bResult = -1;                        
                        CryptDestroyKey(hKey);  // Release provider handle.                
                        // Error during CryptDeriveKey!                    
                        bResult = -1;                
                    // Error during CryptHashData!                
                    bResult = -1;            
                // Destroy session key.        
                // Error during CryptCreateHash!            
                bResult = -1;        
            CryptReleaseContext(hProv, 0);    
        return bResult;

    LVL 86

    Accepted Solution

    There are a lot of classes/code out there that can do that for you, e.g. ("ARACrypt - a crypto class"):

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

    Crypt.TransformString( Key, sSecretString); ("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
    Crypt(HelloCrypt, _tcslen(HelloCrypt));

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now