I have done XOR encryption in C. Its working fine. Now i need to dohigher encryption standards such as RSA. How can i do that. I tried but its failing. Please help.

Note: I have posted my code now. please help

Printable View

- 09-18-2006sankarvHow to do encryption in C
I have done XOR encryption in C. Its working fine. Now i need to dohigher encryption standards such as RSA. How can i do that. I tried but its failing. Please help.

Note: I have posted my code now. please help - 09-18-2006Dave_Sinkula
Hm. Try to be more vague.

Or else post your code.

Or... Search the 'net (visit Wikipedia). Make another attempt. And then post your code with specific comments about issues with the code that you don't understand.

Or wait a couple of weeks until the psychics get their crystal balls fixed. - 09-18-2006sankarv
http://www.di-mgt.com.au/rsa_alg.html#simpleexample

I referred to the above page and here is my code:

please notify whats actually wrong in this code.

Code:`#include <stdio.h>`

#include<stdlib.h>

int main()

{

long long count,bytes; /* counter when looping through file, and bytes to count filesize */

FILE *in,*out,*in2; /* In and out FILE Streams to read/write data */

int i=2;

if (( in = fopen("D:/cfiles/cfiles/cfiles/sort.c", "rb")) == NULL) /* Error Check File Streams */

{

printf("Error opening file for reading");

}

if (( out = fopen("D:/cfiles/cfiles/cfiles/sort1.1c", "wb")) == NULL)

{

printf("Error opening file for writing");;

}

while(( count = getc(in)) != EOF)

{

count = (count ^ 3) % 33; /*Encryption from the RSA */

bytes++; /* Increment counter of filesize */

putc(count, out); /* Write new file */

}

fclose(in);

fclose(out);

printf("Encryption Success:");

printf("Encrypted and stored data ");

printf("Wrote %d bytes to sort1.1c", bytes);

if (( in = fopen("D:/cfiles/cfiles/cfiles/sort1.1c", "rb")) == NULL) /* Error Check File Streams */

{

printf("Error opening file for reading");

}

if (( out = fopen("D:/cfiles/cfiles/cfiles/sort1.c", "wb")) == NULL) /* Error Check File Streams */

{

printf("Error opening file for writing");

}

while(( count = getc(in)) != EOF)

{

count = (count ^ 7) % 33 ; /* Decryption from the RSA */

bytes++; /* Increment counter of filesize */

putc(count, out); /* Write new file */

}

fclose(in);

fclose(out);

printf("Decryption done");

system("PAUSE");

return 0;

}

Also please help me how to form a generalized code for RSA instead of taking from the example as i did. - 09-18-2006Dave_SinkulaQuote:

Originally Posted by**sankarv**

Quote:

PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software 1985-2006

--- Module: test.c (C)

test.c 20 error [Warning 613] Possible use of null pointer 'in' in left argument to operator '->' [Reference: file test.c: line 11]

test.c 20 error [Warning 613] Possible use of null pointer 'in' in left argument to operator '->' [Reference: file test.c: line 11]

test.c 25 error [Warning 530] Symbol 'bytes' (line 7) not initialized

test.c 26 error [Warning 613] Possible use of null pointer 'out' in left argument to operator '->' [Reference: file test.c: line 15]

test.c 26 error [Warning 613] Possible use of null pointer 'out' in left argument to operator '->' [Reference: file test.c: line 15]

test.c 20 error [Warning 613] Possible use of null pointer 'in' in left argument to operator '->' [Reference: file test.c: line 11]

test.c 20 error [Warning 613] Possible use of null pointer 'in' in left argument to operator '->' [Reference: file test.c: line 11]

test.c 29 error [Warning 668] Possibly passing a null pointer to function 'fclose(FILE *)', arg. no. 1 [Reference: file test.c: line 11]

test.c 30 error [Warning 668] Possibly passing a null pointer to function 'fclose(FILE *)', arg. no. 1 [Reference: file test.c: line 15]

test.c 33 error [Warning 559] Size of argument no. 2 inconsistent with format

test.c 46 error [Warning 539] Did not expect positive indentation from line 40

test.c 46 error [Warning 613] Possible use of null pointer 'in' in left argument to operator '->' [Reference: file test.c: line 35]

test.c 46 error [Warning 613] Possible use of null pointer 'in' in left argument to operator '->' [Reference: file test.c: line 35]

test.c 47 error [Warning 525] Negative indentation from line 46

test.c 51 error [Warning 613] Possible use of null pointer 'out' in left argument to operator '->' [Reference: file test.c: line 40]

test.c 51 error [Warning 613] Possible use of null pointer 'out' in left argument to operator '->' [Reference: file test.c: line 40]

test.c 52 error [Warning 525] Negative indentation from line 46

test.c 46 error [Warning 613] Possible use of null pointer 'in' in left argument to operator '->' [Reference: file test.c: line 35]

test.c 46 error [Warning 613] Possible use of null pointer 'in' in left argument to operator '->' [Reference: file test.c: line 35]

test.c 54 error [Warning 668] Possibly passing a null pointer to function 'fclose(FILE *)', arg. no. 1 [Reference: file test.c: line 35]

test.c 55 error [Warning 668] Possibly passing a null pointer to function 'fclose(FILE *)', arg. no. 1 [Reference: file test.c: line 40]

test.c 58 error [Warning 534] Ignoring return value of function 'system(const char *)' (compare with line 148, file c:\progra~1\borland\bcc55\include\stdlib.h)

test.c 60 error [Warning 529] Symbol 'in2' (line 8) not subsequently referenced

