• python C# DES 加密转换


    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;
            }
    '''
    
  • 相关阅读:
    ArrayList、Vector、LinkedList的区别联系?
    TFFS格式化到创建成功过程
    C语言中的far关键字
    Android编码规范05
    微信小程序开发总结
    1-2 Mobx 入门实践之TodoList(官方Demo)
    1-1、create-react-app 配置 mobx
    3-0 js基础 语言特性及性能优化
    2-9 js基础 cookie封装
    2-8 js基础 jsonp封装
  • 原文地址:https://www.cnblogs.com/DotNet1010/p/14393091.html
Copyright © 2020-2023  润新知