zoukankan      html  css  js  c++  java
  • python进行des加密解密,而且可以与JAVA进行互相加密解密

    import binascii
    from pyDes import des, CBC, PAD_PKCS5
    import uuid
    import time

    # pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyDes
    secret_key = '19771011'


    # http://blog.csdn.net/lihao21/article/details/78557461?locationNum=11&fps=1
    def des_encrypt(s):
    iv = secret_key
    k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
    en = k.encrypt(s, padmode=PAD_PKCS5)
    return binascii.b2a_hex(en)


    def des_descrypt(s):
    iv = secret_key
    k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
    de = k.decrypt(binascii.a2b_hex(s), padmode=PAD_PKCS5)
    return de


    def get_mac_address():
    mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
    return ":".join([mac[e:e + 2] for e in range(0, 11, 2)]).upper()


    # 获取时间戳
    def get_time_stamp():
    ct = time.time()
    local_time = time.localtime(ct)
    data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
    data_secs = (ct - int(ct)) * 1000
    time_stamp = "%s.%03d" % (data_head, data_secs)
    return time_stamp


    def trydecode(token):
    try:
    str_de = des_descrypt(token)
    return True, str(str_de, encoding="utf-8")
    except:
    return False, '无法解密'


    # 任务编号
    guid = uuid.uuid3(uuid.NAMESPACE_DNS, 'dsideal')
    # 时间戳
    timespan = get_time_stamp().replace('-', '').replace(':', '').replace(' ', '').replace('.', '')
    str_en = des_encrypt(str(guid).upper() + ' ' + get_mac_address() + ' ' + timespan + ' ')
    encodestr = str(str_en, encoding="utf-8")
    print('Token:' + encodestr)

    # 尝试解密
    b, m = trydecode(encodestr)
    if b:
    print(m)
    else:
    print('无法解密!')
  • 相关阅读:
    Wincc的使用
    三菱Ethernet工业以太网
    Wincc flexable的局势视图的组态
    Wincc flexable的数据记录的组态
    Wincc flexable的画面浏览切换组态
    CP342-5做主站的profibus-dp组态应用
    Winccflexable触摸屏的报警
    Wincc flexable的按钮组态
    《Java从入门到精通》第八章学习笔记
    Java Lab(1)控制台下的人物PK
  • 原文地址:https://www.cnblogs.com/littlehb/p/8082483.html
Copyright © 2011-2022 走看看