zoukankan      html  css  js  c++  java
  • pyDes介绍

      使用参数如下(拷贝自上述提供的地址):

    Class initialization
    --------------------
    pyDes.des(key, [mode], [IV], [pad], [padmode])
    pyDes.triple_des(key, [mode], [IV], [pad], [padmode])
    
    key     -> Bytes containing the encryption key. 8 bytes for DES, 16 or 24 bytes
    	   for Triple DES
    mode    -> Optional argument for encryption type, can be either
    	   pyDes.ECB (Electronic Code Book) or pyDes.CBC (Cypher Block Chaining)
    IV      -> Optional Initial Value bytes, must be supplied if using CBC mode.
    	   Length must be 8 bytes.
    pad     -> Optional argument, set the pad character (PAD_NORMAL) to use during
    	   all encrypt/decrpt operations done with this instance.
    padmode -> Optional argument, set the padding mode (PAD_NORMAL or PAD_PKCS5)
    	   to use during all encrypt/decrpt operations done with this instance.
    
    I recommend to use PAD_PKCS5 padding, as then you never need to worry about any
    padding issues, as the padding can be removed unambiguously upon decrypting
    data that was encrypted using PAD_PKCS5 padmode.
    
    Common methods
    --------------
    encrypt(data, [pad], [padmode])
    decrypt(data, [pad], [padmode])
    
    data    -> Bytes to be encrypted/decrypted
    pad     -> Optional argument. Only when using padmode of PAD_NORMAL. For
    	   encryption, adds this characters to the end of the data block when
    	   data is not a multiple of 8 bytes. For decryption, will remove the
    	   trailing characters that match this pad character from the last 8
    	   bytes of the unencrypted data block.
    padmode -> Optional argument, set the padding mode, must be one of PAD_NORMAL
    	   or PAD_PKCS5). Defaults to PAD_NORMAL

    Example
    -------

    pyDES 是一个Python的模块,用来提供 DES、Triple-DES 的加密算法。

    使用示例:

    from pyDes import *
    
    # For Python3, you'll need to use bytes, i.e.:
    #   data = b"Please encrypt my data"
    #   k = des(b"DESCRYPT", CBC, b"", pad=None, padmode=PAD_PKCS5)
    
    data = "Please encrypt my data"
    k = des("DESCRYPT", CBC, "", pad=None, padmode=PAD_PKCS5)
    d = k.encrypt(data)
    print "Encrypted: %r" % d
    print "Decrypted: %r" % k.decrypt(d)
    assert k.decrypt(d, padmode=PAD_PKCS5) == data
    
    import base64
    from pyDes import *
    
    Des_Key = "BHC#@*UM" # Key
    Des_IV = "x22x33x35x81xBCx38x5AxE7" # 自定IV向量
    
    def DesEncrypt(str):
        k = des(Des_Key, CBC, Des_IV, pad=None, padmode=PAD_PKCS5)
    
        EncryptStr = k.encrypt(str)
    
        return base64.b64encode(EncryptStr) #转base64编码返回
    

      

  • 相关阅读:
    js对象数组(JSON) 根据某个共同字段 分组
    一个 函数 用来转化esSearch 的range 条件
    关于 vuex 报错 Do not mutate vuex store state outside mutation handlers.
    android listview 重用view导致的选择混乱问题
    android SDK和ADT的更新
    Android中adb push和adb install的使用区别
    pycharm中添加扩展工具pylint
    su Authentication failure解决
    Putty以及adb网络调试
    有关android源码编译的几个问题
  • 原文地址:https://www.cnblogs.com/chjbbs/p/6692170.html
Copyright © 2011-2022 走看看