int eval_d(int e,int Q_n)
{
	for(int i=1;i<Q_n;i++)
		if(fmea(i*e,1,Q_n)==1)
			return i;
	return 0;
}
int CRT(int x,int y, int M)
{
	int i,j,sum=0,*m=(int *)malloc(sizeof(int));
	for(i=0;i<factorsOf(M,m);i++)
		sum += (M/m[i] * (inverseOf(M/m[i],m[i]))) * fmea(x,y,m[i]);
	return sum % M;
}
int inverseOf(int M,int m)
{
	for(int i=1;i<m;i++)
		if(fmea(i*M,1,m)==1)
			return i;
	return 0;
}
int rsa_DEC(int p,int q,int C)
{
	int n = p * q,Q_n = (p-1)*(q-1),e = generate_e(Q_n),d = eval_d(e,Q_n);
	return fmea(C,d,n);
}
int rsa_ENC(int p,int q,int M)
{
	int n = p * q,Q_n = (p-1)*(q-1),e = generate_e(Q_n);
	return fmea(M,e,n);
}
Example #6
0
File: rsa.c Project: jasongi/rsa
/*  this program does three different things depending on the
    flags given. -g will generate a public key e, private key d
    and a modulo n. -e will take in/out filenames, a key and n
    and it will encrypt the in file and save it to the out file.
    -d will take an in/out filename, a key and a n and it will
    decrypt the in file and save it to the out file. The reason
    I didn't have key generation and encryption happen at the same
    time is because with RSA, the person you're encrypting the message
    for is the person who generates the key to encrypt it with*/
int main(int argc, char** argv)
{
    /*  the usage will print if the arguments are wrong*/
    char usage[] = "rsa by Jason Giancono\nUSAGE: rsa <FLAG>\nFLAGS:\t\t-g: generate keys\n\t\t-d <input filename> <output filename> <key> <n modulo>: decrypt file using key and modulo and output to <output filename>\n\t\t-e <input filename> <output filename> <key> <n modulo>: encrypt file using key and modulo and output to <output filename>\n";
    /*  seed the PRNG when the program starts*/
    srand(time(NULL));
    /*  variable for i/o errors*/
    int error = 0;
    /*  test to see if the -g flag and make sure there is any arguments at all
        (it will segfault otherwise*/
    if ( (argc != 1) && !strcmp(argv[1],"-g") )
    {
        uint64_t pub, priv,n;
        /*generates the keys*/
        generateKeys(&pub, &priv, &n);
        /*print the keys to the terminal*/
        printf("\nKeys:\t\t\tPublic: \t%llu\n\t\t\tPrivate: \t%llu\n\t\t\tN: \t\t%llu\n", pub, priv, n);
    }
    else if (argc ==6)
    {
        uint64_t key,n;
        /*open files for reading/writing*/
        FILE *input,*output;
        input = fopen(argv[2], "r");
        output = fopen(argv[3], "w");
        /*convert arguments to integers*/
        key = strtoll(argv[4], NULL,10);
        n = strtoll(argv[5], NULL,10);
        /*calculate the amount of chars I can fit in one encryption block*/
        int arraySize = (msb(n)-1)/8;
        /*make sure the files opened OK*/
        if(output != NULL)
        {
            if(input !=NULL)
            {
                /*check the flag for encrypt*/
                if(!strcmp(argv[1], "-e"))
                {
                    /*continue reading and encrypting until an error or end of file*/
                    while((!feof(input)) && !ferror(input) && !ferror(output) )
                    {
                        /*make the array size 8 because then it is sizeof(uint64_t)*/
                        char block[8];
                        memset(block,0,8);
                        int ii;
                        /*read characters into the array*/
                        for(ii=0; ii<arraySize; ii++)
                        {
                            block[ii] = getc(input);
                        }
                        /*dirty pointer manipulation so you can do FMEA on the array*/
                        uint64_t *m = (uint64_t*)block;
                        /*do the RSA encryption*/
                        *m = fmea(*m,key,n);
                        /*  write characters into encrypted file, one extra character is copied
                            copies the arraySize+1 because the FMEA may have made the block
                            larger than it's original size*/
                        for(ii=0; ii<arraySize+1; ii++)
                        {
                            putc(block[ii],output);
                        }
                    }
                }
                /*check the flag for decrypt*/
                else if(!strcmp(argv[1], "-d"))
                {
                    /*continue reading and encrypting until an error or end of file*/
                    while((!feof(input)) && !ferror(input) && !ferror(output) )
                    {
                        /*make the array size 8 because then it is sizeof(uint64_t)*/
                        char block[8];
                        memset(block,0,8);
                        int ii;
                        /*read characters into the array*/
                        for(ii=0; ii<arraySize+1; ii++)
                        {
                            block[ii] = getc(input);
                        }
                        /*dirty pointer manipulation so you can do FMEA on the array*/
                        uint64_t *m = (uint64_t*)block;
                        /*do the RSA decryption*/
                        *m = fmea(*m,key,n);
                        /*  write characters into decrypted file, check for EOF so that
                        you don't copy in extra junk.*/
                        for(ii=0; ii<(arraySize); ii++)
                        {
                            if(!feof(input) && (block[ii] != EOF))
                                putc(block[ii],output);
                        }
                    }
                }
                else error = 1;
            }
            else error = 1;
        }
        else error = 1;
        /*error handling*/
        if (error || ferror(output) || ferror(input))
        {
            error = 1;
            perror("An Error Occurred");
            if (output !=NULL)
                fclose(output);
            if (input != NULL)
                fclose(input);
        }
        else
        {
            /*close the files*/
            fclose(output);
            fclose(input);
        }
    }
    /*print the usage because the muffin obviously doesn't know the arguments*/
    else printf("Wrong number of args \n\n%s", usage);
    return error;
}