CS100A Lecture 13 15 Oct. 1998

advertisement
CS100A Lecture 13
15 Oct. 1998
Discussion of Prelim 2
(Tuesday, 20 October, 7:30-9PM)
Rooms for Prelim 2
A-K: Hollister B14
L-Z: Kimball B11
Cryptography:
Encryption-Decryption
using arrays (assignment 6)
CS100A, Lecture 13, 15
October 1998
1
Prelim 2
1. Everything that was on Prelim 1. In particular:
•Be able to define four kinds of variables: local
variables, parameters, fields (instance variables) and
static fields (class variables)
• Know the three kinds of methods (procedures,
functions, constructors)
•Know what an argument is.
•Know precisely the steps in executing a method call.
•Know precisely the steps in evaluating new C(…)
2. Loops: Know what a loop invariant is and how it is
used. Be able to write a loop, given an invariant. Write
simple loops without be given an invariant.
3. Arrays: know how to declare an array, allocate an
array, use an array.
4. Understand type char.
CS100A, Lecture 13, 15
October 1998
2
CS100A Lect. 13, 15 Oct. 1998
Cryptography:
Encryption-Decryption
using arrays
(assignment 6)
Cryptography even before Caesar’s time: Encode
messages, with the hope that only “friends”, who have
been given the decoding scheme, can decode them.
“Have a nice day”
replace every character by the next one: a --> b, etc.
“Ibwf b ojdf ebz”
CS100A, Lecture 13, 15
October 1998
3
During world war II, the Germans encoded
messages using a kind of “computer” (not a real
computer, as we know them today) they had
built, called the Enigma.
The British had a group that worked continuously to intercept and decode the messages.
They succeeded in breaking the code, and this
was one reason for the success of the Allies. At
times, they couldn’t use what they had learned
because they didn’t want the Germans to suspect
that their codes had been broken.
Alan Turing, a mathematician who did a lot for
computing (about 1936) even before computers
were developed, had a big part in this. You’ll
learn about Turing’s contributions to the theory
of computing --the Turing Machine-- in CS481.
CS100A, Lecture 13, 15
October 1998
4
Two types of cryptosystems:
• Secret key: both the sender and the receiver have the
key to encoding/decoding. Hopefully, no one else
does. How to send the receiver the secret key
(assuming it has to be changed) without others
intercepting it?
•Public key-private key: Gries decides on a public
key - private key pair. He makes the public key
available to everyone. Anyone wanting to send Gries
an encoded message encodes it using the public key.
Only Gries, who knows the private key, can decode the
message. Diffie and Hellman published the idea in
1976, but without a good implementation.
•RSA (by Ron Rivest, Adi Shamir, and Leonard
Adelman) found a way to implement it, using number
theory. This assignment concerns the RDA public key
- private key method.
CS100A, Lecture 13, 15
October 1998
5
long integers:
-9223372036854775808.. 9223372036854775807
Public key (puk, m)
Private key (prk, m)
Examples:
puk
401
229
241
109
prk
137
349
481
m
551
399
551
493
Send Gries/Cardie messages using public key (109, 493).
Only they can decode them because only they know the
value prk. With small numbers, it can be guessed, but
remember that these can be long integers --or even larger
integers if we use some other representation of itnegers in
Java.
We don’t show how to generate public key - private key
pairs. Must be hard to guess the prk. For example, given
two primes p1 and p2, it’s easy to calculate p1*p2. But,
given p1*p2, it’s very hard to calculate p1 or p2! Easy to
multiply two integers; hard to factor some integers.
CS100A, Lecture 13, 15
October 1998
6
Arithmetic modulo m (for m>0)
Numbers can get too big when encrypting and decrypting
(bigger than the biggest number in type long). We need a
way to keep integers small. Use arithmetic modulo m, in
which all integers are kept in the range 0..m-1.
For any integer i,
mod(i,m), or i mod m ,
is the integer that satisfies
i = q*m+r and 0<=r < m (for some q)
6 mod 5 = 1
5 mod 5 = 0
4 mod 5 = 4
3 mod 5 = 3
2 mod 5 = 2
1 mod 5 = 1
0 mod 5 = 0
-1 mod 5 = 4
-2 mod 5 = 3
-3 mod 5 = 2
-4 mod 5 = 1
-5 mod 5 = 0
To calculate (i mod m):
If i >=0: Use i%m
(remainder when i is divided by m)
If i<0: Use (i%m) + m
See method mod in class Crypto for an analysis.
CS100A, Lecture 13, 15
October 1998
7
Use arithmetic modulo m
When encrypting and decrypting, after EVERY operation that might produce an integer r that is larger than
m, reduce it modulo m, that is, use r mod m instead!
RSA: To encrypt an integer i as an integer j, use
j = i puk mod m
To decrypt an integer j to yield i, use
i = j prk mod m
In RSA, puk. prk, and m are chosen to guarantee that
i = ( i puk mod m)prk mod m
CS100A, Lecture 13, 15
October 1998
8
Encrypt a String s of characters as a
long array c[0..s.length()]
Each element c[i] of c is the encryption of s[i]:
((int) s.charAt(i)) puk mod m
To decrypt long array c and produce the String s: each
character s[i] is
(char) (c[i] prk mod m)
Example: the String “CS100” with prk = 401 and m
=551 is encrypted as the array
{383, 277, 197, 98, 98}
CS100A, Lecture 13, 15
October 1998
9
Download