Overview
A security fundamentals project comparing symmetric encryption modes (ECB vs CBC) through visual image encryption, plus implementing RSA public-key cryptography from scratch in C using BIGNUM arithmetic. View the code on GitHub.
Key Achievements
- Visually demonstrated ECB’s security weakness through image encryption
- Implemented RSA key generation, creating a valid public/private key pair
- Successfully encrypted and decrypted “A top secret!” using generated keys
Implementation
ECB vs CBC Encryption Comparison
| Method | Pros | Cons |
|---|---|---|
| ECB | Parallel encryption, tolerates lost blocks | Patterns preserved, identical plaintexts make identical ciphertexts |
| CBC | No pattern preservation, multi-use keys with IV | No parallel encryption, vulnerable to active attacks |

To compare CBC and ECB encryption, I encrypted the image above with both encryption methods and key = 123456789.
ECB Encryption (Insecure)

CBC Encryption (Secure)

RSA Encryption
RSA encryption is used to get symmetric session keys and has the following property: secret_key(public_key(message)) = message = public_key(secret_key(message))
The process of RSA key generation is as follows:
- Define large primes
pandq - Calculate
n=p*q - Calculate
phin=(p-1)*(q-1)using the BIGNUM functionBN_mul(n,p,q,context)
- Since 1 is not a BIGNUM, this is done as
phin = (p=hex of p -1) * (q=hex of q -1)
- Pick an
ethat is relatively prime tophin(ie their greatest common divisor is 1 ) - Calculate a unique private key
dsuch thated = 1 % phinusing the BIGNUM methodBN_mod_inverse(d,e,phin,context)sincedis the multiplicative inverse ofe % phin - The public key is then
(e,n)and the secret key is(d,n)
To encrypt a message:
cipher = message ^ e % n
To decrypt a message:
cipher ^ d % n = (message ^ e)^d = message
Using the above process, I determined
d = 3587A24598E5F2A21DB007D89D18CC50ABA5075BA19A33890FE7C28A9B496AEB.
After converting the plaintext message A top secret! to hex, I encrypted it using the public key (e,n) and cipher = message ^ e % n. Then I decrypted it using the private key d and message = cipher ^ d % n, which gives the hex value of A top secret!, verifying the encryption.
Technologies
C, BIGNUM Library, RSA Algorithm, ECB/CBC Encryption