python实现DES加密算法的ECB,CBC,CTR模式

  • 流程图
    DES

  • f_package.py文件

# 字符变成2进制
def str_to_bin(s):
    res = []
    for c in s:
        tem = bin(ord(c)).replace('b', '')
        # 转为字符串时,后7位中,如果存在前面为0,会自动去掉,需要加回来,使之满足8位
        if len(tem) < 8:
            tem = "0" + tem
        res.append(tem)
    return ''.join(res)


# 2进制变成字符
def bin_to_str(s):
    return ''.join([chr(e) for e in [int(b, 2) for b in [s[8 * j: 8 * (j + 1)] for j in range(int(len(s) / 8))]]])


# 2机制到16进制
def bin_to_hex(s):
    return ''.join([hex(e).replace('0x', '') for e in [int(b, 2) for b in [s[4 * j: 4 * (j + 1)] for j in range(int(len(s) / 4))]]]).upper()


# 16进制到2进制
def hex_to_bin(s):
    res = []
    for c in s:
        tem = bin(int(c, 16)).replace('0b', '')
        while True:
            if len(tem) < 4:
                tem = "0" + tem
            else:
                break
        res.append(tem)
    return ''.join(res)
  • f_fun.py文件
key_table_l_1 = [49, 42, 35, 28, 21, 14, 7,
                 0, 50, 43, 36, 29, 22, 15,
                 8, 1, 51, 44, 37, 30, 23,
                 16, 9, 2, 52, 45, 38, 31]

key_table_r_1 = [55, 48, 41, 34, 27, 20, 13,
                 6, 54, 47, 40, 33, 26, 19,
                 12, 5, 53, 46, 39, 32, 25,
                 18, 11, 4, 24, 17, 10, 3]

key_table_l_2 = [13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,
                 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1]

key_table_r_2 = [12, 23, 2, 8, 18, 26, 1, 11, 22, 16, 4, 19,
                 15, 20, 10, 27, 5, 24, 17, 13, 21, 7, 0, 3]

expand_table = [31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8,
                7, 8, 9, 10, 11, 12, 11, 12, 13, 14, 15, 16,
                15, 16, 17, 18, 19, 20, 19, 20, 21, 22, 23, 24,
                23, 24, 25, 26, 27, 28, 27, 28, 29, 30, 31, 0]

s_boxes_table_1 = [14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
                   0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
                   4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
                   15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13]

s_boxes_table_2 = [15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
                   3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
                   0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
                   13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9]

s_boxes_table_3 = [10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
                   13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
                   13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
                   1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12]

s_boxes_table_4 = [7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
                   13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
                   10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
                   3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14]

s_boxes_table_5 = [2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
                   14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
                   4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
                   11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3]

s_boxes_table_6 = [12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
                   10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
                   9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
                   4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13 ]

s_boxes_table_7 = [4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
                   13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
                   1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
                   6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12]

s_boxes_table_8 = [13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
                   1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
                   7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
                   2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11]

s_boxes_table = [s_boxes_table_1, s_boxes_table_2, s_boxes_table_3, s_boxes_table_4,
                 s_boxes_table_5, s_boxes_table_6, s_boxes_table_7, s_boxes_table_8]

p_box_table = [15, 6, 19, 20, 28, 11, 27, 16, 0, 14, 22, 25, 4, 17, 30, 9,
               1, 7, 23, 13, 31, 26, 2, 8, 18, 12, 29, 5, 21, 10, 3, 24]


def f_shift(key_, step):  # key_: 56bit
    return key_[step:] + key_[:step]


def f_zero_fill(str_, num_1_, num_2_, pos_):
    if num_1_ < num_2_:
        for _ in range(num_2_ - num_1_):
            if pos_ == 'l':
                str_ = '0' + str_
            if pos_ == 'r':
                str_ = str_ + '0'
    return str_


def f_sub_keys(key_):  # key: 56bit
    key_result = []
    key_l = [key_[key_table_l_1[_]] for _ in range(28)]
    key_r = [key_[key_table_r_1[_]] for _ in range(28)]
    for i in range(16):
        if i == 0 or i == 1 or i == 8 or i == 15:
            step = 1
        else:
            step = 2
        key_l = f_shift(key_l, step)
        key_r = f_shift(key_r, step)
        key_l_ = [key_l[key_table_l_2[_]] for _ in range(24)]
        key_r_ = [key_l[key_table_r_2[_]] for _ in range(24)]
        key_result.append(key_l_ + key_r_)
    return key_result  # key_result: list_16 48bit


def f_xor(x, y):
    return [str(int(x[_]) ^ int(y[_])) for _ in range(len(x))]


def f_expand(r_):
    return [r_[expand_table[_]] for _ in range(48)]


def f_s_boxes(expand_):  # expand_ 48bit
    s_boxes_ = []
    for i in range(8):
        row = 2 * int(expand_[i * 6]) + int(expand_[i * 6 + 5])
        col = 8 * int(expand_[i * 6 + 1]) + 4 * int(expand_[i * 6 + 2]) + 2 * int(expand_[i * 6 + 3]) + int(expand_[i * 6 + 4])
        t_ = s_boxes_table[i][row * 16 + col]
        s_ = bin(s_boxes_table[i][t_])[2:]
        s_ = f_zero_fill(s_, len(s_), 4, 'l')
        s_boxes_.extend(s_)
    return s_boxes_  # 32bit


