University Discrete Mathematics — Modular Arithmetic and Cryptography Worksheet
Arithmetic where only the remainder survives, and the encryption scheme that is built out of it. The two readings of "mod" — the relation a ≡ b (mod n) and the operation a mod n — and the rules that let a product or a power be reduced before it is worked out; the extended Euclidean algorithm turned into a modular inverse; linear congruences, which may have no solution, one, or several at once; Fermat's little theorem and repeated squaring, the two ways a huge exponent is brought down to something a hand can manage; and RSA, with keys small enough that you build one, encrypt with it and decrypt with it yourself. Have a look on this page, then print the free PDF when you want to write on it.
Practice worksheet — free PDF
No email, no account, no watermark. Teachers: photocopy it for your classes freely. Worked solutions and 6 harder problems come with the University Discrete Math bundle.
All 8 questions
Each question targets one named concept from the sheet. Read them here, or print the PDF — it has working space under each one.
-
Q1Congruences and Arithmetic Modulo n
Recall that for an integer , means , and is the remainder with and .
- Decide whether .
- Decide whether .
- Compute .
- List every integer with and .
-
Q2Congruences and Arithmetic Modulo n
Without a calculator and without multiplying out, find:
- the remainder when is divided by ;
- ;
- the last digit of .
-
Q3Modular Inverses and Linear Congruences
Use the extended Euclidean algorithm to find an inverse of modulo , that is, an integer with and . Check your answer.
-
Q4Modular Inverses and Linear Congruences
Solve each congruence, giving every solution modulo the stated modulus, or show that there is none.
-
Q5Fermat's Little Theorem and Modular Exponentiation
Fermat's little theorem states: if is prime and , then .
- Compute by repeated squaring: write in binary, tabulate , and multiply the powers you need.
- Use Fermat's little theorem to compute .
-
Q6RSA Encryption and Decryption
In RSA, one chooses distinct primes and , sets , chooses with , and lets be the inverse of modulo . The public key is , the private key is . A message is an integer with ; it is sent as and recovered as .
Take and .
- Compute and , and explain why is not an allowed choice.
- With , find the private key .
- Encrypt the message .
-
Q7RSA Encryption and Decryption
An RSA key has public part and private key . (A message with is sent as and recovered as .) You receive . Decrypt it by repeated squaring, and check your answer by re-encrypting it.
-
Q8Synthesis — drawing on several topics in this unit
Find every with such that .
The 6 challenge problems for this topic are a separate, paid sheet and are not reproduced here.
What does this set assume? This set assumes Secondary 5 mathematics and nothing past it — no calculus and no linear algebra, whatever a calendar lists as a prerequisite. What it does lean on comes from earlier in this course. From Divisibility and Primes: the division algorithm and the quotient-remainder form of an integer, the meaning of a | b, gcd(a, b), and the Euclidean algorithm together with its extended form, which is how a modular inverse is actually found here. From Sets, Functions and Relations: the idea of an equivalence relation, because congruence modulo n is one, and its classes are what "working modulo n" quietly means. Beyond that, ordinary algebraic manipulation and the laws of exponents. Every key in the cryptography sheets is small enough to work by hand and no question needs software or a calculator. Deliberately out of scope: the Chinese remainder theorem and systems of simultaneous congruences — a single linear congruence is as far as this set goes — and representing integers in any base other than ten, with binary appearing only as the bookkeeping device inside fast exponentiation.
Which course is this for? In the public course calendars of Montreal universities, this material is part of the courses numbered MATH 240 and MAT210. Each course orders and weights the topics its own way, so check your own outline for what your exam covers. Which sets match your course.
How to do every concept on this sheet
This is the part worksheet sites usually leave out. Below is the actual reasoning behind each group of questions — not a full solution set, the decisions that get you to one. Read it before you start, or after you get stuck.
Two meanings of "mod", and why both are needed
The same three letters do two different jobs. As a relation, a ≡ b (mod n) is a statement about a pair of integers: it is true exactly when n divides their difference. As an operation, a mod n is a single number — the remainder the division algorithm produces, pinned to the range from 0 up to but not including n. Q1 opens with both definitions written out and then asks you to use each one.
a ≡ b (mod n) means n | (a − b) a mod n = r where a = qn + r and 0 ≤ r < nTo decide a congruence, subtract and divide. Parts (a) and (b) of Q1 are both settled the same way: form the difference of the two sides and ask whether the modulus goes into it a whole number of times. Do not reduce each side separately and compare by eye — that works, but it is the slower route and it invites a sign slip. Write the difference, write it as the modulus times something, and the question is answered either way.
A negative number still has a non-negative remainder. Q1(c) is the trap. The remainder is defined to satisfy 0 ≤ r < n, so the quotient for a negative a is the integer below the exact quotient, not the one obtained by ignoring the sign. Find the largest multiple of the modulus that is less than or equal to a and subtract it; whatever is left is the answer, and it is never negative.
Q1(d) shifts from a yes-or-no question to a set. Everything congruent to a fixed value modulo 7 sits on an arithmetic progression with common difference 7, so start from the stated value and step up and down by the modulus until you leave the interval at both ends. Listing them is worth doing deliberately: that list is one equivalence class of congruence modulo 7, which is the connection back to Sets, Functions and Relations.
Reducing before you multiply
The reason modular arithmetic is usable at all is that it respects the operations. If a ≡ b and c ≡ d modulo the same n, then the sums are congruent and so are the products — and, by repeating the product rule, so are the powers. That licence is what Q2 is built to exercise, and its instruction not to multiply out is the point of the question rather than a restriction on it.
a ≡ b, c ≡ d (mod n) ⇒ a + c ≡ b + d and a·c ≡ b·d (mod n)Reducing an expression modulo n
Four moves, in this order, every time.
- 1Reduce every ingredient first
Replace each number by its remainder modulo n before any arithmetic. Small numbers, and often a negative one just below the modulus, which is easier still.
- 2Do one operation, then reduce again
Never carry a number larger than the modulus into the next step. The whole saving is in reducing between operations rather than at the end.
- 3For a power, look for a small cycle
Compute successive powers of the base modulo n until one of them comes back to 1. From there the exponent only matters modulo the length of that cycle.
- 4Write the answer in range
Finish with the representative between 0 and n − 1, even if a negative one was more convenient mid-calculation.
Q2(a) is steps 1 and 2 on a sum of a product and a square. Q2(b) is step 3: find where the powers of 2 return to 1 modulo 7, divide the exponent 50 by that cycle length, and let the remainder pick the answer out of the short list you already built. Q2(c) is the same idea in disguise — the last decimal digit of a number is that number modulo 10, so the question is really asking for 7⁸³ mod 10, and the powers of 7 cycle quickly there too.
The exponent is not reduced modulo n. A very common wrong move is to shrink the exponent modulo the modulus. The exponent is reduced modulo the cycle length — the smallest positive power that returns to 1 — which is a different number and has to be found before it can be used.
Modular inverses and the extended Euclidean algorithm
Division has no direct meaning modulo n. What replaces it is an inverse: a number s with a·s ≡ 1 (mod n). It exists exactly when a and n are coprime, and the reason is Bézout's identity from Divisibility and Primes — the extended Euclidean algorithm produces integers s and t with a·s + n·t = gcd(a, n), and when that gcd is 1, reading the identity modulo n makes the n·t term vanish and leaves a·s ≡ 1.
a·s + n·t = 1 ⇒ a·s ≡ 1 (mod n) so s is an inverse of a modulo nFinding an inverse, as Q3 asks
Forward to the gcd, then back for the coefficients.
- 1Run the Euclidean algorithm
Divide, keep the remainder, repeat. Record each line in the form dividend = quotient × divisor + remainder; you will need every one of them.
- 2Check the gcd is 1
If it is not, stop: no inverse exists, and saying so is the correct answer rather than a dead end.
- 3Back-substitute
Start from the line whose remainder is the gcd, solve it for that remainder, and substitute the previous line's remainder into it. Never multiply out the two numbers you are tracking — keep them as coefficients.
- 4Put the coefficient in range
The coefficient of a may come out negative. Add the modulus until it lands in 0 ≤ s < n, which is what the question asks for.
Q3 also asks you to check the answer, and that instruction is not decoration. Multiply your candidate by 11, reduce modulo 26, and confirm you land on 1. The check costs one line and catches every sign error the back-substitution can make.
Linear congruences: none, one, or several
Q4 asks for every solution modulo the stated modulus, or a demonstration that there is none, and the three parts are deliberately different cases. The single fact that decides which case you are in is the gcd of the coefficient and the modulus.
ax ≡ b (mod n) has a solution ⇔ gcd(a, n) | bSolving ax ≡ b (mod n)
Compute the gcd before anything else.
- 1Let d = gcd(a, n)
If d does not divide b, there is no solution. Say why: every value of ax is a multiple of d, so it can never be congruent to something that is not.
- 2If d = 1, invert
Find the inverse of a modulo n by the method of Q3 and multiply both sides by it. Exactly one solution modulo n.
- 3If d divides b and d > 1, divide through
Divide a, b and the modulus n all by d. The reduced congruence has coprime coefficient and modulus, so step 2 solves it.
- 4Lift back to the original modulus
One solution modulo n/d becomes d solutions modulo n: add n/d repeatedly until you are back at the start. List all of them — that is what "every solution" means.
You may not cancel a common factor without dividing the modulus too. Going from 12x ≡ 20 (mod 28) to 3x ≡ 5 (mod 28) is the error this question exists to expose: cancelling 4 from both sides while leaving the modulus alone changes the problem. The modulus is divided by the same factor, or it is not cancelled at all.
Each part of Q4 deserves the same opening line — the gcd of the coefficient and the modulus — written down before any attempt to solve. It tells you in one step which of the three endings the part has, and it costs less than discovering it halfway through.
Fermat's little theorem and repeated squaring
Two tools bring an enormous exponent down to size, and Q5 puts one in each part so that the difference between them is visible. Repeated squaring works for any modulus; Fermat's little theorem is sharper but needs a prime modulus and a base the prime does not divide.
p prime and p does not divide a ⇒ aᵖ⁻¹ ≡ 1 (mod p)Q5(a) spells out the repeated-squaring routine, which is worth learning as a fixed procedure. Write the exponent in binary; that tells you which powers of the base you need. Build a short table of a, a², a⁴, a⁸ and so on, each entry the square of the one above it reduced modulo the modulus immediately. Then multiply together only the table entries whose binary bit is 1, reducing after each multiplication. The table is never longer than the number of binary digits, which is why the method scales.
Reduce inside the table, not after it. The entire benefit disappears if you square the real numbers and reduce at the end. Each row of the table is a number smaller than the modulus; if a row is bigger, reduce it before writing the next.
Q5(b) is the Fermat route. Check the hypotheses first — the modulus 13 is prime and it does not divide 7 — then divide the exponent 2026 by 12 and write the exponent as 12 times the quotient plus a remainder. The theorem collapses the whole first part to 1, and only the small leftover power is left to compute. The arithmetic that remains is a few lines.
Fermat needs a prime modulus. Applying aⁿ⁻¹ ≡ 1 with a composite n is the standard error, and it does not announce itself — the calculation runs smoothly and gives a wrong answer. Check primality of the modulus before quoting the theorem, and say in your solution that you did.
Building an RSA key pair
Q6 states the whole scheme in its opening paragraph and then walks you through it with two small primes, so nothing is a black box. The three quantities that matter are n = pq, which is public; (p − 1)(q − 1), which is the modulus the exponents live in and must stay secret; and the pair e, d, which are inverses of each other modulo that second number.
Where each number lives
Two moduli, and they are never interchangeable.
- 1Messages live modulo n
m and c are both reduced modulo n = pq. That is the arithmetic of encrypting and decrypting.
- 2Exponents live modulo (p − 1)(q − 1)
d is the inverse of e there, not modulo n. Mixing the two is the single most expensive slip in this sheet.
- 3e must be coprime to (p − 1)(q − 1)
Otherwise no inverse exists and there is no private key at all. This is the gcd condition from Q3, reappearing as a design rule.
Q6(a) asks for both moduli and then for a reason a particular exponent is not allowed. Compute (p − 1)(q − 1) first, then test the proposed e against the coprimality condition in step 3 — the explanation is whatever that gcd calculation shows, stated as a sentence rather than as a number. Q6(b) is Q3's extended Euclidean algorithm again, run in the exponent modulus. Q6(c) is a modular exponentiation with a small exponent: reduce as you go and finish inside the range 0 ≤ c < n.
The security is an asymmetry, not a secret formula. Everything in the public key is given to anyone. What an attacker lacks is the factorisation of n into p and q; without it, (p − 1)(q − 1) is unavailable, and without that, d cannot be computed. With primes this small you can factor n on sight, which is exactly why these are teaching keys and not real ones.
Decrypting, and checking that you did
Q7 hands you a complete key pair and a ciphertext, and asks for the plaintext by repeated squaring. The private exponent is large enough that Q5(a)'s table is the only sensible route: write 43 in binary, build the table of squares of the ciphertext modulo 77, and multiply the selected rows together.
c = mᵉ mod n m = cᵈ mod nThen do what the question asks and re-encrypt. Raising your recovered plaintext to the public exponent modulo n must return the ciphertext you started from. This is a genuine verification rather than a formality: the two exponentiations use different exponents and different tables, so an arithmetic slip in one will not be reproduced by the other. Any question in this sheet that produces a message can be checked this way, and a solution that shows the check is a stronger piece of writing than one that does not.
Keep the plaintext in range. The scheme only recovers m when 0 ≤ m < n. A value outside that range comes back as its remainder modulo n, which is a different number, and the re-encryption check is what makes that visible.
The synthesis question
Q8 needs both halves of the set in one line of work. The right side is a large power modulo a prime, so it is handled by Fermat's little theorem exactly as in Q5(b) — reduce the exponent against p − 1 first, and a manageable power is left. The left side is then a linear congruence in the shape of Q4: compute the gcd of the coefficient and the modulus to establish how many solutions there are, find the inverse of the coefficient by the extended Euclidean algorithm, and multiply through.
Reduce the right side before touching the left. The two steps are independent, and doing them in the other order means carrying an unevaluated power through an inversion. Get the right side down to a single number between 0 and 12, then solve.
Preview all 5 pages
Click any page to open the full PDF.
Getting the most out of it
Reduce early and reduce often
The habit that makes every question in this set easy is replacing each number by its remainder before doing anything with it, and again after each operation. Nothing larger than the modulus should ever appear in your work. If a big number has appeared, you have made the question harder than it was set.
Write the gcd first
Whether an inverse exists, whether a linear congruence has solutions, how many it has, and whether an RSA exponent is legal are all the same question about a gcd. Make computing it the first line of any of those questions, before you decide what method to use.
Check by substituting back
Every answer in this set is checkable in one line: multiply the inverse by the original number, substitute the solution into the congruence, re-encrypt the message you decrypted. These checks are cheap and they catch precisely the arithmetic slips that modular work invites, so build them into the answer rather than leaving them for a spare moment.
Keep a table of small powers
For each modulus you meet, jotting down the first several powers of the base takes under a minute and often reveals a short cycle that makes the rest of the question trivial. It is also the table repeated squaring needs, so the work is never wasted.
Say which modulus you are in
In the RSA questions two moduli are live at once, n for messages and (p − 1)(q − 1) for exponents. Label every congruence you write with its modulus. Marks in this chapter are lost far more often to working in the wrong one than to bad arithmetic.
Want the solutions, or something more challenging?
The worksheet, the questions above and every explanation on this page stay free permanently. Three more PDFs exist for this topic — the worked answer key, a harder problem set, and the answer key to that. They come with the University Discrete Math Solutions Bundle, beside the unit notes and the unit test, which is what keeps the rest of the series free.
What else exists for Modular Arithmetic and Cryptography
Three PDFs · 10 pages · all three are in the bundle below.
- Answer key — 2 pages. All 8 questions worked step by step, including the restrictions and the justifications. Not a list of final answers.
- Challenge problems — 6 pages, 6 problems. A separate sheet at exam-plus difficulty covering the same 4 concepts. Harder than anything on the free sheet.
- Challenge answer key — 2 pages. Every challenge problem worked to the same standard, with the checks shown.
- PDF, letter size, print-ready.
The one thing that's for sale
Every University Discrete Math topic — the complete Solutions Bundle
One download, one payment, the whole program. For all 9 University Discrete Math units: the worksheet, the reference notes, the challenge set, the unit test and every answer key — including this one.
- Worked solutions, not answer lists — every step written out
- Covers the whole year's program at this level
- Less than the price of one hour of tutoring — for the entire year's solutions
Taking Secondary 1 Math as well? The Secondary 1 Math bundle covers all 15 of its units — 106 PDFs, 474 pages — on the same terms.
Taking Secondary 2 Math as well? The Secondary 2 Math bundle covers all 14 of its units — 98 PDFs, 449 pages — on the same terms.
Taking Secondary 3 Math as well? The Secondary 3 Math bundle covers all 11 of its units — 77 PDFs, 367 pages — on the same terms.
Taking Secondary 4 Math as well? The Secondary 4 Math bundle covers all 17 of its units — 122 PDFs, 466 pages — on the same terms.
Taking Secondary 5 Math as well? The Secondary 5 Math bundle covers all 21 of its units — 147 PDFs, 589 pages — on the same terms.
Taking CEGEP Calculus I as well? The CEGEP Calculus I bundle covers all 9 of its units — 72 PDFs, 371 pages — on the same terms.
Taking CEGEP Calculus II as well? The CEGEP Calculus II bundle covers all 8 of its units — 64 PDFs, 335 pages — on the same terms.
Taking CEGEP Linear Algebra as well? The CEGEP Linear Algebra bundle covers all 7 of its units — 56 PDFs, 298 pages — on the same terms.
Taking University Calculus III as well? The University Calculus III bundle covers all 9 of its units — 72 PDFs, 516 pages — on the same terms.
Taking University Linear Algebra as well? The University Linear Algebra bundle covers all 9 of its units — 72 PDFs, 496 pages — on the same terms.
Taking University Differential Equations as well? The University Differential Equations bundle covers all 9 of its units — 36 PDFs, 182 pages — on the same terms.
Taking University Business Math as well? The University Business Math bundle covers all 9 of its units — 36 PDFs, 190 pages — on the same terms.
Taking University Introductory Statistics as well? The University Introductory Statistics bundle covers all 9 of its units — 36 PDFs, 186 pages — on the same terms.
Taking AP Calculus AB as well? The AP Calculus AB bundle covers all 8 of its units — 64 PDFs, 527 pages — on the same terms.
Common questions
Is this worksheet really free?
Yes — the questions are on this page to read, and the PDF downloads directly, no email and no account. The one paid item is optional: the complete University Discrete Mathematics Solutions Bundle, which covers every set at this level.
Which university courses is this for?
The course codes listed on this page are taken from the public course calendars of universities that teach discrete mathematics to mathematics, computer science and engineering students. Each course orders and weights the chapters its own way, and some reach a chapter late in the term or not at all — so check the outline for your own section to see where this set falls in your term.
What do I need to know before starting this set?
Secondary 5 algebra, and two earlier sets in this course: Divisibility and Primes, for the division algorithm, gcd(a, b) and the extended Euclidean algorithm, and Sets, Functions and Relations, for what an equivalence relation is. No calculus and no linear algebra are used anywhere here.
What is the difference between a ≡ b (mod n) and a mod n?
The first is a relation — a statement that is true or false about two integers, meaning that n divides their difference. The second is an operation whose value is a single number, the remainder in the range from 0 up to n − 1. Many integers are congruent to a given one; only one of them is its remainder.
Does every number have an inverse modulo n?
No. A number a has an inverse modulo n exactly when gcd(a, n) = 1. When they share a factor, no multiple of a is ever congruent to 1, and the extended Euclidean algorithm shows why by returning a gcd bigger than 1 instead.
How many solutions can a linear congruence have?
Let d = gcd(a, n). If d does not divide b there are none. If it does, there are exactly d solutions modulo n, spaced n/d apart — so a congruence can have none, one, or several, and computing d first tells you which before you solve anything.
Why can I reduce the base but not the exponent modulo n?
Congruence is preserved by multiplication, so replacing the base by a congruent number is always safe. The exponent counts how many multiplications happen, which is a different kind of quantity. It can be reduced, but modulo the length of the cycle of powers — the smallest positive power that returns to 1 — not modulo n.
Do I need a calculator or software for these questions?
No. Every modulus and every key here is small enough to work with by hand, and the methods — reducing as you go, repeated squaring, the extended Euclidean algorithm — are designed so the arithmetic stays small. A question that looks impossible without a machine is a signal that a reduction has been missed.
Can teachers use this in class?
Yes. Print and photocopy it for your own classes freely — I just ask that the tutorinmontreal.ca footer stays on the page.
I'm stuck on one question. Can you help?
Yes — through one-on-one tutoring, in Montreal or online. Get in touch to arrange a session, or see the current rates.
← All 9 University Discrete Math worksheets · Secondary 1 Math series (15 sheets) → · Secondary 2 Math series (14 sheets) → · Secondary 3 Math series (11 sheets) → · Secondary 4 Math series (17 sheets) → · Secondary 5 Math series (21 sheets) → · CEGEP Calculus I series (9 sheets) → · CEGEP Calculus II series (8 sheets) → · CEGEP Linear Algebra series (7 sheets) → · University Calculus III series (9 sheets) → · University Linear Algebra series (9 sheets) → · University Differential Equations series (9 sheets) → · University Business Math series (9 sheets) → · University Introductory Statistics series (9 sheets) → · AP Calculus AB series (8 sheets) →



