import time
import base64
import pyDes
import binascii
def DESEncrypt(desKey, target):
key = desKey[0:8]
IV =desKey[::-1][0:8]
btKey =key.encode(encoding='utf8')
btIV =IV.encode(encoding='utf8')
k = pyDes.des(btKey, mode=pyDes.CBC, IV=btIV, pad=None, padmode=pyDes.PAD_PKCS5)
en = k.encrypt(target)
return en
def GetEncryptBase64(userid,pwd):
str_time=time.strftime("%Y%m%d")
char_list = list(str_time)
int_list=list(map(lambda x:int(x,10),char_list))
deskey=sum(int_list)
deskey*=int(str_time,10)
deskeystr=str(deskey).zfill(8)
userinfostr=userid+":"+pwd
userinfoEncryptBytes=DESEncrypt(deskeystr,userinfostr.encode(encoding='utf8'))
userinfoEncryptBase64 = base64.b64encode(userinfoEncryptBytes).decode('ascii')
return userinfoEncryptBase64
result=GetEncryptBase64("userid","userpwd")
print("result={}".format(result))
'''
byte[] DESEncrypt(string desKey, byte[] target)
{
if (desKey.Length < 8) throw new Exception("parameter desKey length should equal greater than 8");
var key = desKey.Substring(0, 8);
var IV = string.Join("", desKey.Reverse()).Substring(0, 8);
byte[] btKey = Encoding.UTF8.GetBytes(key);
byte[] btIV = Encoding.UTF8.GetBytes(IV);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(target, 0, target.Length);
cs.FlushFinalBlock();
}
return ms.ToArray();
}
}
public string GetEncryptBase64(string userid, string pwd)
{
Int64 deskey = DateTime.Now.ToString("yyyyMMdd").Sum(c => Convert.ToInt32(c.ToString()));
deskey *= Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"));
string deskeystr = deskey.ToString().PadLeft(8, '0');
string userinfostr = userid + ":" + pwd;
var userinfoEncryptBytes = DESEncrypt(deskeystr, Encoding.UTF8.GetBytes(userinfostr));
var userinfoEncryptBase64 = Convert.ToBase64String(userinfoEncryptBytes);
return userinfoEncryptBase64;
}
'''