Introduction To Cryptography 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

What is Cryptography?

Cryptography is a method of protecting information and communications through the


use of codes, so that only those for whom the information is intended can read and
process it.
In computer science, cryptography refers to secure information and communication
techniques derived from mathematical concepts and a set of rule-based calculations
called algorithms, to transform messages in ways that are hard to decipher. These
deterministic algorithms are used for cryptographic key generation, digital signing,
verification to protect data privacy, web browsing on the internet and confidential
communications such as credit card transactions and email.
Evolution of Cryptography
Ancient Cryptography:
1. Pre-Modern Era:
Ancient civilizations used various methods to encode messages, such as substitution ciphers
(e.g., Caesar cipher) and transposition ciphers (e.g., Scytale).
The primary goal was often to obscure the meaning of messages from unauthorized
individuals.
Classical Cryptography:
1. Monoalphabetic Ciphers:
The Renaissance saw the development of more sophisticated substitution ciphers like the
Vigenère cipher.
These methods were vulnerable to frequency analysis.
2. Polyalphabetic Ciphers:
In the 16th century, the Vigenère cipher introduced the concept of using multiple alphabets
to increase security.
However, these methods were still susceptible to attacks.
Evolution of Cryptography
World Wars and Modern Cryptography:
1. Enigma Machine (WWII):
The Enigma machine, used by the Germans during WWII, was a complex electromechanical cipher device.
Breaking Enigma codes was a significant achievement in Allied cryptography.
2. Public Key Cryptography (1970s):
The invention of public-key cryptography, with algorithms like RSA, marked a revolutionary shift.
Public-key cryptography allows secure communication without a shared secret key, addressing key distribution
challenges.
Digital Age and Modern Cryptography:
1. Advanced Encryption Standard (AES):
AES became the standard symmetric encryption algorithm, replacing older algorithms like DES.
It is widely used for securing sensitive information in various applications.
2. Secure Sockets Layer (SSL) and Transport Layer Security (TLS):
These cryptographic protocols provide secure communication over a computer network, commonly used for
securing web traffic.
3. Blockchain and Cryptocurrencies:
Cryptocurrencies like Bitcoin rely on cryptographic techniques, providing secure and decentralized transactions
through blockchain technology.
What does the future of cryptography hold?

Cryptography will be an essential key in major developing innovations such as:


Quantum Computing
Homomorphic encryption
Tips for CTF Cryptography Challenges:

1. Understand the Basics:


Ensure a solid understanding of symmetric and asymmetric cryptography, hashing, and common
algorithms.
2. Code Proficiency:
Familiarize yourself with basic programming, especially in languages like Python. Many
challenges require scripting.
3. Learn from Write-ups:
Review write-ups for past challenges to understand different approaches and techniques.
4. Stay Curious:
CTF challenges often require creative thinking. Don't be afraid to experiment and try different
approaches.
5. Collaborate:
Work with others, either in-person or online. Discussing challenges with peers can lead to
valuable insights.
Terms and concepts
Encoding: to convert (something, such as a body of information) from one system
of communication into another
Cipher: an algorithm for performing encryption or decryption.
Plaintext: The unencrypted or “original” message
Ciphertext: The encrypted message
Key: a piece of information that specifies the transformation of plaintext into
ciphertext, and vice versa for decryption algorithms. Essentially, the key is part of
the input into a cryptographic function that modifies the function’s operations
while creating ciphertext in such a way that you have to have the key to get the
plaintext from a decryption function.
Terms and concepts
Symmetric Cipher: The same key is used to encrypt and decrypt the message.
For example, ROT13.
Asymmetric Cipher: Two distinct yet related keys (public and private) are used to
encrypt and decrypt the message. For example, RSA.
Hashing: Hashing is a mathematical algorithm that converts plaintext to a unique
text string or a ciphertext.
Padding: the process of adding extra bits to the plaintext before encryption to
ensure that it meets specific requirements. Padding is commonly used in block
cipher algorithms, where data is processed in fixed-size blocks.
Bits and Bytes
A bit is binary value – a value that has only two possible states. Typically binary
values are represented visually as 0 or 1, but remember that their actual value is not
a printable character. A byte on modern computers is 8 bits and represents 256
possible values. In cryptographic applications when you see something say it
requires a 128 bit key, you can calculate the number of bytes by dividing by 8. 128
divided by 8 is 16, so a 128 bit key is a 16 byte key.
In most of the crypto challenges we will be dealing with bytes through operations of
encoding and decoding
Bytes important tools
str.encode(): string -> bytes
example: print(“crypto”.encode())
output: b’crypto’
byte.decode(): bytes -> string
example: print (b’crypto’.decode())
output: crypto
bytes_to_long(): bytes string -> decimal int
long_to_bytes(): decimal int-> bytes string
bytes and hex Tools:
bytes.fromhex() : hex string -> bytes
b.hex() : bytes -> hex (b is a bytes object)
Encoding & Decoding
Encoding:
basically a form of “translation”. Different computer systems operate with different
forms of encoding like different people use different languages. Just like languages
have specific alphabets, encodings have alphabets of their own.
In most of the challenges, we will have to encode and decode between various bases
such as decimal, binary, octal ,Base 16 and 64
ASCII
ASCII is a 7-bit encoding standard which allows the representation of text using
the integers 0-127.
Main functions are ord() and chr()

