python進行des加密解密,而且可以與JAVA進行互相加密解密

來源:互聯網
上載者:User

標籤:print   secret   解密   無法   idea   import   article   hex   ace   

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(‘無法解密!‘)

python進行des加密解密,而且可以與JAVA進行互相加密解密

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.