test.c 60 error [Warning 529] Symbol 'i' (line 9) not subsequently referenced

- 09-18-2006sankarv
Its really surprising.

I am getting this as output.

Quote:

gcc -pedantic -Os -c en.c -o en.o -std=c99

>Exit code: 0

>en

Encryption Success:Encrypted and stored data Wrote 1582 bytes to sort1.1c Decryption done>Exit code: 0

Press any key to continue . . .

The code is working for XOR encryption but for RSA its not working. Please help me logic wise as i think i am somewhere wrong in the logic. - 09-19-2006sankarv
pls help

- 09-19-2006whiteflags
Did you actually fix the problems lint noticed?

this too: http://c-faq.com/stdio/fflush.html - 09-19-2006Adak
This is what I found googling on RSA encryption. I don't see this kind of code in your program.

I believe you need to SHOW the output of your program, (all of it), and then you can see what's really going on. There's some big numbers here, how are you avoiding overflowing your datatype?

Adak

It's a pretty thorough example, I believe.

An Example of the RSA Algorithm

P = 61 <- first prime number (destroy this after computing E and D)

Q = 53 <- second prime number (destroy this after computing E and D)

PQ = 3233 <- modulus (give this to others)

E = 17 <- public exponent (give this to others)

D = 2753 <- private exponent (keep this secret!)

Your public key is (E,PQ).

Your private key is D.

The encryption function is:

encrypt(T) = (T^E) mod PQ

= (T^17) mod 3233

The decryption function is:

decrypt(C) = (C^D) mod PQ

= (C^2753) mod 3233

To encrypt the plaintext value 123, do this:

encrypt(123) = (123^17) mod 3233

= 337587917446653715596592958817679803 mod 3233

= 855

To decrypt the ciphertext value 855, do this:

decrypt(855) = (855^2753) mod 3233

= 123

One way to compute the value of 855^2753 mod 3233 is like this:

2753 = 101011000001 base 2, therefore

2753 = 1 + 2^6 + 2^7 + 2^9 + 2^11

= 1 + 64 + 128 + 512 + 2048

Consider this table of powers of 855:

855^1 = 855 (mod 3233)

855^2 = 367 (mod 3233)

855^4 = 367^2 (mod 3233) = 2136 (mod 3233)

855^8 = 2136^2 (mod 3233) = 733 (mod 3233)

855^16 = 733^2 (mod 3233) = 611 (mod 3233)

855^32 = 611^2 (mod 3233) = 1526 (mod 3233)

855^64 = 1526^2 (mod 3233) = 916 (mod 3233)

855^128 = 916^2 (mod 3233) = 1709 (mod 3233)

855^256 = 1709^2 (mod 3233) = 1282 (mod 3233)

855^512 = 1282^2 (mod 3233) = 1160 (mod 3233)

855^1024 = 1160^2 (mod 3233) = 672 (mod 3233)

855^2048 = 672^2 (mod 3233) = 2197 (mod 3233)

Given the above, we know this:

855^2753 (mod 3233)

= 855^(1 + 64 + 128 + 512 + 2048) (mod 3233)

= 855^1 * 855^64 * 855^128 * 855^512 * 855^2048 (mod 3233)

= 855 * 916 * 1709 * 1160 * 2197 (mod 3233)

= 794 * 1709 * 1160 * 2197 (mod 3233)

= 2319 * 1160 * 2197 (mod 3233)

= 184 * 2197 (mod 3233)

= 123 (mod 3233)

= 123

If you have a computer program (such as the "bc" utility that comes with Linux),

you can compute 855^2753 mod 3233 directly, like this:

855^2753 mod 3233

= 50432888958416068734422899127....<ad nauseum>....021484375 mod 3233

= 123 - 09-19-2006whiteflags
You know, that giant example looks really unnecessary.

And it's not even a hex dump. - 09-19-2006sankarv
The encryption function is:

Quote:

encrypt(T) = (T^E) mod PQ

= (T^17) mod 3233

The decryption function is:

decrypt(C) = (C^D) mod PQ

= (C^2753) mod 3233

Hi Adak. Instead of this i used

Encrypt=(T^3) % 33

Decrypt=(C^7) % 33

Please see the link i specified above the code.

Anyway how will u code what u have presented here?

Anybody please explain how to code that... - 09-19-2006zx-1
The "^"s are meant to signify exponentiation, not bitwise-XOR.

- 09-19-2006jafetCode:
`count = (count ^ 3) % 33; /*Encryption from the RSA */`

If you want real industrial-grade RSA encryption, ints are not large enough for the calculations. You'd want to invest in bignum computation libraries like apfloat and GMP. - 09-19-2006sankarv
Then wat can we use for XOR and whats the code for implementing this?

pls help... - 09-19-2006Salem
count = (count ^ 3) % 33

Would be

count = pow(count,3) % 33 - 09-19-2006AdakQuote:

Originally Posted by**sankarv**

I'm toast, and MUST get some zzz's. I'll take another look at your link tomorrow, at first glance, it has good stuff on it, but I didn't see any actual code, at all.

The thing that struck me immediately is that BIG BIG BIG number that is the result of the exponentiation. And the "^" is the exponentiation sign NOT XOR, as the above poster kindly pointed out.

Do you have a function to handle super large numbers? Why don't you google for this, and I'll check in with you in about 10 hours from now. (2:00 pm, Pacific Daylight Time).

Another resource I'd like you to check is the newsgroup sci.crypto (iirc), they're WAY into this kind of thing.

Adak