#coding=utf8 from pyDes import * import base64 class Crypt_Error(): pass """ des方法,des(处理后的秘钥,加密方式,位移向量,位移方式,填充方式) """ def _get_key(key): if not key: key="U8y0R81k" elif len(key)>8: key=key[0:8] elif len(key)<8: raise Crypt_Error('秘钥长度不足八位,请检查秘钥') return key def encrypt_data(data,key): """ 加密 :return: """ k = des(_get_key(key), ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5) d = base64.encodestring(k.encrypt(data)) return ''.join(d.split('\n')[:-1]) def decrypt_data(data,key): """ 解密 :param data: :return: """ data = base64.decodestring(data) k = des(_get_key(key), ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5) return k.decrypt(data, pad=None, padmode=PAD_PKCS5) # if __name__=='__main__': # data = "123456" # key= "e10adc3949ba59abbe56e057f20f88dd" # b=encrypt_data(data,key) # # print b # print decrypt_data(b,key)