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
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
Last edited by sankarv; 09-18-2006 at 10:13 PM.
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.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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.
Last edited by sankarv; 09-18-2006 at 10:30 PM.
Some lint...Originally Posted by sankarv
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
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Its really surprising.
I am getting this as output.
For me it runs without error but its not producing proper output on decryption.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.
Last edited by sankarv; 09-18-2006 at 10:32 PM.
pls help
Did you actually fix the problems lint noticed?
this too: http://c-faq.com/stdio/fflush.html
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
Last edited by Salem; 09-19-2006 at 05:27 AM. Reason: snip pointlessly long number
You know, that giant example looks really unnecessary.
And it's not even a hex dump.
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
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...
Last edited by sankarv; 09-19-2006 at 03:37 AM.
The "^"s are meant to signify exponentiation, not bitwise-XOR.
System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.
Useful resources:
comp.lang.c FAQ | C++ FQA Lite
The ^ isn't doing what you think. It's the XOR operator, not exponentiation. Which makes me doubt whether you actually wrote an XOR encryption program in the first place.Code: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.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
Then wat can we use for XOR and whats the code for implementing this?
pls help...
count = (count ^ 3) % 33
Would be
count = pow(count,3) % 33
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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