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
相關文章
- C均值聚類 C實現 Python實現聚類Python
- switch的python實現Python
- Python實現建立字典Python
- Python 字典實現原理Python
- python實現快速排序Python排序
- 字母排列(python實現)Python
- python 實現有序字典Python
- Python yield與實現Python
- Python實現火柴人的設計與實現Python
- 用 Python 實現 Python 直譯器Python
- Go 實現的 python collectionsGoPython
- Python實現24點遊戲Python遊戲
- Python底層實現KNNPythonKNN
- Python實現氣泡排序Python排序
- 投擲骰子 -Python實現Python
- python實現密碼破解Python密碼
- 選擇排序(python)實現排序Python
- python實現貪吃蛇Python
- Python實現的快速排序Python排序
- python set和get實現Python
- python中svm方法實現Python
- python實現千圖成像Python
- 基於Python實現MapReducePython
- Python Turtle實現彈球Python
- python 介面實現類的Python
- 【Python】Python實現解壓rar檔案Python
- Python實現微博輿情分析的設計與實現Python
- 【數值方法-Python實現】Crout分解+追趕法實現Python
- 譜聚類的python實現聚類Python
- FM演算法python實現演算法Python
- Python + Wxpy 實現微信防撤回。Python
- python實現冒泡演算法Python演算法
- python能實現並行嗎Python並行
- python實現查詢糾錯Python
- python OpenCV加法操作的實現PythonOpenCV
- Python中迭代器的實現Python
- Python之禪-import this的實現PythonImport
- Python課程程式碼實現Python