Maojui

古典密碼學 I | Classical Cipehr I

2017-03-07

Caesar Cipher

將明文做一定的推移
把每個字元往後推一格 ( ex: key = 1 ) [ z → a ]

1
2
plaintext:  defend the east wall of the castle
ciphertext: efgfoe uif fbtu xbmm pg uif dbtumf
Alias for some key in caesar
  • Avocat (A→K)[ key=10 ]
  • ROT13 (A→N) [ key=13 ]
  • Cassis (A→V) [ key=-5(21) ]
  • Cassette (A→U) [ key=-6(20) ]

Atbash Cipher [不用Key]

只是將每個字元反序 ord(‘Z’) - ord( c ) + ord(‘A’) A→Z , B→Y , C→X …. Z→A
Simple Substitution Cipher:

1
2
plain alphabet : abcdefghijklmnopqrstuvwxyz
cipher alphabet: zyxwvutsrqponmlkjihgfedcba

依照 cipher alphabet 將 每個字元做對應

1
2
plaintext : defend the east wall of the castle
ciphertext: wvuvmw gsv vzhg dzoo lu gsv xzhgov

也可以用單字作為 Key (重複的字母記得移除)
Key = ‘bear’ → ‘bearcdfghijklmnopqstuvwxyz’


Affine Cipher [ a = (1,3,5,7,9,11,15,17,19,21,23,25…) , b = 0~25 ]

key 就是 a 和 b 兩個數字的值,其中 A 的選擇必須讓 gcd(a,m) = 1的值
[ m 是字元數 ex: A-Z(26), A-Z+a-z+’ ‘(53) … ]

先把內容數值化 plaintext : brute force = [1, 17, 20, 19, 4, 5, 14, 17, 2, 4]
密文 cipher[i] = ( a * plain[i] + b )



Vigenère Cipher

在左列找到 plaintext 的字元,在上方找到 key 的字元,兩字的交叉點就是密文
Plaintext : ‘DEFEND THE EAST WALL OF THE CASTLE’
key : ‘Fortification’
cipher text : ‘ISWXVI BJE XIGG BOCE WK BJE VIGGQS’

tableau :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
---------------------------------------------------
A A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
B B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
C C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
D D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
E E F G H I J K L M N O P Q R S T U V W X Y Z A B C D
F F G H I J K L M N O P Q R S T U V W X Y Z A B C D E
G G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
H H I J K L M N O P Q R S T U V W X Y Z A B C D E F G
I I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
J J K L M N O P Q R S T U V W X Y Z A B C D E F G H I
K K L M N O P Q R S T U V W X Y Z A B C D E F G H I J
L L M N O P Q R S T U V W X Y Z A B C D E F G H I J K
M M N O P Q R S T U V W X Y Z A B C D E F G H I J K L
N N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
O O P Q R S T U V W X Y Z A B C D E F G H I J K L M N
P P Q R S T U V W X Y Z A B C D E F G H I J K L M N O
Q Q R S T U V W X Y Z A B C D E F G H I J K L M N O P
R R S T U V W X Y Z A B C D E F G H I J K L M N O P Q
S S T U V W X Y Z A B C D E F G H I J K L M N O P Q R
T T U V W X Y Z A B C D E F G H I J K L M N O P Q R S
U U V W X Y Z A B C D E F G H I J K L M N O P Q R S T
V V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
W W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
X X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
Y Y Z A B C D E F G H I J K L M N O P Q R S T U V W X
Z Z A B C D E F G H I J K L M N O P Q R S T U V W X Y

Gronsfeld Cipher

跟 Vigenère Cipher 一模一樣,只是 Key 換成數字表示
Vigenère key : ‘Neptune’
Gronsfeld key : [13, 4, 15, 19, 20, 13, 4]


Beaufort Cipher

密文的找法和 Vigenère Cipher 有些不同
在左列找到 plaintext 的字元,在該列[row]找到key的字元,往上找到對應的字元就是密文的字元

1
2
3
Plaintext  : "DEFEND THE EAST WALL OF THE CASTLE"
key : 'Fortification'
ciphertext : "CKMPVCPVWPIWUJOGIUAPVWRIWUUK"

Autokey Cipher [ Key 不重複 ]

AutoKey加密用的key = 輸入的Key + 明文plaintext(補到和plaintext一樣長)
cipher[i] = plaintext[i] + ord(AutoKey[i])
ex: key=FORTIFICATION

1
2
3
Autokey    : FORTIFICATIONDEFENDTHEEASTWA
plaintext : DEFENDTHEEASTWALLOFTHECASTLE
ciphertext : ISWXVIBJEXIGGZEQPBIMOIGAKMHE

Running Key Cipher [ Key 不重複 ]

用一句話或是書上的一段文章(要夠長)來進行 Vigenère 加密,可以做到像 AutoKey 一樣,讓密文不重複


Porta Cipher

Porta Cipher 用此加密表 : [Key中的 (A&B),(C&D) … 效果完全相同]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Keys| a b c d e f g h i j k l m n o p q r s t u v w x y z
--------------------------------------------------------
A,B | n o p q r s t u v w x y z a b c d e f g h i j k l m
C,D | o p q r s t u v w x y z n m a b c d e f g h i j k l
E,F | p q r s t u v w x y z n o l m a b c d e f g h i j k
G,H | q r s t u v w x y z n o p k l m a b c d e f g h i j
I,J | r s t u v w x y z n o p q j k l m a b c d e f g h i
K,L | s t u v w x y z n o p q r i j k l m a b c d e f g h
M,N | t u v w x y z n o p q r s h i j k l m a b c d e f g
O,P | u v w x y z n o p q r s t g h i j k l m a b c d e f
Q,R | v w x y z n o p q r s t u f g h i j k l m a b c d e
S,T | w x y z n o p q r s t u v e f g h i j k l m a b c d
U,V | x y z n o p q r s t u v w d e f g h i j k l m a b c
W,X | y z n o p q r s t u v w x c d e f g h i j k l m a b
Y,Z | z n o p q r s t u v w x y b c d e f g h i j k l m a

( key = Fortification )

1
2
3
Key        : F O R T I F  I C A  T I O N  F O R T  I F  I C A  T I O N F O
Plaintext : D E F E N D T H E E A S T W A L L O F T H E C A S T L E
Ciphertext : S Y N N J S C V R N R L A H U T U K U C V R Y R L A N Y

加密過程和解密過程一樣:從左邊找到Key的字元,在該行找到要加密 (解密) 的字元,上方的字母就是加密 (解密) 後的字元



參考資料:
Practical Cryptography