zoukankan      html  css  js  c++  java
  • 加密解密

    RSA

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import rsa
    import base64
     
     
    # ######### 1. 生成公钥私钥 #########
    pub_key_obj, priv_key_obj = rsa.newkeys(256)
     
    pub_key_str = pub_key_obj.save_pkcs1()
    pub_key_code = base64.standard_b64encode(pub_key_str)
     
    priv_key_str = priv_key_obj.save_pkcs1()
    priv_key_code = base64.standard_b64encode(priv_key_str)
     
    print(pub_key_code)
    print(priv_key_code)
     
    # ######### 2. 加密 #########
    def encrypt(value):
        key_str = base64.standard_b64decode(pub_key_code)
        pk = rsa.PublicKey.load_pkcs1(key_str)
        val = rsa.encrypt(value.encode('utf-8'), pk)
        return val
     
     
    # ######### 3. 解密 #########
    def decrypt(value):
        key_str = base64.standard_b64decode(priv_key_code)
        pk = rsa.PrivateKey.load_pkcs1(key_str)
        val = rsa.decrypt(value, pk)
        return val
     
     
    # ######### 基本使用 #########
    if __name__ == '__main__':
        v = 'wupeiqi'
        v1 = encrypt(v)
        print(v1)
        v2 = decrypt(v1)
        print(v2)

    AES(高级加密)

    #3.6安装  pip3 install pycryptodome
    
    #mac     pip3 install pycrypto

    a. 事例:

    ############################### 加密 ##############################
     
        from Crypto.Cipher import AES
        def encrypt(message):
            key = b'dfdsdfsasdfdsdfs'                       #key必须是16的整数倍
            cipher = AES.new(key, AES.MODE_CBC, key)        #创建对象
            ----------------------------------------------
            #先转成字节,把数据拼够16字节的整数倍
            ba_data = bytearray(message,encoding='utf-8')   #把数据转成bytearray(byte的数组),bytearray只能追加数字,默认把数字转成字节
            v1 = len(ba_data)
            v2 = v1 % 16
            if v2 == 0:
                v3 = 16
            else:
                v3 = 16 - v2                                #v3是追加的长度
            for i in range(v3):
                ba_data.append(v3)                          #bytearray只能追加数字,默认把数字转成字节
            final_data = ba_data.decode('utf-8')
            ----------------------------------------------
            msg = cipher.encrypt(final_data)                #要加密的字符串,必须是16个字节或16个字节的倍数,加密后是byte格式
            return msg
     
    ############################### 解密 ##############################
        def decrypt(msg):
            key = b'dfdsdfsasdfdsdfs'
            cipher = AES.new(key, AES.MODE_CBC, key)
            result = cipher.decrypt(msg)                    #把加密后的字节解密成不加密的字节
            data = result[0:-result[-1]]
            return str(data,encoding='utf-8')
  • 相关阅读:
    poj2661Factstone Benchmark
    完整的微信接口类 (转)
    位运算(转载)
    PHP学习笔记之数组游标操作
    MYSQL数据库数据拆分之分库分表总结 (转)
    webservice使用
    MySQL索引类型总结和使用技巧以及注意事项 (转)
    PHP empty、isset、isnull的区别
    myisam和innodb的区别
    看看PHP迭代器的内部执行过程(转)
  • 原文地址:https://www.cnblogs.com/bingabcd/p/7271689.html
Copyright © 2011-2022 走看看