crypto must know

crypto must know

I recently participated in a ctf competition. In Spring and Autumn, Nanyou brushed some crypto cryptography topics, and gained a lot of knowledge from it. Here is a brief summary of common cryptography knowledge!

Base encoding

Base64 is mostly used in Base encoding. First of all, let's talk about Base64 encoding.

  1. The string is grouped in bytes, each group of three bytes, each group has a total of 24 binary bits. (fill with '=' if less than 3 bytes)
  2. For the above groupings, each group is divided into 4 groups, that is, 24 bits are divided into 4 groups, and each group has 6 bits.
  3. Prepend 00 to each group to expand it to 32 binary (4 bytes)
  4. Convert it to ascii characters for each group, convert according to the following chart

base64 conversion table

code value character   code value character   code value character   code value character
0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 and
3 D 19 T 35 j 51 with
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 AND 40 O 56 4
9 J 25 WITH 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /

Uses: Generally not used for encryption, mainly to convert binary numbers into ordinary strings for network transmission, because some binary characters are control characters in the transmission protocol and cannot be directly transmitted.

And base32 is similar to base64.

base32 conversion table

RFC 4648 Base32 Alphabet
value symbol value symbol value symbol value symbol
0 A 8 I 16 Q 24 AND
1 B 9 J 17 R 25 WITH
2 C 10 K 18 S 26 2
3 D 11 L 19 T 27 3
4 E 12 M 20 U 28 4
5 F 13 N 21 V 29 5
6 G 14 O 22 W 30 6
7 H 15 P 23 X 31 7
filling =

Base32 divides any string into bytes, and concatenates the corresponding binary values ​​of each byte (less than 8 bits with high-order 0s), divides them into groups of 5 bits, and converts each group of binary values ​​into Decimal to correspond to one of the 32 printable characters.

base16 conversion table

Base16 encoding table
value coding value coding
0 0 8 8
1 1 9 9
2 2 10 A
3 3 11 B
4 4 12 C
5 5 13 D
6 6 14 E
7 7 15 F

Base16编码使用16个ASCII可打印字符(数字0-9和字母A-F)对任意字节数据进行编码。Base16先获取输入字符串每个字节的二进制值(不足8比特在高位补0),然后将其串联进来,再按照4比特一组进行切分,将每组二进制数分别转换成十进制,在下述表格中找到对应的编码串接起来就是Base16编码。可以看到8比特数据按照4比特切分刚好是两组,所以Base16不可能用到填充符号“=”。

需要注意的是Base各种编码秘文的区分。

比如base16没有F之后的字母以及没有[a-zG-Z],base32没有[a-z0-1+/],快速区分base编码有助于解码。

python3里实现base64加解密有 base64.b64decode()等各种方法。

RSA解密

关于RSA加密原理请看这里传送门:RSA算法原理(一)

以及RSA简单理解请看这里传送门:RSA算法原理(二)

这里我简单说说CTF题目中的RSA

一般RSA题目会给出n,e,c,有时候也会给出p,q(c一般就是指密)

一般计算过程是先计算phi,有了phi和e可以求出d(密钥)再根据c,d,n就可以求得明文!

具体python3实现代码(需要安装第三方库gmpy2)

import gmpy2
import binascii
n = 
p = 
q = 
c = 
e = 
phi = (p-1) * (q-1)
d = gmpy2.invert(e, phi)
n = p * q
print(pow(c, d, n))
print(binascii.a2b_hex(hex(pow(c, d, N))[2:]))

不过很多时候会只给出n而没有给出p,q这种情况要求通过n而求出q和p。这里提供一个可以分解n的网站

而且要注意e=1或者2,3这种情况,仔细看传送门:RSA算法原理(二)你会发现如果e=1,明文plain = c + n*k(k=0,1,2,3....),这种情况在题目中也遇到过,要注意!

恺撒密码

基本思想: 通过把字母移动一定的位数来实现加解密。如果移动的位数是13,加解密的算法一样。如下一个简单demo。

# -*- coding: utf-8 -*-
from string import ascii_uppercase, ascii_lowercase
__author__ = 'lateink'

cryptoMessage = 'synt{5pq1004q-86n5-46q8-o720-oro5on0417r1}'

message = ''

for i in cryptoMessage:
    if i in ascii_uppercase:
        x = (ord(i) - ord('A') + 13) % 26
        message += chr(x + ord('A'))
    elif i in ascii_lowercase:
        x = (ord(i) - ord('a') + 13) % 26
        message += chr(x + ord('a'))
    else:
        message += i

print(message)

变异恺撒

变异恺撒并不是简单把字母移动一定的位数,而是每个字母根据密码字典进行加密,这种必须要有密码字典才能解密。如果没有密码字典,如果爆破的话,数量级大概是26!但是一般情况并不会真的通过爆破去获取密码字典。一般密文是英文单词,而英文单词的话,可以通过词频分析这攻击手段破解。这里提供一个词频分析解密网站quipquip.

奇偶位变化

恺撒密码中有一种套路是,字符串的奇数位加,偶数位减(或者相反),如果解密过程中没什么头绪的时候不妨试试这种方式。

md5

md5采用hash算法加密数据,不可逆,不能从密文推算出明文。有些网站提供md5解密,大概都是使用一个比较大密码字典爆破md5密文,搜索密码字典里是否有对应的密码加密后与要解密的密文相等。感觉没啥用。

密文特征

32个或者16个16进制字符串

其他奇奇怪怪的加密

猪圈密码

{ 'a': 'j', 'b': 'k', 'c': 'l', 'd': 'm', 'e': 'n', 'f': 'o', 'g': 'p', 'h': 'q', 'i': 'r',
's': 'w', 'v': 'z', 't': 'x', 'u': 'y'
}
猪圈密码就是根据上面密文字典加解密

异或

ctf题中还有一种常见的密文解密是用到疑惑,需要明文和密文异或才能得到一串有意义的字符串。

目前遇到的CTF中所需要的加解密总结大致如此,之后遇到新的问题新的套路再更新

continue updating...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324482834&siteId=291194637