def f_p_box(s_boxes_):  # 32bit
    return [s_boxes_[p_box_table[_]] for _ in range(32)]
  • des.py文件
# ECB
import f_fun as f
import f_package as p


# 按照DES算法的流程图进行运算
def des(text, key_, flag_):  # text 2进制列表
    sub_keys = f.f_sub_keys(key_)  # 16个子密钥
    if flag_ == 1:
        sub_keys.reverse()
    L, R = text[:32], text[32:]
    for net in range(16):
        __ = R
        __ = f.f_expand(__)
        __ = f.f_xor(__, sub_keys[net])
        __ = f.f_s_boxes(__)
        __ = f.f_p_box(__)
        __ = f.f_xor(__, L)
        L, R = R, __
    return R + L


def des_input_plain():
    input_ = input("请输入明文:")
    input_ = p.str_to_bin(input_)
    n_ = len(input_) // 64
    mod = len(input_) % 64
    if mod != 0:
        n_ = n_ + 1
        input_ = f.f_zero_fill(input_, mod, 64, 'r')
    return input_, n_


def des_input_cipher():
    input_ = input("请输入16进制密文:")
    input_ = p.hex_to_bin(input_)
    n_ = len(input_) // 64
    return input_, n_


def des_output_cipher(cipher_str_):
    print("---------加密----------")
    print("2进制密文:" + cipher_str_)
    print("16进制密文:", p.bin_to_hex(cipher_str_))
    print()


def des_output_plain(plain_str_):
    print("---------解密----------")
    print("2进制明文:" + plain_str_)
    print("明文:", p.bin_to_str(plain_str_))
    print()


d_xor = f.f_xor

d_zero_fill = f.f_zero_fill
  • des_ecb.py文件
# des_ecb
import des as d
import os
if __name__ == '__main__':
    key = '01010010100110010101000100101001001101000101011001111000'

    #  ################# 加密 ##################  #
    plain, n = d.des_input_plain()
    cipher_str = ''
    for i in range(n):
        plain_text = plain[i*64: (i + 1) * 64]
        flag = 0
        cipher_str = cipher_str + ''.join(d.des(plain_text, key, flag))
    d.des_output_cipher(cipher_str)

    #  ################# 解密 ##################  #
    cipher, n = d.des_input_cipher()
    plain_str = ''
    for i in range(n):
        cipher_text = cipher[i * 64: (i + 1) * 64]
        flag = 1
        plain_str = plain_str + ''.join(d.des(cipher_text, key, flag))
    d.des_output_plain(plain_str)
    os.system("pause")
  • des_cbc.py文件
# des_cbc
import des as d
import os
if __name__ == '__main__':
    key = '01010010100110010101000100101001001101000101011001111000'
    IV = '0101110001000010100101000101110110001000100110010001111000010000'

    #  ################# 加密 ##################  #
    plain, n = d.des_input_plain()
    iv = IV
    cipher_str = ''
    for i in range(n):
        plain_text = plain[i * 64: (i + 1) * 64]
        flag = 0
        iv = d.des(d.d_xor(iv, plain_text), key, flag)
        cipher_str = cipher_str + ''.join(iv)
    d.des_output_cipher(cipher_str)

    #  ################# 解密 ##################  #
    cipher, n = d.des_input_cipher()
    iv = IV
    plain_str = ''
    for i in range(n):
        cipher_text = cipher[i * 64: (i + 1) * 64]
        flag = 1
        t_ = d.d_xor(iv, d.des(cipher_text, key, flag))
        iv = cipher_text
        plain_str = plain_str + ''.join(t_)
    d.des_output_plain(plain_str)
    os.system("pause")
  • des_ctr.py文件
# des_ctr
import des as d
import os
if __name__ == '__main__':
    key = '01010010100110010101000100101001001101000101011001111000'
    IV = '0101110001000010100101000101110110001000100110010001111000010000'

    #  ################# 加密 ##################  #
    plain, n = d.des_input_plain()
    iv = IV
    cipher_str = ''
    for i in range(n):
        plain_text = plain[i * 64: (i + 1) * 64]
        flag = 0
        _t = d.des(iv, key, flag)
        cipher_str = cipher_str + ''.join(d.d_xor(plain_text, _t))
        iv = bin(int(iv, 2) + 1)[2:]
        iv = d.d_zero_fill(iv, len(iv), 64, 'l')
    d.des_output_cipher(cipher_str)

    #  ################# 解密 ##################  #
    cipher, n = d.des_input_cipher()
    iv = IV
    plain_str = ''
    for i in range(n):
        cipher_text = cipher[i * 64: (i + 1) * 64]
        flag = 0
        _t = d.des(iv, key, flag)
        plain_str = plain_str + ''.join(d.d_xor(cipher_text, _t))
        iv = bin(int(iv, 2) + 1)[2:]
        iv = d.d_zero_fill(iv, len(iv), 64, 'l')
    d.des_output_plain(plain_str)
    os.system("pause")
  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值