Encryption and Decryption
University Projects #Cyber Security#C

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#

MethodProsCons
ECBParallel encryption, tolerates lost blocksPatterns preserved, identical plaintexts make identical ciphertexts
CBCNo pattern preservation, multi-use keys with IVNo 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)

ECB Encryption Resuls

CBC Encryption (Secure)

CBC Encryption Results

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:

  1. Define large primes p and q
  2. Calculate n=p*q
  3. Calculate phin=(p-1)*(q-1) using the BIGNUM function BN_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)
  1. Pick an e that is relatively prime to phin (ie their greatest common divisor is 1 )
  2. Calculate a unique private key d such that ed = 1 % phin using the BIGNUM method BN_mod_inverse(d,e,phin,context) since d is the multiplicative inverse of e % phin
  3. 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

← Back to Projects