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);
}