SIngle precision

advertisement
Cosc 413 Algorithms
Part 1
Outline of the RSA system
Generate large primes ‘p’ and ‘q’.
Multiply ‘p’ and ‘q’ to get ‘n’.
Find ‘ψ(n)’ = (𝑝 − 1)(π‘ž − 1)
Generate a co-prime ‘d’ between 1 and ψ(n).
Find the inverse ‘e’ which satisfies
𝑑 ∗ 𝑒 ≡ 1 π‘šπ‘œπ‘‘(ψ(n))
Send ‘n’ and ‘e’ to everyone.
Now that everyone knows your public key, anyone who wants to establish a connection with
you simply takes these two keys and sends you an encrypted message.
To encrypt a message ‘M’ to form cipher text ‘C’.
𝐢𝑖 = 𝑀𝑖 ∗ 𝑒 π‘šπ‘œπ‘‘ (n)
For size i message.
For example
If we have prime p = 13 and q = 17
N = 221 ψ(n)=192
Pick a random number between 1 and 192 which also a co-prime.
D = 53
Find the inverse of 53 and 192 which in this case is 29.
Now we can show the world our public key ‘n’ and ‘e’.
This works because the person who generated the public key is the only one who can decrypt
it efficiently (unless factorisation can be done quickly). All other people simply use ‘e’ and ‘n’
to encrypt data and send it off to the generator. RSA relies on the lack of ability to factorise
large numbers quickly, if a fast method is discovered, RSA will no longer be secure.
Running instructions.
Compile the program and run it. The program will crash if text.txt is not within the same
directory.
Enter will be prompted to proceed from one stage of the encryption to the next.
You will need to change the text.txt file to encrypt something smaller.
Yen-Sheng Pan
SID 94555779
int gcd(a,b){
int t;
while (b != 0){
t = b;
b = a % b;
a = t;
}
return a;
}
Finds the gcd of two parameters a and b
int findInverse(int m, int x){
int q,r,temp;
int s = m; int t = x;
/* set up for gcd(x,m) */
int a = 0; int b = 1;
/* 0*x = s and 1*x = t */
while(t) {
q = s/t, r = s%t;
/* quotient and remainder */
s = t, t = r; /* push back */
temp = (a-b*q) % m;
a = b, b = temp; /* push back */
}
return a;
Finds the inverse of the
two parameters m and x
}
int main(char* args, int argc){
unsigned int a[MAXSIZE/2];
unsigned int messageOut[MAXSIZE/2];
unsigned encryptedMessage[MAXSIZE/2];
int ENDINDEX;
long t = clock();
char c;
int primeP, primeQ;
int totn;
int d;
int e;
int numberOfChars = 0;
Initialisation of the local
parameter and reads the file
into the buffer.
//int j = findInverse(192,53,1);
printf("Reading file: %s");
fr = fopen("text.txt", "r");
i = 0;
while ((string[i] = fgetc(fr))!= EOF){
i++;
numberOfChars++;
if (i == MAXSIZE) break;
}
fclose(fr);
Yen-Sheng Pan
SID 94555779
//chose good primes
do{
primeP = getPrime();
primeQ = getPrime();
n = primeP*primeQ;
}while(n > 65536/2 || n <128*128 + 128);
totn = (primeP-1) * (primeQ-1);
Finds prime and calculates (p1)(q-1) totn
Then it finds the d and e which
are inverses of each other.
Sometimes finding the inverse
can return a negative value so
when that happens, add totn to
modulate it fully.
//chose random d
e = -1;
while(e == -1){
d = rand()%totn;
if (gcd(d,totn) == 1)
e = findInverse(totn,
d,log(totn,2));
while (e < -1){
e = (e + totn);
if (e < -1){
e += totn;
}
}
}
//initialising the arrays
for (i = 0; i < MAXSIZE/2; i++){
a[i] = messageOut[i] = encryptedMessage[i] = 0;
}
//transform into int with 2 chars
for (i = 0; i < MAXSIZE/2 ; i++){
if (string[i*2] == '\0') break;
a[i] = string[i*2]*128 + string[i*2+1];
printf("%d ",a[i]);
}
printf("\n");getchar();
//encode here
for (i = 0; i < MAXSIZE/2; i++){
if (a[i] == 0 ) break;
encryptedMessage[i] = exp1(a[i], e);
printf("%d ",encryptedMessage[i]);
}
printf("\n");
//decode here
for (i = 0; i < MAXSIZE/2; i++){
if (encryptedMessage[i] == 0 ) break;
messageOut[i] = exp1(encryptedMessage[i], e);
printf("%d ",messageOut[i]);
}
printf("\n");
getchar();
//turns back into text
for (i = 0; i < MAXSIZE/2; i++){
if (messageOut[i] == 0) break;
printf("%c%c", messageOut[i]/128,
messageOut[i]%128);
}
printf("\n");
Sets all parameters to 0
Transforms two characters
into an integer with 7
bytes offset.
Encrypts it with e the
public key
Decrypts with d the
inverse of e
Transforms an integer
into two characters
getchar();
printf("n= %d inverse= %d, e= %d, totn= %d, p= %d, q=
%d\n",n, inverse, e, totn, primeP, primeQ);
scanf("",&c);
getchar();
return 0;
}
Yen-Sheng Pan
SID 94555779
Download