Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <winsock.h>
- int repeatsquare( int x, int e, int n);
- bool RelativelyPrime (int a, int b);
- int main(){
- unsigned char chararray[80];
- unsigned char encrypted[80];
- unsigned char normalarray[80];
- char temp[80];
- int length;
- printf("Please Enter A Word : ");
- //scanf("%s",chararray);
- fgets(temp,80,stdin);
- // strcpy(chararray, (unsigned char) temp);
- //strcpy(chararray, temp);
- for(int i=0; i<strlen(temp); i++){
- chararray[i] = (unsigned char)temp[i];
- printf("-> %c", chararray[i]);
- }
- length = sizeof(chararray);
- int primenumbers[15] = {11,13,17,19,23,29,31,37,41,43};
- int q,p,n,z,e,d;
- while(true){
- srand(time(NULL));
- q = primenumbers[(rand()%15)];
- p = primenumbers[(rand()%15)];
- z = (p-1)*(q-1);
- n = q*p;
- e = n-1;
- if(n == 0){
- continue;
- }
- //printf("p=%d,q=%d n=%d\n",p,q,n);
- if((n)>255){
- continue;
- }
- while(!RelativelyPrime(e,z)){
- e = rand()%n;
- if(n==(e-1)){
- continue;
- }
- }
- if ((RelativelyPrime(e,z)) && (n <= 255)){
- printf("break\n");
- break;
- }
- }
- int i = 0;
- while(true){
- if((((e*i)-1)% z) == 0){
- d = i;
- break;
- }else{
- i++;
- }
- }
- printf("p=%d q=%d n=%d z=%d, e=%d d=%d\n",p,q,n,z,e,d);
- for(i=0;i<length;i++){
- encrypted[i]=repeatsquare(chararray[i],e,n);
- //printf("%d\n",encrypted[i]);
- }
- printf("TITLE : %s\n",encrypted);
- for(i=0;i<length;i++){
- normalarray[i]=repeatsquare(encrypted[i],d,n);
- //printf("%d\n",normalarray[i]);
- }
- printf("TITLE 2 : %s\n",normalarray);
- }
- int repeatsquare( int x, int e, int n) {
- int y=1;//initialize y to 1, very important
- while (e > 0) {
- if (( e % 2 ) == 0) {
- x = (x*x) % n;
- e = e/2;
- }
- else {
- y = (x*y) % n;
- e = e-1;
- }
- }
- return y; //the result is stored in y
- }
- bool RelativelyPrime (int a, int b) { // Assumes a, b > 0
- for ( ; ; ) {
- if (!(a %= b)) return b == 1 ;
- if (!(b %= a)) return a == 1 ;
- }
- }
Add Comment
Please, Sign In to add comment