Guest User

Untitled

a guest
Dec 13th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <windows.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <winsock.h>
  11.  
  12.  
  13. int repeatsquare( int x, int e, int n);
  14. bool RelativelyPrime (int a, int b);
  15.  
  16.  
  17. int main(){
  18. unsigned char chararray[80];
  19. unsigned char encrypted[80];
  20. unsigned char normalarray[80];
  21. char temp[80];
  22. int length;
  23. printf("Please Enter A Word : ");
  24. //scanf("%s",chararray);
  25. fgets(temp,80,stdin);
  26. // strcpy(chararray, (unsigned char) temp);
  27. //strcpy(chararray, temp);
  28. for(int i=0; i<strlen(temp); i++){
  29. chararray[i] = (unsigned char)temp[i];
  30. printf("-> %c", chararray[i]);
  31. }
  32.  
  33. length = sizeof(chararray);
  34. int primenumbers[15] = {11,13,17,19,23,29,31,37,41,43};
  35. int q,p,n,z,e,d;
  36. while(true){
  37. srand(time(NULL));
  38. q = primenumbers[(rand()%15)];
  39. p = primenumbers[(rand()%15)];
  40. z = (p-1)*(q-1);
  41. n = q*p;
  42. e = n-1;
  43. if(n == 0){
  44. continue;
  45. }
  46. //printf("p=%d,q=%d n=%d\n",p,q,n);
  47. if((n)>255){
  48. continue;
  49. }
  50. while(!RelativelyPrime(e,z)){
  51.  
  52. e = rand()%n;
  53. if(n==(e-1)){
  54. continue;
  55. }
  56. }
  57.  
  58. if ((RelativelyPrime(e,z)) && (n <= 255)){
  59. printf("break\n");
  60. break;
  61. }
  62. }
  63.  
  64. int i = 0;
  65. while(true){
  66. if((((e*i)-1)% z) == 0){
  67. d = i;
  68. break;
  69. }else{
  70. i++;
  71. }
  72. }
  73.  
  74. printf("p=%d q=%d n=%d z=%d, e=%d d=%d\n",p,q,n,z,e,d);
  75.  
  76. for(i=0;i<length;i++){
  77. encrypted[i]=repeatsquare(chararray[i],e,n);
  78. //printf("%d\n",encrypted[i]);
  79. }
  80. printf("TITLE : %s\n",encrypted);
  81. for(i=0;i<length;i++){
  82. normalarray[i]=repeatsquare(encrypted[i],d,n);
  83. //printf("%d\n",normalarray[i]);
  84. }
  85. printf("TITLE 2 : %s\n",normalarray);
  86.  
  87. }
  88.  
  89. int repeatsquare( int x, int e, int n) {
  90. int y=1;//initialize y to 1, very important
  91. while (e > 0) {
  92. if (( e % 2 ) == 0) {
  93. x = (x*x) % n;
  94. e = e/2;
  95. }
  96. else {
  97. y = (x*y) % n;
  98. e = e-1;
  99. }
  100. }
  101. return y; //the result is stored in y
  102. }
  103.  
  104. bool RelativelyPrime (int a, int b) { // Assumes a, b > 0
  105. for ( ; ; ) {
  106. if (!(a %= b)) return b == 1 ;
  107. if (!(b %= a)) return a == 1 ;
  108. }
  109. }
Add Comment
Please, Sign In to add comment