Encryption and Decryption
University Projects #Cyber Security#C

ECB Encryption#

A simple encryption method that allows parallel encryption and tolerates lost/damaged blocks. However, patterns in plaintext are repeated in ciphertext. Additionally, if 2 messages are the same, they will have the same ciphertext.

CBC Encryption#

A fast and simple encryption method that does not repeat plaintext patterns in ciphertext. Additionally, allows multi use keys by adding a unique IV (key, IV) for each message. However, there is no parallel encryption and it is insecure against active attacks.

CBC vs ECB Encryption#

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

CBC Encryption Results The above image was encrypted with CBC and we can see that there are no patterns to see, only noise.

ECB Encryption Resuls The above image was encrypted with ECB and shows clear patterns and shapes similar to the original image.

Compared to CBC, ECB is less secure because clear patterns are visible.

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 and this c code, 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.

← Back to Projects