├── RSA 3.pdf ├── RSA Image.pdf ├── Python ├── RSA.jpg └── RSA.py ├── RSA Image2.pdf ├── C++ Algorithms ├── a.out ├── ExtendedEuclidean.cc └── MillerRabin.cc ├── Matlab ├── programmer.jpg └── RSA.m ├── Generate Random Prime_RSA.pdf ├── README.md └── Store_Big_Numbers.txt /RSA 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhruvie/RSA/HEAD/RSA 3.pdf -------------------------------------------------------------------------------- /RSA Image.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhruvie/RSA/HEAD/RSA Image.pdf -------------------------------------------------------------------------------- /Python/RSA.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhruvie/RSA/HEAD/Python/RSA.jpg -------------------------------------------------------------------------------- /RSA Image2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhruvie/RSA/HEAD/RSA Image2.pdf -------------------------------------------------------------------------------- /C++ Algorithms/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhruvie/RSA/HEAD/C++ Algorithms/a.out -------------------------------------------------------------------------------- /Matlab/programmer.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhruvie/RSA/HEAD/Matlab/programmer.jpg -------------------------------------------------------------------------------- /Generate Random Prime_RSA.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhruvie/RSA/HEAD/Generate Random Prime_RSA.pdf -------------------------------------------------------------------------------- /C++ Algorithms/ExtendedEuclidean.cc: -------------------------------------------------------------------------------- 1 | // C++ program to demonstrate working of 2 | // extended Euclidean Algorithm 3 | 4 | // This Algorithm is only meant to calculate private key (d), therefore if gcd(eulerTotient,E)!=1. 5 | // The program will not work. 6 | 7 | 8 | #include 9 | using namespace std; 10 | #define int long long 11 | // Function for extended Euclidean Algorithm 12 | int gcdExtended(int E, int eulerTotient) 13 | { 14 | int a1=1,a2=0,b1=0,b2=1,d1=eulerTotient,d2=E,temp; 15 | 16 | while(d2!=1) 17 | { 18 | //k 19 | int k=(d1/d2); 20 | 21 | //a 22 | temp=a2; 23 | a2=a1-(a2*k); 24 | a1=temp; 25 | 26 | //b 27 | temp=b2; 28 | b2=b1-(b2*k); 29 | b1=temp; 30 | 31 | //c 32 | temp=d2; 33 | d2=d1-(d2*k); 34 | d1=temp; 35 | 36 | } 37 | cout<<"X:"<eulerTotient d=dMod(eulerTotient) 43 | // 2. if d<0 d=d+eulerTotient 44 | 45 | if(d>eulerTotient) 46 | d=d%eulerTotient; 47 | else if(d<0) 48 | d=d+eulerTotient; 49 | 50 | cout<<"Private Key is:"<>eulerTotient; 59 | cout<<"Enter E:\n"; 60 | cin>>E; 61 | gcdExtended(E,eulerTotient); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /C++ Algorithms/MillerRabin.cc: -------------------------------------------------------------------------------- 1 | // To Check If a Given Number is Prime or Not By Miller Rabin Primality Test 2 | #include 3 | using namespace std; 4 | #define int long long 5 | int power(int a, int d, int Mod) 6 | { 7 | int result=1; 8 | a=a%Mod; 9 | while(d!=0) 10 | { 11 | if(d&1) 12 | result=((result%Mod)*(a%Mod))%Mod; 13 | a=((a%Mod)*(a%Mod))%Mod; 14 | d>>=1; 15 | } 16 | return result; 17 | } 18 | bool MillerRabin(int n, int d) 19 | { 20 | int a; // which is choosen randomly between 2 to n-2 21 | // for learning purpose we are choosing a from 1 to n-2; 22 | for(a=2; a>=1; 54 | cout<<"d:"<>n; 67 | if(isPrime(n,k)) 68 | cout<<"Prime\n"; 69 | else 70 | cout<<"Composite\n"; 71 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RSA 2 | Image Encryption using RSA Algorithm 3 | 4 | # PROJECT TITLE 5 | 6 | Image Encryption using RSA Algorithm :- The RSA is an cryptographic algorithm which is use to encrypt and decrypt the data. Here we are Implementing RSA(Asymmetric key Cryptography) Algorithm on an IMAGE to encrypt and decrypt using two keys, Private key and Public Key. 7 | To know more about RSA Encryption on an Image read the pdf(RSA Image) that I have uploaded in my Repositiories. 8 | 9 | # GETTING STARTED 10 | 11 | These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. 12 | 13 | # Prerequisites 14 | 15 | You just need Python 3.0+ or Jupyter Notebook installed in your local machine or you can open the project in Google Colab. 16 | 17 | Install Python: https://www.python.org/downloads/ 18 | 19 | Install Jupyter Notebook:- https://jupyter.org/install.html 20 | 21 | For Google Colab:- Just type Google Colab in any Search Engine and click on the Google Colab link(Upload Image and Code File in Your Google Drive Account and make sure the Path is correct according to your account where you have Uploaded) 22 | 23 | 24 | # Deployment 25 | 26 | When you installed the Python in your local machine, then open Terminal and type 'python3' to see if its correctly insatlled or not. 27 | Now, fork my project on your github account and Clone/Download it on your machine. 28 | Then, open your Jupyter Notebook or any IDE you have open it. (Remember RSA.ipynb file only work in Jupyter Notebook/Google Colab, so for any other IDE open RSA.py file) 29 | And , RUN IT... 30 | 31 | # About The Project 32 | 33 | # Additional Information 34 | I've Uploaded some C++ Algorithms that are used by me in this code. As everyone is familiar with C++ language so it will be easy for understand. 35 | Now, in RSA we deal with really big numbers. And every language as a limitation upto how large Numbers can be stored in any Datatype. Therefore, we need a Data Structure to store that Big Numbers.But,in Python we can store any Big Number easily so here it is not a problem, but those who want to do this algorithm in C++, either they can store their number in array and do all calculations in array itself. Yes, this is bit hard but you can use C++ boost library in which you can store large Numbers and it will be easy for you to do code of this Algorithm. And everything will be same. 36 | 37 | # Where This Project Can be Improved 38 | If you have RUN the Python file for larger bits, then you know it's taking a longer time to do encrytpion and decryption. And we know in this world time is more Important, so If anyone who wants to do improvement in the speed of RSA Image Encryption and Decryption will be Valuable for me and ofcourse for you also. Thank you for your Time. 39 | 40 | # CONTIBUTORS 41 | These are the names of the contributors who contribute to my project. 42 | https://github.com/dhruvie/RSA/graphs/contributors 43 | 44 | -------------------------------------------------------------------------------- /Python/RSA.py: -------------------------------------------------------------------------------- 1 | from google.colab import drive 2 | drive.mount('/content/drive') 3 | 4 | 5 | import cv2 6 | import numpy as np 7 | from google.colab.patches import cv2_imshow 8 | import matplotlib.pyplot as plt 9 | %matplotlib inline 10 | 11 | my_img = cv2.imread('drive/My Drive/RSA.jpg') 12 | # cv2_imshow(my_img) 13 | plt.imshow(my_img, cmap="gray") 14 | 15 | 16 | 17 | #RSA 18 | 19 | # STEP 1: Generate Two Large Prime Numbers (p,q) randomly 20 | from random import randrange, getrandbits 21 | 22 | 23 | def power(a,d,n): 24 | ans=1; 25 | while d!=0: 26 | if d%2==1: 27 | ans=((ans%n)*(a%n))%n 28 | a=((a%n)*(a%n))%n 29 | d>>=1 30 | return ans; 31 | 32 | 33 | def MillerRabin(N,d): 34 | a = randrange(2, N - 1) 35 | x=power(a,d,N); 36 | if x==1 or x==N-1: 37 | return True; 38 | else: 39 | while(d!=N-1): 40 | x=((x%N)*(x%N))%N; 41 | if x==1: 42 | return False; 43 | if x==N-1: 44 | return True; 45 | d<<=1; 46 | return False; 47 | 48 | 49 | def is_prime(N,K): 50 | if N==3 or N==2: 51 | return True; 52 | if N<=1 or N%2==0: 53 | return False; 54 | 55 | #Find d such that d*(2^r)=X-1 56 | d=N-1 57 | while d%2!=0: 58 | d/=2; 59 | 60 | for _ in range(K): 61 | if not MillerRabin(N,d): 62 | return False; 63 | return True; 64 | 65 | 66 | 67 | 68 | def generate_prime_candidate(length): 69 | # generate random bits 70 | p = getrandbits(length) 71 | # apply a mask to set MSB and LSB to 1 72 | # Set MSB to 1 to make sure we have a Number of 1024 bits. 73 | # Set LSB to 1 to make sure we get a Odd Number. 74 | p |= (1 << length - 1) | 1 75 | return p 76 | 77 | 78 | 79 | def generatePrimeNumber(length): 80 | A=4 81 | while not is_prime(A, 128): 82 | A = generate_prime_candidate(length) 83 | return A 84 | 85 | 86 | 87 | length=5 88 | P=generatePrimeNumber(length) 89 | Q=generatePrimeNumber(length) 90 | 91 | print(P) 92 | print(Q) 93 | 94 | 95 | 96 | #Step 2: Calculate N=P*Q and Euler Totient Function = (P-1)*(Q-1) 97 | N=P*Q 98 | eulerTotient=(P-1)*(Q-1) 99 | print(N) 100 | print(eulerTotient) 101 | 102 | 103 | 104 | #Step 3: Find E such that GCD(E,eulerTotient)=1(i.e., e should be co-prime) such that it satisfies this condition:- 1eulerTotient: 151 | D=D%eulerTotient 152 | elif D<0: 153 | D=D+eulerTotient 154 | 155 | return D 156 | 157 | 158 | D=gcdExtended(E,eulerTotient) 159 | print(D) 160 | 161 | 162 | row,col=my_img.shape[0],my_img.shape[1] 163 | enc = [[0 for x in range(3000)] for y in range(3000)] 164 | 165 | 166 | 167 | #Step 5: Encryption 168 | 169 | for i in range(100,700): 170 | for j in range(100,1000): 171 | r,g,b=my_img[i,j] 172 | C1=power(r,E,N) 173 | C2=power(g,E,N) 174 | C3=power(b,E,N) 175 | enc[i][j]=[C1,C2,C3] 176 | C1=C1%256 177 | C2=C2%256 178 | C3=C3%256 179 | my_img[i,j]=[C1,C2,C3] 180 | 181 | 182 | # plt.imshow(my_img, cmap="gray") 183 | cv2_imshow(my_img) 184 | 185 | 186 | #Step 6: Decryption 187 | for i in range(100,700): 188 | for j in range(100,1000): 189 | r,g,b=enc[i][j] 190 | M1=power(r,D,N) 191 | M2=power(g,D,N) 192 | M3=power(b,D,N) 193 | my_img[i,j]=[M1,M2,M3] 194 | 195 | cv2_imshow(my_img) 196 | # plt.imshow(my_img, cmap="gray") -------------------------------------------------------------------------------- /Store_Big_Numbers.txt: -------------------------------------------------------------------------------- 1 | Hello all ! 2 | The problem that we will be taking up is http://www.codechef.com/problems/FCTRL2/ 3 | This problem basically asks you to calculate the factorial of a number up to 100. Now, I guess most of you know what a “factorial” is. For those who don’t, the factorial of a number N is 1*2*…*N. This problem would be very simple, had it not been for the maximum value of N. The structure of the problem is such that it asks the user to take the number of test cases as the first input. Then ‘t’ integers follow where ‘t’ is the number of test cases which was given as input previously. 4 | For every integer here, we have to calculate the factorial. This is very simple in languages like python or java which have built-in support for big integer types. It proves to be a hassle for people using C / C++ or languages that do not have a built-in biginteger type. Let’s think about how we can store the the result. 5 | Now, the maximum number that we can store in an unsigned 32 bit integer is 2 ^ 32 – 1 and in an unsigned 64 bit integer is 2 ^ 64 – 1. Something like 100!(‘!’ is the notation 6 | for factorial) has over 150 decimal digits. The data types mentioned earlier can store numbers having at most 9 and 19 decimal digits respectively. So, we need to find a way to store the 150+ digits that we will get as the answer. The simplest data structure that we can use is an integer array of size of about 200 to be on the safe side. 7 | In the simplest form, let us store one decimal digit per array index. So, if the number is say 120, then the array will have the numbers as follows: 8 | Say a[200] is how we declare the array, then 9 | a[0] = 0 10 | a[1] = 2 11 | a[2] = 1 12 | The least significant digit is stored in the lowest index 0. The next one in index 1 and so on. Along with the array, we need an integer specifying the total number of digits in the array at the given moment. Let this number be ‘m‘. Initially, a[0] will be 1 and the value of ‘m‘ will be 1 specifying that we have just one digit in the array. 13 | Let’s take a simple example first. Consider that the array has some value like 45 and we need to multiply it with a value 37. This can be done in the following way. 14 | The array will be: 15 | a[0] = 5 16 | a[1] = 4 17 | and the value of m will be 2 specifying that there are 2 digits in the array currently. 18 | Now, to multiply this array with the value 37. We start off from the index 0 of the array to index 1. At every iteration, we calculate 37 * a[index]. We also maintain a temporary variable called temp which is initialized to 0. Now, at every step, we calculate x = a[index] * 37 + temp. The new value of a[index] will be x % 10 and the new value of temp will be temp / 10. We are simply carrying out multiplication the way it is carried out usually. So, for the current situation, the iterations will be something like this. 19 | Initialize temp = 0 20 | Iteration 1 : 21 | array = (5, 4) 22 | temp = 0 23 | index = 0, a[index] = 5 24 | x = a[index] * 37 + temp = 5 * 37 + 0 = 185. 25 | the new value of a[index] = 185 % 10 which is 5 and the new value of temp is 185 / 10 which is 18 26 | Iteration 2 : 27 | array : (5, 4) 28 | temp = 18 29 | index = 1, a[index] = 4 30 | x = a[index] * 37 + temp = 4 * 37 + 18 = 166. 31 | the new value of a[index] = 166 % 10 which is 6 and the new value of temp is 166 / 10 which is 16 32 | We have finished 2 iterations and this is the value of ‘m‘, the array size at the moment. The required number of iterations is now over, but the value of temp is still greater than 0. This means that the current value of temp is to be incorporated into the array. For that, we keep appending the last digit of temp to the array and divide temp by 10 till temp becomes 0. So, we will get something like 33 | Iteration 1 : 34 | temp = 16 , array = (5, 6) 35 | So, we add 16 % 10 to the array so that the array becomes (5, 6, 6) and we divide temp by 10 so that temp becomes 1. We update the value of ‘m’ to m + 1 that is m = 3 36 | Iteration 2 : 37 | temp = 1, array = (5, 6, 6) 38 | Now, we add 1 % 10 to the array so the array becomes (5, 6, 6, 1) and we divide temp by 10 so that temp becomes 0. We update the value of ‘m’ to m + 1 that is m = 4 39 | The value of temp is now 0 and our multiplication is now over. The final array we get is (5, 6, 6, 1) 40 | Voila, we have the answer to 45 * 37 in our array with the Least significant digit in the 0th position. 41 | For finding the factorial, we need to carry out this exact multiplication operation at every step as we loop from 1 to N. At the end of the Nth iteration, our array will contain 42 | the answer and the value of m will be the number of digits in the answer. We can then just print the array from the Most significant digit to the least for the answer. 43 | The basic flow of the program will be as below : 44 | Start 45 | Take in the number of test cases 46 | While there is a test case remaining to be handled 47 |     Take in the number whose factorial is to be found, let it be N 48 |     Initialize the array's 0th index to 1 and m to 1 49 |     Initialize i to 1 50 |     While i is less than or equal to N 51 |         Carry out multiplication of the array with 'i' as shown above. 52 |     Print the contents of the array starting from the most significant digit and ending with the least significant digit. 53 | Stop 54 | Certain improvements can be made to the above mentioned method. We are storing only one digit per array index, We can store more than 1 digit per index so that the number of computations are reduced. The method to do that is the same as above. We leave it to the reader as an exercise 55 | 56 | -------------------------------------------------------------------------------- /Matlab/RSA.m: -------------------------------------------------------------------------------- 1 | 2 | Img=(imread("programmer.jpg")); 3 | #figure, imshow(Img) 4 | 5 | #Step 1: Find p and q, such that they are large prime numbers 6 | p=floor(10000+(1000000-10000)*rand()); 7 | p; 8 | q=floor(10000+(1000000-10000)*rand()); 9 | q; 10 | 11 | #if p is even, increase by 1 and if q is even increase by 1 12 | if(mod(p,2)==0) 13 | p=p+1; 14 | endif 15 | if(mod(q,2)==0) 16 | q=q+1; 17 | endif 18 | 19 | #check if p is prime or not.. find prime no! 20 | while(1) 21 | sqr=floor(sqrt(p)); 22 | flag=0; 23 | for i = 2:sqr 24 | if(mod(p,i)==0) 25 | flag=1; 26 | break 27 | endif 28 | endfor 29 | if(flag==0) 30 | break 31 | endif 32 | p=p+2; 33 | endwhile 34 | p 35 | 36 | #check if q is prime or not.. find prime no! 37 | while(1) 38 | sqr=floor(sqrt(q)); 39 | flag=0; 40 | for i = 2:sqr 41 | if(mod(q,i)==0) 42 | flag=1; 43 | break 44 | endif 45 | endfor 46 | if(flag==0) 47 | break 48 | endif 49 | q=q+2; 50 | endwhile 51 | q 52 | 53 | lp=1; 54 | P=[]; 55 | while(p!=0) 56 | P(lp)=mod(p,10); 57 | lp++; 58 | p=floor(p/10); 59 | endwhile 60 | 61 | Q=[]; 62 | lq=1; 63 | while(q!=0) 64 | Q(lq)=mod(q,10); 65 | lq++; 66 | q=floor(q/10); 67 | endwhile 68 | P 69 | Q 70 | 71 | P1=P; 72 | P1(1)=P1(1)-1; 73 | Q1=Q; 74 | Q1(1)=Q1(1)-1; 75 | 76 | #Step 2:Calculate The Value of n and Euler Totient Function 77 | 78 | N=zeros(1,200); 79 | Totientfunc= zeros(1,200); 80 | function [Result,length]= Multiplication(A,B,C,lp,lq) 81 | C=zeros(1,200);k=1;temp=0; 82 | for i=1:(lp-1) 83 | k=i; 84 | for j=1:(lq-1) 85 | x=A(i)*B(j)+temp+C(k); 86 | C(k)=mod(x,10); 87 | k++; 88 | temp=floor(x/10); 89 | endfor 90 | while(temp!=0) 91 | C(k)=mod(temp,10); 92 | k++; 93 | temp=floor(temp/10); 94 | endwhile 95 | endfor 96 | Result=C; 97 | length=k; 98 | i=k-1; 99 | while(i>=1) 100 | C(i) 101 | i=i-1; 102 | endwhile 103 | endfunction 104 | 105 | disp("N:") 106 | [N,ln]=Multiplication(P,Q,lp,lq); 107 | disp("Totientfunc:") 108 | [Totientfunc,ltf]=Multiplication(P1,Q1,Totientfunc,lp,lq,0,0); 109 | 110 | #Totientfunc=[6,9,0,4,0,2,3,4,3,5,4,6] 111 | #ltf=13; 112 | #Step 3: Set e such that 1 < e < Totientfunc and gcd(e,Totientfunc)=1 113 | # i.e., e should be co-prime. 114 | 115 | e=floor(1000+(100000-1000)*rand()); 116 | E=[]; 117 | le=1; 118 | while(e!=0) 119 | E(le)=mod(e,10); 120 | le++; 121 | e=floor(e/10); 122 | endwhile 123 | E 124 | le 125 | 126 | function [X,i]= Mult(b,y,z,j,n,m) 127 | temp=0;x=0; 128 | for k=1:m-1 129 | x=b(j)*y(k)+temp; 130 | z(n++)=mod(x,10); 131 | temp=floor(x/10); 132 | endfor 133 | while(temp!=0) 134 | z(n++)=mod(temp,10); 135 | temp=floor(temp/10); 136 | endwhile 137 | i=n; 138 | X=z; 139 | endfunction 140 | 141 | function [Y,i] = add(x,value,n) 142 | k=1; 143 | while(value!=0) 144 | value=(x(k)+value); 145 | x(k++)=mod(value,10); 146 | value=floor(value/10); 147 | endwhile 148 | if(k>n) 149 | n=k; 150 | endif 151 | i=n; 152 | Y=x; 153 | endfunction 154 | 155 | function Z= Copy(X,Y,n) 156 | for k=1:n-1 157 | X(k)=Y(k); 158 | endfor 159 | Z=X; 160 | endfunction 161 | 162 | function [Z,i]= EliminateZeros(X,n) 163 | k=0;i=n-1; 164 | while(i>=1) 165 | if(X(i)==0) 166 | k++; 167 | else 168 | break; 169 | endif 170 | i--; 171 | endwhile 172 | Z=X; 173 | i=n-k; 174 | endfunction 175 | 176 | 177 | function GCD= gcd(A,B,le,ltf) 178 | while(1) 179 | b=[0,1];r=zeros(200,1);lr=1;lq=1;a=1;result=zeros(200,1);q=zeros(100,1);q2=zeros(100,1);flag=1;temp=0;value=0; 180 | i=0; 181 | while(ild) 213 | flag=0; 214 | break; 215 | endif 216 | x=d2(k)-result(k); 217 | if(x<0) 218 | if(k==(ld-1)) 219 | flag=0; 220 | break; 221 | endif 222 | x=x+10; 223 | d2(k+1)=d2(k+1)-1; 224 | endif 225 | r(k++)=x; 226 | endwhile 227 | if(flag) 228 | Beta=9-j; 229 | lr=k; 230 | [r,lr]=EliminateZeros(r,k); 231 | break; 232 | endif 233 | endfor 234 | q2=Copy(q2,q,lq); 235 | temp=0; 236 | for j=1:2 237 | lq2=j; 238 | [q2,lq2]=Mult(b,q,q2,j,lq2,lq); 239 | lq=lq2; 240 | endfor 241 | q=Copy(q,q2,lq); 242 | [q,lq]=add(q,Beta,lq); 243 | [q,lq]=EliminateZeros(q,lq); 244 | endfor 245 | #Break Condition 246 | rem=0; 247 | i=lr; 248 | while(i>=1) 249 | if(r(i)==0) 250 | rem++; 251 | endif 252 | i--; 253 | endwhile 254 | if(rem==lr) 255 | i=le-1; 256 | value=0; 257 | while(i>=1) 258 | if(i!=1) 259 | value=A(i)*10+value; 260 | else 261 | value=A(i)+value; 262 | endif 263 | i--; 264 | endwhile 265 | GCD=value; 266 | GCD 267 | break; 268 | endif 269 | B=Copy(B,A,le); 270 | ltf=le; 271 | A=Copy(A,r,lr); 272 | le=lr; 273 | endwhile 274 | endfunction 275 | 276 | while(1) 277 | if(gcd(E,Totientfunc,le,ltf)==1) 278 | break 279 | endif 280 | [E,le]=add(E,1,le); 281 | E 282 | endwhile 283 | 284 | function [Qoutient,lk]= Division(D,Dl1,Dl2) 285 | b=[0,1];r=zeros(200,1);lr=1;lq=1;a=1;result=zeros(200,1);q=zeros(100,1);q2=zeros(100,1);flag=1;temp=0;value=0; 286 | i=0; 287 | while(ild) 320 | flag=0; 321 | break; 322 | endif 323 | x=d2(k)-result(k); 324 | if(x<0) 325 | if(k==(ld-1)) 326 | flag=0; 327 | break; 328 | endif 329 | x=x+10; 330 | d2(k+1)=d2(k+1)-1; 331 | endif 332 | r(k++)=x; 333 | endwhile 334 | if(flag) 335 | Beta=9-j; 336 | lr=k; 337 | [r,lr]=EliminateZeros(r,k); 338 | break; 339 | endif 340 | endfor 341 | q2=Copy(q2,q,lq); 342 | temp=0; 343 | for j=1:2 344 | lq2=j; 345 | [q2,lq2]=Mult(b,q,q2,j,lq2,lq); 346 | lq=lq2; 347 | endfor 348 | q=Copy(q,q2,lq); 349 | [q,lq]=add(q,Beta,lq); 350 | [q,lq]=EliminateZeros(q,lq); 351 | endfor 352 | i=lq-1; 353 | disp("Q") 354 | while(i>=1) 355 | q(i--) 356 | endwhile 357 | Qoutient=q; 358 | lk=lq; 359 | endfunction 360 | 361 | function [Result,f]= Subtract(A,B,Z,n,flag) 362 | for i=1:n-1 363 | x=B(1,i)-A(1,i); 364 | if(x<0) 365 | if(flag==0 && i==(n-1)) 366 | flag=1; 367 | break; 368 | endif 369 | x=x+10; 370 | B(1,i+1)=B(1,i+1)-1; 371 | endif 372 | Z(i)=x; 373 | endfor 374 | Result=Z; 375 | # disp("ZZZ") 376 | #Z 377 | f=flag; 378 | endfunction 379 | 380 | function [Result,l]=addArray(X,Y,n,Z) 381 | i=1;temp=0;x=0; 382 | while(i!=n) 383 | x=X(1,i)+Y(i)+temp; 384 | Z(i++)=mod(x,10); 385 | temp=floor(x/10); 386 | endwhile 387 | while(temp!=0) 388 | Z(i++)=mod(temp,10); 389 | temp=floor(temp/10); 390 | endwhile 391 | Result=Z; 392 | l=i; 393 | endfunction 394 | 395 | function [Result,l,Sign]=Sub(A,B,al,bl,Aneg,Bneg) 396 | A2=zeros(2,100);C=zeros(1,200);Cneg=0; 397 | [B,bl]=EliminateZeros(B,bl); 398 | if((Aneg(1)==1 && Bneg==0) || (Aneg(1)==0 && Bneg==1)) 399 | if(Aneg(1)==1) 400 | if(al>=bl) 401 | [C,cl]=addArray(A,B,al,C); 402 | else 403 | [C,cl]=addArray(A,B,bl,C); 404 | endif 405 | Cneg=1; 406 | else 407 | if(al>=bl) 408 | [C,cl]=addArray(A,B,al,C); 409 | else 410 | [C,cl]=addArray(A,B,bl,C); 411 | endif 412 | Cneg=0; 413 | endif 414 | elseif(al==bl) 415 | flag=0; 416 | for i=1:al-1 417 | A2(1,i)=A(1,i); 418 | endfor 419 | if(flag==0) 420 | [C,flag]=Subtract(B,A2,C,al,flag); 421 | [C,cl]=EliminateZeros(C,al); 422 | #disp("ok") 423 | #C 424 | if(Aneg(1)==1 && Bneg==1) 425 | Cneg=1; 426 | else 427 | Cneg=0; 428 | endif 429 | endif 430 | if(flag) 431 | [C,flag]=Subtract(A,B,C,bl,1); 432 | #disp("ok2") 433 | #C 434 | [C,cl]=EliminateZeros(C,al); 435 | if(Aneg(1)==1 && Bneg==1) 436 | Cneg=0; 437 | else 438 | Cneg=1; 439 | endif 440 | endif 441 | elseif(al>bl) 442 | [C,flag]=Subtract(B,A,C,al,1); 443 | [C,cl]=EliminateZeros(C,al); 444 | Cneg=0; 445 | else 446 | [C,flag]=Subtract(A,B,C,bl,1); 447 | [C,cl]=EliminateZeros(C,bl); 448 | Cneg=1; 449 | endif 450 | Result=C; 451 | l=cl; 452 | Sign=Cneg; 453 | endfunction 454 | 455 | 456 | function [key,l,Status]=private(A,B,le,ltf) 457 | X=zeros(2,100); 458 | X(1,1)=1; X(2,1)=0; 459 | Xl1=2; Xl2=2; 460 | Xneg=zeros(2); 461 | Y=zeros(2,100); 462 | Y(1,1)=0; Y(2,1)=1; 463 | Yl1=2; Yl2=2; 464 | Yneg=zeros(2); 465 | D=zeros(2,100); 466 | Dneg=zeros(2); 467 | for i=1:ltf-1 468 | D(1,i)=B(i); 469 | endfor 470 | Dl1=ltf; 471 | for i=1:le-1 472 | D(2,i)=A(i); 473 | endfor 474 | D 475 | Dl2=le; 476 | K=zeros(200,1); 477 | [K,lk]=Division(D,Dl1,Dl2); 478 | while(1) 479 | #A 480 | C=zeros(1,100);Cneg=0; 481 | temp=0;k=1; 482 | for i=1:Xl2-1 483 | k=i; 484 | for j=1:(lk-1) 485 | x=X(2,i)*K(j)+temp+C(k); 486 | C(k++)=mod(x,10); 487 | temp=floor(x/10); 488 | endfor 489 | while(temp!=0) 490 | C(k++)=mod(temp,10); 491 | temp=floor(temp/10); 492 | endwhile 493 | endfor 494 | if(Xneg(2)==1) 495 | Cneg=1; 496 | endif 497 | [C,lc,Cneg]=Sub(X,C,Xl1,k,Xneg,Cneg); 498 | i=lc-1; 499 | disp("X:C") 500 | Cneg 501 | lc 502 | while(i>=1) 503 | C(i) 504 | i=i-1; 505 | endwhile 506 | for i=1:Xl2-1 507 | X(1,i)=X(2,i); 508 | endfor 509 | Xl1=Xl2; 510 | Xneg(1)=Xneg(2); 511 | for i=1:lc-1 512 | X(2,i)=C(i); 513 | endfor 514 | Xl2=lc; 515 | Xneg(2)=Cneg; 516 | Xneg; 517 | Xl1; 518 | Xl2; 519 | X; 520 | 521 | #B 522 | C=zeros(1,100);Cneg=0; 523 | temp=0;k=1; 524 | for i=1:Yl2-1 525 | k=i; 526 | for j=1:(lk-1) 527 | x=Y(2,i)*K(j)+temp+C(k); 528 | C(k++)=mod(x,10); 529 | temp=floor(x/10); 530 | endfor 531 | while(temp!=0) 532 | C(k++)=mod(temp,10); 533 | temp=floor(temp/10); 534 | endwhile 535 | endfor 536 | if(Yneg(2)==1) 537 | Cneg=1; 538 | endif 539 | [C,lc,Cneg]=Sub(Y,C,Yl1,k,Yneg,Cneg); 540 | for i=1:Yl2-1 541 | Y(1,i)=Y(2,i); 542 | endfor 543 | Yl1=Yl2; 544 | Yneg(1)=Yneg(2); 545 | for i=1:lc-1 546 | Y(2,i)=C(i); 547 | endfor 548 | Yl2=lc; 549 | Yneg(2)=Cneg; 550 | Yneg; 551 | Yl1; 552 | Yl2; 553 | Y; 554 | 555 | #D 556 | C=zeros(1,200);Cneg=0; 557 | temp=0;k=1; 558 | for i=1:Dl2-1 559 | k=i; 560 | for j=1:(lk-1) 561 | x=D(2,i)*K(j)+temp+C(k); 562 | C(k++)=mod(x,10); 563 | temp=floor(x/10); 564 | endfor 565 | while(temp!=0) 566 | C(k++)=mod(temp,10); 567 | temp=floor(temp/10); 568 | endwhile 569 | endfor 570 | [C,lc,Cneg]=Sub(D,C,Dl1,k,Dneg,Cneg); 571 | for i=1:Dl2-1 572 | D(1,i)=D(2,i); 573 | endfor 574 | Dl1=Dl2; 575 | Dneg(1)=Dneg(2); 576 | for i=1:k 577 | D(2,i)=C(1,i); 578 | endfor 579 | Dl2=lc; 580 | Dneg(2)=Cneg; 581 | #D 582 | #Dl2 583 | 584 | #break 585 | if((Dl2-1)==1 && D(2,1)==1) 586 | for i=1:Yl2-1 587 | Y(1,i)=Y(2,i); 588 | endfor 589 | Yl1=Yl2; 590 | Yneg(1)=Yneg(2); 591 | key=Y; 592 | l=Yl1; 593 | Status=Yneg 594 | break; 595 | endif 596 | 597 | #k 598 | [K,lk]=Division(D,Dl1,Dl2); 599 | endwhile 600 | endfunction 601 | 602 | [Dkey,Dl,Sign]=private(E,Totientfunc,le,ltf); 603 | Dkey 604 | i=Dl-1; 605 | while(i>=1) 606 | Dkey(1,i) 607 | i--; 608 | endwhile 609 | 610 | #Check wheteher D is >Totient function or not. Or whether Dkey is negative or Positive. 611 | 612 | if(Sign(1)==1) 613 | Sign(1)=0; 614 | #Sub(Dkey,Totientfunc,Dl,ltf,Sign,0); 615 | Dkey 616 | elseif(Dl>=ltf) 617 | disp("ok") 618 | endif 619 | 620 | --------------------------------------------------------------------------------