473,220 Members | 2,276 Online

# RSA encryption decryption function problem

I have written a RSA encryption/decryption function in c using the formula a = b^c %n. For solving the equation I have used Squaring and multiplying method for modulo exponentiation .

These functions work fine when Two random prime numbers (required to generate public n private key) are within certain range but fails afterwords.All the variables are of datatype ULONGLONG. I m unable to fix up the problem. Is these really a storage problem (exceeds when perform squaring ??) or some thing else ???the variable pPrivateKey->ullSecretExponent comes as 9-10 digits.The variable pPrivateKey->ullModulus = product of two 32 bits ULONG variable.MAX_LENGTH = 64.please help.
my code for decryption, encryption are::

ULONGLONG DecryptNumber(ULONGLONG ullCipherText, RSA_PRIVATE_KEY *pPrivateKey)
{
int iBitLength = 0;
int iBitArr[MAX_LENGTH];
ULONGLONG ullLocal = 0;
ULONGLONG ullLocal1 = 0;
ULONGLONG ullDecryptedText = 0;

ullDecryptedText = 1;
::SecureZeroMemory(iBitArr,MAX_LENGTH);
DecimalToBinary(pPrivateKey->ullSecretExponent, &iBitLength, iBitArr); //Evaluate binary of
//secret exponent
// evaluate decrypted text = pow(Encrypted text,secret exponent of private key) % modulus of key
for(int iCount = iBitLength - 1; iCount >= 0; iCount--) // square n multiply method to
//evaluate exponentiation effitiently
{
//ullLocal = ullDecryptedText*ullDecryptedText;
//ullDecryptedText = ullLocal % pPrivateKey->ullModulus;
ullLocal = (ullDecryptedText % pPrivateKey->ullModulus);
ullDecryptedText = (ullLocal * ullLocal) % pPrivateKey->ullModulus;
if(1 == iBitArr[iCount])
{
//ullLocal = ullDecryptedText * ullCipherText;
//ullDecryptedText = ullLocal % pPrivateKey->ullModulus;
ullLocal = (ullDecryptedText % pPrivateKey->ullModulus);
ullLocal1 = (ullCipherText % pPrivateKey->ullModulus);
ullDecryptedText = (ullLocal * ullLocal1) % pPrivateKey->ullModulus;
}
}
return ullDecryptedText;
}

ULONGLONG EncryptNumber(int iNumToBeEncrypted, RSA_PUBLIC_KEY *pPublicKey)
{
int iBitLength = 0;
int iBitArr[MAX_LENGTH];
ULONGLONG ullLocal = 0;
ULONGLONG ullLocal1 = 0;
ULONGLONG ullEncriptedText = 1;

::SecureZeroMemory(iBitArr, MAX_LENGTH);
DecimalToBinary(pPublicKey->ullPublicExponent, &iBitLength, iBitArr); //Evaluate binary of
//public exponent

// evaluate encrypted text = pow(plain text,public exponent of public key) % modulus of key
for(int iCount = iBitLength - 1; iCount >= 0; iCount--) // square n multiply method to
//evaluate exponentiation effitiently
{
//ullLocal = ullEncriptedText*ullEncriptedText;
//ullEncriptedText=ullLocal%pPublicKey->ullModulus;
ullLocal = ullEncriptedText % pPublicKey->ullModulus;
ullEncriptedText = (ullLocal * ullLocal) % pPublicKey->ullModulus;
if(1 == iBitArr[iCount])
{
//ullLocal = ullEncriptedText*iNumToBeEncrypted;
//ullEncriptedText = ullLocal %pPublicKey->ullModulus;
ullLocal = (ullEncriptedText % pPublicKey->ullModulus);
ullLocal1 = (iNumToBeEncrypted % pPublicKey->ullModulus);
ullEncriptedText = (ullLocal * ullLocal1) % pPublicKey->ullModulus;
}
}
return ullEncriptedText;
}

void EncryptAndDecrypt(TCHAR *pchPlainText, TCHAR *pchEncryptedFile, TCHAR *pchDecryptedFile,
RSA_PUBLIC_KEY *pPublicKey, RSA_PRIVATE_KEY *pPrivateKey)
{
ULONGLONG ulCipherText = 0;
ULONGLONG iDecriptText = 0;
int iConvertedInt = 0;
//FILE *fpEncrypted = NULL;
//FILE *fpDecrypted = NULL;

if(pchPlainText == NULL)
{
_tprintf(TEXT("No text to be available to be encrypted.\n"));
}
while('\0' != (*pchPlainText))
{
if(IsNumber(*pchPlainText))
{
iConvertedInt = (int)(*pchPlainText) - '0';
}
else
{
iConvertedInt = (int)(*pchPlainText);
}
ulCipherText = EncryptNumber(iConvertedInt,pPublicKey);
iDecriptText = DecryptNumber(ulCipherText,pPrivateKey);
pchPlainText++;
}
}
Jan 18 '08 #1
0 2340