python實現Simplified_DES (S-DES)
https://codereview.stackexchange.com/questions/108057/simplified-des-encryption
1. 原始碼修正後:
FIXED_IP = [2, 6, 3, 1, 4, 8, 5, 7]
FIXED_EP = [4, 1, 2, 3, 2, 3, 4, 1]
FIXED_IP_INVERSE = [4, 1, 3, 5, 7, 2, 8, 6]
FIXED_P10 = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6]
FIXED_P8 = [6, 3, 7, 4, 8, 5, 10, 9]
FIXED_P4 = [2, 4, 3, 1]
S0 = [[1, 0, 3, 2],
[3, 2, 1, 0],
[0, 2, 1, 3],
[3, 1, 3, 2]]
S1 = [[0, 1, 2, 3],
[2, 0, 1, 3],
[3, 0, 1, 0],
[2, 1, 0, 3]]
KEY = ''
def permutate(original, fixed_key):
new = ''
for i in fixed_key:
new += original[i - 1]
return new
def left_half(bits):
return bits[:int(len(bits)/2)]
def right_half(bits):
return bits[int(len(bits)/2):]
def shift(bits):
rotated_left_half = left_half(bits)[1:] + left_half(bits)[0]
rotated_right_half = right_half(bits)[1:] + right_half(bits)[0]
return rotated_left_half + rotated_right_half
def key1():
return permutate(shift(permutate(KEY, FIXED_P10)), FIXED_P8)
def key2():
return permutate(shift(shift(shift(permutate(KEY, FIXED_P10)))), FIXED_P8)
def xor(bits, key):
new = ''
for bit, key_bit in zip(bits, key):
new += str(((int(bit) + int(key_bit)) % 2))
return new
def lookup_in_sbox(bits, sbox):
row = int(bits[0] + bits[3], 2)
col = int(bits[1] + bits[2], 2)
return '{0:02b}'.format(sbox[row][col])
def f_k(bits, key):
L = left_half(bits)
R = right_half(bits)
bits = permutate(R, FIXED_EP)
bits = xor(bits, key)
bits = lookup_in_sbox(left_half(bits), S0) + lookup_in_sbox(right_half(bits), S1)
bits = permutate(bits, FIXED_P4)
return xor(bits, L)
def encrypt(plain_text):
bits = permutate(plain_text, FIXED_IP)
temp = f_k(bits, key1())
bits = right_half(bits) + temp
bits = f_k(bits, key2())
print('The encryption result is = ' + permutate(bits + temp, FIXED_IP_INVERSE))
def decrypt(cipher_text):
bits = permutate(cipher_text, FIXED_IP)
temp = f_k(bits, key2())
bits = right_half(bits) + temp
bits = f_k(bits, key1())
print('The decryption result is = ' + permutate(bits + temp, FIXED_IP_INVERSE))
if __name__ == '__main__':
oper = input('Enter D for encryption and E for decryption:')
KEY = input('Enter the 10-digit key:')
if oper == 'D':
msg = input('Enter 8-digit plaintext:')
encrypt(msg)
elif oper == 'E':
msg = input('Enter 8-digit ciphertext:')
decrypt(msg)
input('--- Type enter to end the process ---')
關於S-DES
相關文章
- switch的python實現Python
- 字母排列(python實現)Python
- Python 字典實現原理Python
- Python實現建立字典Python
- C均值聚類 C實現 Python實現聚類Python
- Python實現火柴人的設計與實現Python
- 【Python】Python實現解壓rar檔案Python
- Spectral Matting的python實現Python
- 投擲骰子 -Python實現Python
- Python實現氣泡排序Python排序
- 基於Python實現MapReducePython
- Go 實現的 python collectionsGoPython
- python實現密碼破解Python密碼
- 選擇排序(python)實現排序Python
- Python Turtle實現彈球Python
- python 介面實現類的Python
- Python實現的快速排序Python排序
- python實現貪吃蛇Python
- Python底層實現KNNPythonKNN
- Python教程:Python如何實現窮舉搜尋?Python
- 【數值方法-Python實現】Crout分解+追趕法實現Python
- FM演算法python實現演算法Python
- python實現冒泡演算法Python演算法
- Python實現單連結串列Python
- Python之禪-import this的實現PythonImport
- Python實現MySQL連線池PythonMySql
- Python課程程式碼實現Python
- Python 3.6:多型的實現Python多型
- python實現傳送郵件Python
- Python中實現單例模式Python單例模式
- 譜聚類的python實現聚類Python
- indexOf原理,Java,javascript,python實現IndexJavaScriptPython
- python 實現課堂練習Python
- python 字串replace方法實現探索Python字串
- Python使用multiprocessing實現多程序Python
- Python實現24點遊戲Python遊戲
- python能實現並行嗎Python並行
- python實現查詢糾錯Python