ASCII challenge:
l=[99, 114, 121, 112, 116, 111, 123, 65, 83, 67, 73, 73, 95, 112, 114, 49, 110, 116, 52,
98, 108, 51, 125]
turn this into a string
Base 16 and 64
The most prominent family of bases in CTF crypto challenges
Base 16: Base 16 (hexadecimal) encoding uses the hexadecimal number system
(0123456789ABCDEF) to encode text.
Tools:
hex(): base 16 encode: decimal int -> hex string (starts with 0x)
int(x,16): decode base 16: hex string -> decimal int
Base 64: Base 64 is similar to base16, but it has an even larger alphabet and
uses padding characters (equals signs)
Tools:
remember to import base64!!
base64.b64encode : byte -> base64 byte string
base64.b64decode: string -> byte string
Practice these tools!
Encoding 1 challenge (friendly CTF) :
flag="5365637572696e6574737b484558464f5254484557494e7d"

Encoding 2 challenge (friendly CTF) :


flag="U2VjdXJpbmV0c3tCQVNFNjRJU0ZPUlNVUkVJU1NPTUVUSElOR30="

Encoding 3 challenge (friendly CTF) :


flag=695831559509425648824083007919730262424152059344495562078194
371075772558986882220904478002799741

Interactive challenge (the person next to you will be your partner):


player 1 encodes a message of their choice (transforms it into numbers) then
sends it to their partner who will decode it and send the original message back
Important presets and libraries

Always the first thing to do: pip install cryptodome (in terminal)
Crypto.Util.number:
long_to_bytes()
bytes_to_long()
get_prime()
inverse()
pwntools:
xor()
Types of Ciphers
Types of Ciphers
Examples of asymmetric ciphers: RSA,ECC
Examples of symmetric ciphers: AES,DES
Morse:
Morse code is a substitution cipher originally designed for telegrams, it’s alphabet consists of
dots, dashes and slashes.
This is some plaintext ----> - .... .. ... / .. ... / ... --- -- . / .--. .-.. .- .. -. - . -..- -
Caesar cipher:
The Caesarian Shift cipher, or Caesar cipher is a substitution method that involves rotating an
alphabet by key n and substituting the rotated letters for the plaintext letters. The best
visualization of how this works is a Caesar Cipher Wheel.
If n=11 then our alphabets are:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
LMNOPQRSTUVWXYZABCDEFGHIJK
So A=L, B=M, etc.
ROT13:
ROT13 is just a Caesar cipher with a key of 13. (Or n=13)
Useful tools
Encoding and Numeric Base Conversions:
Simply calc and rapid tables
CyberChef:
Text manipulation, processing, ciphers and encoding: Cyberchef
FeatherDuster:
Cipher identification Add a little bit of body text
dCode:
encryption/decryption goldmine: link
Practical Cryptography:
Practical Cryptography has resources for learning to break classical ciphers (as
opposed to just decrypting the message!)
XOR
XOR
2 ways to use XOR function in python:
x ^ y (x and y 2 ints or 2 booleans)
from pwn import xor: xor(byte,byte)
xor properties:
⊕ ⊕
Commutative: A B = B A
⊕ ⊕ ⊕ ⊕
Associative: A (B C) = (A B) C

Identity: A 0 = A

Self-Inverse: A A = 0
Exploiting XOR Encryption
Single Byte XOR Encryption
Single Byte XOR Encryption is trivial to bruteforce as there are only 255 key
combinations to try.
Multibyte XOR Encryption
Multibyte XOR gets exponentially harder the longer the key, but if the
encrypted text is long enough, character frequency analysis is a viable method
to find the key. Character Frequency Analysis means that we split the cipher
text into groups based on the number of characters in the key. These groups
then are bruteforced using the idea that some letters appear more frequently
in the english alphabet than others.
XOR challenges
Challenge 1:
KEY1 = a6c8b6733c9b22de7bc0253266a3867df55acde8635e19c73313
KEY2 ^ KEY1 = 37dcb292030faa90d07eec17e3b1c6d8daf94c35d4c9191a5e1e
KEY2 ^ KEY3 = c1545756687e7573db23aa1c3452a098b71a7fbf0fddddde5fc1
FLAG ^ KEY1 ^ KEY3 ^ KEY2 =
04ee9855208a2cd59091d04767ae47963170d1660df7f56f5faf
Can you find the FLAG?

Challenge 2:
I've hidden some data using XOR with a single byte, but that byte is a secret. Don't
forget to decode from hex first. Knowing that the flag format is “crypto{FLAG}” try
to decode it.

73626960647f6b206821204f21254f7d694f7624662065622127234f726927756d
XOR challenges
Challenge 3:
I've encrypted the flag with my secret key, you'll never be able to guess it.
0e0b213f26041e480b26217f27342e175d0e070a3c5b103e2526217f27342e175d0e077e26
3451150104
Knowing that the flag format is “crypto{FLAG}” try to decode it.

Challenge 4:
XOR the string “label” with the key 13 and find the flag
(We can XOR strings by first converting each character to the integer representing the
Unicode character. )

Challenge 5:
x="1c0111001f010100061a024b53535009181c"
y="686974207468652062756c6c277320657965"
xor these two hex strings to find the flag (IN TWO WAYS)

You might also like