473,220 Members | 2,276 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,220 software developers and data experts.

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

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jase H | last post by:
Hello, I have a ASP.NET web application problem involving the data encryption and decryption assembly(DLL) used on the connection string value that is set in the webconfig file. The problem...
2
by: Dave Bailey | last post by:
I have developed a web app using DPAPI to encrypt a connection string in the web.config file. The application works perfectly on the development machine but when deployed to the server when...
0
by: sushant.bhatia | last post by:
Hi All. I'm using the NCrypto dll for RSA Encryption/Decryption (http://sourceforge.net/projects/ncrypto/). My encryption code in .Net is pretty simple. The dataToEncrypt length is 1024. The...
2
by: almurph | last post by:
Hi everyone, Can you help me please? I am having a problem with the encryption/decryption of words with the Irish fada in them. The Irish fada is like this: /. It's kind of like the...
1
by: ppuniversal | last post by:
Hello, I am making a DES encryption/decryption program using OpenSSL library. I am using function des_ecb_encrypt(des_cblock *input, des_cblock *output, des_cblock *keysched, int mode); ...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
3
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
9
by: Alan M Dunsmuir | last post by:
In my (PHP-5) application I have to write some records to a table in my database, which I don't want even my clients using the system to be able to read. This is not a problem in National...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.