Introduction To Cryptography 2
Introduction To Cryptography 2
Introduction To Cryptography 2
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"
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)