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
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
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.
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.
Some lint...Quote:
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
Its really surprising.
I am getting this as output.
For me it runs without error but its not producing proper output on decryption.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.
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
You know, that giant example looks really unnecessary.
And it's not even a hex dump.
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...
The "^"s are meant to signify exponentiation, not bitwise-XOR.
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.
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
Quote:
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
what i actually need is the c code for rsa algorithm. May be its of any of these examples in this topic....
pls help....
You're looking at the code man.
What the real problem will be for you is that RSA deals with much much bigger numbers than can be represented using normal C datatypes. For that you need a bignum package like GMP
However, before you move up to that, you can at least prove to yourself that you UNDERSTAND the algorithms by practicing on the very small numbers used for examples.
All you seem to be doing is whining about it.
If you're not even going to post your attempt at using pow(), and showing the code / results you get, then forget about the crypto side.
If you just want to download a lib to do it all for you, then do it and stop pretending that you can write it yourself.
I haven't been able to find exact code for RSA encryption, but this link should be enough to get you on the right path with it.Quote:
Originally Posted by sankarv
Understand that as Salem mentioned, you'll HAVE to use a library or function to handle the HUGE numbers involved in working with RSA.
Work through the examples by hand until you understand just what's going on with it. No one can just sit down and code up a program that they don't have a fair understanding of. The computer can give us great answers, but it doesn't give us great algorithmic inspirations.
The link is:
http://www.di-mgt.com.au/rsa_alg.html#simpleexample
Adak
Quote:
Originally Posted by Salem
Its returning error as "invalid operands to binary %". What may be the reason for this?
Anyway i will try with big num tools as u said...
Thanks for the help.
could anyone please suggest me antoher type of encryption which can be done without the libraries? basic XOR encryption i already done. other than that.
pls help...
> invalid operands to binary %
You get this error because pow returns a double or float, and % only works with integers. Try using fmod instead.
i corrected that and no error is coming but even then its not working . may be the problem with big numbers.
There are MANY kinds of encryption.. Much more than could be run by you in a post.Quote:
Originally Posted by sankarv
Why don't you google for "Mayfair encryption", and see if that doesn't help. Another one might be "Cyber Saber".
Most of the newer one's do involve big numbers, because there is no known way to "hack" them, easily.
ANY encryption system can be broken, given enough time and skilled resources working on it, with modern computers. An acceptable system will just make it impossible for the unskilled/less skilled to figure out, and difficult enough that the value of the message can't be broken out of the cypher text in a reasonable time, for the skilled cyptologist.
The only totally secure possibility that I know of, is a one-time pad system, and that's so awkward to use it's usually not worth it, anyway.
If you don't want to work with BIG BIG BIG numbers, then you don't want to mess further with RSA, because that's the heart of the whole RSA system.
Adak
You CAN do RSA with small numbers, too. What happens is that it's much easier to crack.
In the heart of RSA is modular exponentation. You don't calculate it by first taking the exponent and then calculating its remainder because the numbers grow out of hands. Also you don't want to use floating point numbers because all calculations with them are inexact. Read this: http://en.wikipedia.org/wiki/Modular_exponentiation
RSA with numbers smaller than say 20 digits are essentially useless and for illustration purposes only. The point is to use so many digits the additional polynomial cost outweighs the exponential factoring time. Say 256 bits.
You could try simpler symmetric algorithms, like the Vinegere and Hill ciphers, for arithmetical manipulation practice.
If all you want is the code, go download the SSL lib source or something. Have fun.
Easy, once I figured out decrypt() was overflowing.Code:#include <stdio.h>
int iPower ( int n, int p ) {
int result = 1, i = p;
while ( i-- ) result *= n;
return result;
}
int encrypt ( int ch, int n, int e ) {
return iPower(ch,e) % n;
}
/* Use a = bc mod n = (b mod n).(c mod n) mod n */
int decrypt ( int ch, int n, int d ) {
/* This is hard-coded to assume d == 7 */
/* in reality, use a loop to extract multiple factors */
/* Yes, even with these small numbers, you can easily overflow an int! */
int t1, t2, t3;
t1 = iPower(ch,3) % n;
t2 = iPower(ch,3) % n;
t3 = iPower(ch,1) % n;
return (t1 * t2 * t3) % n;
}
int main ( ) {
char *msg = "hello";
int n = 33, e = 3, d = 7;
int ch;
for ( ch = 0 ; ch < 26 ; ch++ ) {
int enc, dec;
enc = encrypt(ch,n,e);
dec = decrypt(enc,n,d);
printf( "Char %2d -> %2d -> %2d\n", ch, enc, dec );
}
while ( (ch=*msg++) != '\0' ) {
int enc, dec;
enc = encrypt(ch-'a',n,e);
dec = decrypt(enc,n,d)+'a';
printf( "Char %c -> %2d -> %c\n", ch, enc, dec );
}
return 0;
}
@sankarv - next time, try debugging it with a debugger, or printf() or at all.
You have to be prepared to go through each line with a fine comb sometimes just to find the really obvious.
It also helps if your code (like mine) uses the same terminology in the reference material.
Also note that it's a complete waste of time trying to do things with files before you have the basic code sorted out. Simple test data embedded in the program is MUCH easier for others to help with.
Thankx a lot. just want to know one thing.
why u added '-a' in encrypt() function and +'a' in decrypt() function for encrypting strings?
Hi. I done file encryption with the help of the code u gave .
Code:#include <stdio.h>
#include<stdlib.h>
int iPower ( int n, int p ) {
int result = 1, i = p;
while ( i-- ) result *= n;
return result;
}
int encrypt ( int ch, int n, int e ) {
return iPower(ch,e) % n;
}
/* Use a = bc mod n = (b mod n).(c mod n) mod n */
int decrypt ( int ch, int n, int d ) {
/* This is hard-coded to assume d == 7 */
/* in reality, use a loop to extract multiple factors */
/* Yes, even with these small numbers, you can easily overflow an int! */
int t1, t2, t3;
t1 = iPower(ch,3) % n;
t2 = iPower(ch,3) % n;
t3 = iPower(ch,1) % n;
return (t1 * t2 * t3) % n;
}
int main()
{
int n = 33, e = 3, d = 7;
int ch,enc,dec;
FILE *in,*out,*in2; /* In and out FILE Streams to read/write data */
if (( in = fopen("D:/cfiles/cfiles/cfiles/ss.txt", "r")) == NULL) /* Error Check File Streams */
{
printf("Error opening file for reading");
}
if (( out = fopen("D:/cfiles/cfiles/cfiles/ss.enc", "w")) == NULL)
{
printf("Error opening file for writing");;
}
while(( ch = getc(in)) != EOF)
{
enc = encrypt(ch-'a',n,e); /* Decryption from the RSA */
putc(enc, out); /* Write new file */
}
fclose(in);
fclose(out);
printf("Encryption Success:n");
printf("Encrypted and stored data ");
if (( in = fopen("D:/cfiles/cfiles/cfiles/ss.enc", "r")) == NULL) /* Error Check File Streams */
{
printf("Error opening file for reading");
}
if (( out = fopen("D:/cfiles/cfiles/cfiles/ss1.txt", "w")) == NULL) /* Error Check File Streams */
{
printf("Error opening file for writing");
}
while(( ch = getc(in)) != EOF)
{
dec = decrypt(ch,n,e)+'a'; /* Decryption from the RSA */
putc(dec, out); /* Write new file */
}
fclose(in);
fclose(out);
printf("Decryption done");
system("PAUSE");
return 0;
}
Still i face a problem in decryption. This program decrypts strings well but other charadcters are not outputted properly. I could nt figure out what may be the reason?
The file for encyption is
Quote:
hello 101
hi #here am i
123456
The encrypted file is
Quote:
à÷ü÷ë
àþ
à àëë÷ûíôõö
The decrypted file is
Quote:
hellofzvzqhifxherefamfiqqz{hu|
pls help what may be the reason...?
I'm not going to tell you - I'm going to see if you can debug a simple problem yourself.
Once again, you've rushed for the finishing line by trying to encrypt a whole file without bothering to do any more testing!.
Just rip out all that file reading and writing rubbish and study what it going on carefully for a while.
Change the message to "hello world" and go through the code with the debugger ONE line at a time, and make damn sure it does what you want. Then try with a digit character then a symbol like # and a control like '\n'
What I posted was a working hint. Not a complete answer you can just take, wrap a bit of file handling round then claim an easy victory.
sorry. i could nt figure out the poblem. i was debugging for long time and im vexed. i am getting awkward values in debugging. just i want this encryption logic common for strings,numbers and special characters since all occur in a file.
pls help...
What Salem is getting at is make yourself a better code troubleshooter. Don't try and carry the 100Kg backpack all at once. Break it up into smaller and smaller units, THEN work through it line by line, watching your variables as you go.Quote:
Originally Posted by sankarv
Forget about the code that works, and concentrate on what doesn't - in this case, the numbers and other non letter, char's.
Post up what you find that's not right, and what you've tried to do to correct it.
I think numbers and other char's need to be treated a little differently than letters, in this program. What do you think?
Adak
Make it simple - just encrypt and decrypt a letter, say 'a'.
Put a printf on every other line in the code, and watch what happens.
Then do the same for a space ' ', and try to understand the differences.
Which compiler / debugger do you have?
> i am getting awkward values in debugging
So you found a bug perhaps?
So how about documenting your confusion in a post rather than just moaning that it doesn't work, and hoping someone will post a complete answer.
Personally, I think it's only a few days since you did the "hello world" program and now you're in way over your head.
Day 1 - build paper aeroplane
Day 2 - try and attach jet engines
Day 3 - post on www.paperplanes.com/forums that plane doesn't work.
CAN YOU HELP ME FOR WRITING A PROGRAM IN C ?
Encrypt the message “I LOVE DISCRETE” by translating the letters into numbers, applying the encryption function given below, and then translating the numbers back into letters. Additionally; find the corresponding decryption function, and apply it to the ciphertext to revert back to the original message
You didn't post all the "see below" information.
You didn't read the forum rules, particularly the one about digging up 4 year old threads.
Nor the rule about homework.
Closed - now try again with more effort in a NEW post.