zoukankan      html  css  js  c++  java
  • 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;
            }
    '''
    
  • 相关阅读:
    正则表达式30分钟入门教程
    21 个HTML网页转RSS Feeds的工具
    批量去除PHP文件中bom的PHP代码
    WEB网页采集技术参考
    xcache
    Sonix SN9P701 OCR点读笔二维码识别源码
    UI设计素材资源网站推荐
    解决电信DNS劫持
    自学电子技术的最佳方法
    wp资源汇总
  • 原文地址:https://www.cnblogs.com/DotNet1010/p/14393091.html
Copyright © 2011-2022 走看看