2. The Mighty Mod Function
● We deﬁne the modulo n function as follows:
If x = qn + r, 0 ≤ r ≤ n, then x mod n = r.
● In other words, x mod n is the nonnegative remainder
(also called the residue of x modulo n) when x is
divided by the positive integer n.
Section 4.5 The Mighty Mod Function 2
Wednesday, March 24, 2010
3. Hashing
● A hash function h: S → T, where the domain S is a set
of text strings or integer values and the codomain T is
the set of integers {0, 1, … , t − 1}, where t is some
relatively small positive integer.
● If the domain S consists of text strings, we can
imagine them encoded in some way into integer
values.
● For example, an algorithm as simple as converting
each individual letter of a text string into its position
in the alphabet and adding up the resulting list.
● The function h therefore maps a potentially large set
of values S into a relatively small window of integer
values T. Consequently h isn’t likely to be a one-to-
one function.
Section 4.5 The Mighty Mod Function 3
Wednesday, March 24, 2010
4. Hashing
● A hash function is often used as part of a search algorithm.
● In a search using a hash function, n elements are stored in an
array (a one-dimensional table) called a hash table, where the
array is indexed from 0 through t − 1; the table is of size t.
● The element x is passed as the argument to the hash function,
and the resulting h(x) value gives the array index at which the
element is then stored.
● Later, when a search is carried out, the target value is run
through the same hash function, giving an index location in the
hash table, which is where to look for the matching stored
element.
Section 4.5 The Mighty Mod Function 4
Wednesday, March 24, 2010
5. Hashing
● However, because the hash function is not one-to-one,
things are not quite that simple.
● Different values may hash to the same array index,
producing a collision.
● Here are several collision resolution algorithms
available. One is called linear probing.
■
Keep going in the array and store element x in the next
available empty slot.
● Another method, called chaining, builds a linked list
for each array index.
Section 4.5 The Mighty Mod Function 5
Wednesday, March 24, 2010
6. Hashing
● The following are the desirable properties of a hash
function:
1. Given an argument value x, h(x) can be computed
quickly.
2. The number of collisions will be reduced because h(x)
does a good job of distributing values throughout the
hash table.
● Use of a modulo function as the hash function
accomplishes goal 1.
● Goal 2 is harder to achieve, but distribution seems to
work better on the average if the table size (the
modulo value) is a prime number.
Section 4.5 The Mighty Mod Function 6
Wednesday, March 24, 2010
7. Hashing
● The average number of comparisons required to
search for an element using hashing depends on the
ratio of n to the total table size t.
● If this ratio is low, then (using linear probing) there
are lots of empty slots, so you won’t have to look very
far to ﬁnd a place to insert a new element into the
table.
● If this ratio is low and chaining is used, the average
length of any linked list you may have to
(sequentially) search for a target element should be
short.
● This ratio n/t is called the load factor of the hash
table.
Section 4.5 The Mighty Mod Function 7
Wednesday, March 24, 2010
8. Computer Security
● The mod function plays a part in many aspects of
security.
● Military information, ﬁnancial information, and
company proprietary information that must be
transmitted securely uses some encoding/decoding
scheme.
● The original information (called the plaintext) is
encrypted using an encryption key, resulting in coded
text called the ciphertext.
● The ciphertext is transmitted, and when it is received,
it can be decoded using the corresponding decryption
key. Encryption and decryption are inverse functions
in the sense that:
■
decryption(encryption(plaintext)) = plaintext
Section 4.5 The Mighty Mod Function 8
Wednesday, March 24, 2010
9. Computer Security
● Cryptography is the study of various encryption/
decryption schemes.
● Military use of cryptographic techniques can be traced
back to Julius Caesar, who sent messages to his
generals in the ﬁeld using a scheme now known as the
Caesar cipher.
● Let us assume that plaintext messages use only the 26
capital letters of the alphabet, that spaces between
words are suppressed, and that each letter is ﬁrst
mapped to its corresponding position in the alphabet.
Section 4.5 The Mighty Mod Function 9
Wednesday, March 24, 2010
10. Computer Security
● We’ll denote this mapping as the bijection g: {A, … ,
Z} → {0, … , 25}.
● Then a positive integer key value k is chosen that
shifts each number k positions to the right with a
“wrap-around” back to the beginning if needed (this is
the mod function).
● Finally, the function g−1 is applied to translate the
resulting number back into a letter.
● The encoding function is given by:
■
f(p) = g−1([g(p) + k] mod 26)
● The decoding function is:
■ f −1(c) = g−1([g(c) − k] mod 26)
Section 4.5 The Mighty Mod Function 10
Wednesday, March 24, 2010
11. Computer Security
● The Caesar cipher is a simple substitution cipher,
meaning that each plaintext character is coded
consistently into the same single ciphertext character.
● Encryption techniques where a single plaintext
character contributes to several ciphertext characters
introduce diffusion. The advantage to diffusion is that
it hides the frequency statistics of individual letters,
making analysis of an intercepted ciphertext message
much more difﬁcult.
● DES (Data Encryption Standard) is an
internationally standard encryption algorithm
developed in 1976. DES was developed to safeguard
the security of digital information, so we may consider
the plaintext to be a string of bits.
Section 4.5 The Mighty Mod Function 11
Wednesday, March 24, 2010
12. Computer Security
● DES is a block cipher.
● A block of 64 plaintext bits is encoded as a unit using
a 56-bit key.
● This results in a block of 64 ciphertext bits.
● Changing one bit in the plaintext or one bit in the key
changes about half the resulting 64 ciphertext bits, so
DES exhibits high diffusion.
● Because the DES algorithm is well known, the only
“secret” part is the 56-bit key that is used.
Section 4.5 The Mighty Mod Function 12
Wednesday, March 24, 2010
13. Computer Security
● AES (Advanced Encryption Standard) is also a
block encryption scheme, but it uses a key length of
128 bits or more.
● AES also uses a form of the Euclidean algorithm.
● Disadvantage of both DES and AES is that they are
symmetric encryption (also called private key
encryption) schemes. The same key is used to both
encode and decode the message.
● In a private key encryption scheme, both the sender
and receiver must know the key. The problem of
securely transmitting a message turns into the problem
of securely transmitting the key to be used for the
encryption and decryption.
Section 4.5 The Mighty Mod Function 13
Wednesday, March 24, 2010
14. Computer Security
● Asymmetric encryption (public key encryption)
schemes use different keys for encoding and decoding.
● The decryption key cannot be derived in any practical
way from the encryption key, so the encryption key
can be made public.
● Anyone can send a message to the intended receiver in
encrypted form using the receiver’s public key, but
only the intended receiver, who has the decryption key,
can decode it.
● The best-known asymmetric encryption scheme is the
RSA public key encryption algorithm.
Section 4.5 The Mighty Mod Function 14
Wednesday, March 24, 2010
15. Hashing for Password Encryption
● A cryptographic hash function is a form of
encryption that does not require storing an encryption
key.
● A hash function is often used to encrypt passwords.
The ideal cryptographic hash function h has two
characteristics:
■
Given x, it is easy to compute the hashed value h(x).
■
Given a hashed value z, it is difﬁcult to ﬁnd a value x
for which h(x) = z.
● Because of these characteristics, a hash function is
also called a one-way encryption.
Section 4.5 The Mighty Mod Function 15
Wednesday, March 24, 2010
16. Generating and Decomposing Integers
● The modulo function provides an easy way to generate
integer values within some range 0 through n − 1 for some
positive integer n.
● Take any positive integer m and compute m mod n.
● If you have a function to generate a random (or
pseudorandom) integer m, this process generates a random
(or pseudorandom) integer within the desired range.
● You may also want to cycle through the integers in this
range in a controlled fashion.
● Addition modulo n is deﬁned on the set of integers {0, 1,
2, … , n - 1} by:
x +n y = (x + y) mod n
■
Section 4.5 The Mighty Mod Function 16
Wednesday, March 24, 2010
Be the first to comment