Thread: Ask about Encryption and Decryption

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    Lightbulb Ask about Encryption and Decryption

    I want to ask about how to use public key to encrypt data and use private key to decrypt data.Now I use Visual C++ 6.0 to compile program.This below is some part of program. Function Decrypt is used for encrypt data by private key and function Encrypt is used for decrypt data by public key.But I want to use public key to encrypt data and use private key to decrypt data. If you know how to do that of know some information, please tell me. Thank you for your kind.

    Raka Sukjai

    void CCrypt:ecrypt(CArray<unsigned char, unsigned char>& message)
    {
    if (!m_isKeyLoaded) throw "No key loaded";
    if (m_rsaKey.isPublicKey) throw "No private components";
    if (message.GetSize() == 0) return;

    CRYPT_CONTEXT cryptContext;
    long error, iSrc, iDest, nZeros, nRounds;
    long keyLength = bitsToBytes(m_rsaKey.nLen);
    CArray<unsigned char, unsigned char> tempData;

    nRounds = (message.GetSize()+keyLength) / (keyLength-1);
    tempData.SetSize(nRounds * keyLength);
    tempData[0] = 'R';
    tempData[1] = 'S';

    nZeros = nRounds*(keyLength-1) - message.GetSize() - 2;
    memset(tempData.GetData()+2, 0, nZeros); // Zeros are padded
    iDest = nZeros + 2; // +2 for 'R' and 'S'
    iSrc = 0;
    cryptCreateContext(&cryptContext, CRYPT_ALGO_RSA, CRYPT_MODE_PKC);
    error = cryptLoadKey(cryptContext, &m_rsaKey, CRYPT_UNUSED);
    // 1st round
    if (iDest%keyLength != 0) { // If 'A' can be added in the 1st round
    tempData[iDest++] = 'A';
    for ( ; iDest%keyLength!=0; iSrc++, iDest++) tempData[iDest] = message[iSrc];
    error = cryptDecrypt(cryptContext, tempData.GetData(), CRYPT_USE_DEFAULT);
    }
    // Special case: if 'A' is added next to 'R' in the second round,
    // run "cryptDecrypt" twice (1st and 2nd round)
    else {
    error = cryptDecrypt(cryptContext, tempData.GetData(), CRYPT_USE_DEFAULT);
    tempData[iDest] = 'R';
    tempData[iDest+1] = 'A';
    memcpy(tempData.GetData()+iDest+2, message.GetData()+iSrc, keyLength-2);
    error = cryptDecrypt(cryptContext, tempData.GetData()+iDest, CRYPT_USE_DEFAULT);
    iDest += keyLength;
    iSrc += keyLength - 2;
    }
    // next round
    while (iSrc < message.GetSize()) {
    tempData[iDest] = 'R';
    memcpy(tempData.GetData()+iDest+1, message.GetData()+iSrc, keyLength-1);
    error = cryptDecrypt(cryptContext, tempData.GetData()+iDest, CRYPT_USE_DEFAULT);
    iDest += keyLength;
    iSrc += keyLength - 1;
    }
    cryptDestroyContext(cryptContext);
    message.Copy(tempData);
    }



    void CCrypt::Encrypt(CArray<unsigned char, unsigned char>& message)
    {
    if (!m_isKeyLoaded) throw "No key loaded";

    CRYPT_CONTEXT cryptContext;
    long error, i, nRounds, nZeros, iDest;
    long keyLength = bitsToBytes(m_rsaKey.nLen);
    if (message.GetSize()%keyLength != 0) throw "Invalid message";
    CArray<unsigned char, unsigned char> buffer, tempData;

    nRounds = message.GetSize() / keyLength;
    buffer.SetSize(keyLength);
    cryptCreateContext(&cryptContext, CRYPT_ALGO_RSA, CRYPT_MODE_PKC);
    error = cryptLoadKey(cryptContext, &m_rsaKey, CRYPT_UNUSED);
    // 1st round
    memcpy(buffer.GetData(), message.GetData(), keyLength);
    error = cryptEncrypt(cryptContext, buffer.GetData(), CRYPT_USE_DEFAULT);
    if (buffer[0]!='R' || buffer[1]!='S') {
    cryptDestroyContext(cryptContext);
    throw "Incorrect key or message";
    }
    nZeros = 0; // Number of zeros
    iDest = 0;

    while (nZeros+2<buffer.GetSize() && buffer[nZeros+2]!='A') nZeros++; // Search for 'A'

    tempData.SetSize( nRounds*(keyLength-1) - 2 - nZeros);
    if (nZeros+2 < buffer.GetSize()) { // If 'A' is in the first round
    memcpy(tempData.GetData(), buffer.GetData()+nZeros+3, keyLength-nZeros-3);
    iDest += keyLength - nZeros - 3; // 3 -> 'R','S' and 'A'
    i = 1;
    }
    // Special case: if 'A' is next to 'R' in the second round,
    // run "cryptDecrypt" for the 2nd round
    else {
    memcpy(buffer.GetData(), message.GetData()+keyLength, keyLength);
    error = cryptEncrypt(cryptContext, buffer.GetData(), CRYPT_USE_DEFAULT);
    if (buffer[0]!='R' || buffer[1]!='A') {
    cryptDestroyContext(cryptContext);
    throw "Corrupted message";
    }
    memcpy(tempData.GetData()+iDest, buffer.GetData()+2, keyLength-2);
    iDest += keyLength - 2;
    i = 2; // Because the 2nd round has been run, the next is the 3rd round
    }
    // next round
    for ( ; i<nRounds; i++, iDest += keyLength-1) {
    memcpy(buffer.GetData(), message.GetData()+(i*keyLength), keyLength);
    error = cryptEncrypt(cryptContext, buffer.GetData(), CRYPT_USE_DEFAULT);
    if (buffer[0]!='R') {
    cryptDestroyContext(cryptContext);
    throw "Corrupted message";
    }
    memcpy(tempData.GetData()+iDest, buffer.GetData()+1, keyLength-1);
    }
    cryptDestroyContext(cryptContext);
    message.Copy(tempData);
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    this is the "C board", try posting on the "C++ board".
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by bigtamscot
    this is the "C board", try posting on the "C++ board".
    and don't forget to use code tags!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    code tags next time, please...look at Hammer's sig!
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. encryption / decryption program
    By epidemic in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2008, 06:40 AM
  3. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  4. Encryption and Decryption Program in C++ using a class
    By goron350 in forum C++ Programming
    Replies: 7
    Last Post: 06-05-2005, 09:29 PM
  5. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM