解密解密

RSA

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)
    b64_bytes = base64.b64encode(val)
    return b64_bytes.decode('utf-8')


# ######### 3. 解密 #########
def decrypt(value):
    key_str = base64.standard_b64decode(priv_key_code)
    pk = rsa.PrivateKey.load_pkcs1(key_str)

    b64_bytes = base64.b64decode(value)
    val = rsa.decrypt(b64_bytes, pk)
    return val


# ######### 基本使用 #########
if __name__ == '__main__':
    v = 'wupeiqi'
    v1 = encrypt(v)
    print(v1)

    v2 = decrypt(v1)
    print(v2)
View Code

 

 

 

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)
# 1. 生成公钥私钥
import rsa
import base64

pub_key_obj, priv_key_obj = rsa.newkeys(1024)
pub_key_str = pub_key_obj.save_pkcs1()
priv_key_str = priv_key_obj.save_pkcs1()

pub_key_code = base64.standard_b64encode(pub_key_str)
priv_key_code = base64.standard_b64encode(priv_key_str)

"""
RSA算法本身要求加密内容也就是明文长度m必须0<m<密钥长度n
由于key是1024,加密内容长度则是128字节,再减去11个padding字节,则:117字节
"""

# 2. 加密
data = b'u' * 11000
key_str = base64.standard_b64decode(pub_key_code)
pk = rsa.PublicKey.load_pkcs1(key_str)
result = []
for i in range(0, len(data), 117):
    val = rsa.encrypt(data[i:i + 117], pk)
    result.append(val)
    print(len(val))
xxxxx = b''.join(result)
print(xxxxx)

# 3. 解密
key_str = base64.standard_b64decode(priv_key_code)
pk = rsa.PrivateKey.load_pkcs1(key_str)
result = []
for i in range(0, len(xxxxx), 128):
    val = rsa.decrypt(xxxxx[i:i + 128], pk)
    result.append(val)
print(result)
大文本加密

 

AES

from Crypto.Cipher import AES


def encrypt(message):
    key = b'dfdsdfsasdfdsdfs'
    cipher = AES.new(key, AES.MODE_CBC, key)
    bmessage = bytearray(message, encoding='utf-8')
    v1 = len(bytes(message, encoding='utf-8'))
    v2 = v1 % 16
    v3 = 16 - v2
    for i in range(v3):
        bmessage.append(32)
    data = bmessage.decode('utf-8')
    msg = cipher.encrypt(data)
    return msg


def decrypt(message):
    key = b'dfdsdfsasdfdsdfs'
    cipher = AES.new(key, AES.MODE_CBC, key)
    result = cipher.decrypt(message)
    return result.decode('utf-8').strip()

v = '我是你爸'
print(len(v))
data = encrypt(v)
print(data)
result = decrypt(data)
print(len(result),result)

  

 

 

 

 

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
import binascii

def aes_encrypt(data_string):
    key_string = "4E2918885FD98109869D14E0231A0BF4"
    key = binascii.a2b_hex(key_string)

    iv_string = "16B17E519DDD0CE5B79D7A63A4DD801C"
    iv = binascii.a2b_hex(iv_string)

    aes = AES.new(
        key=key,
        mode=AES.MODE_CBC,
        iv=iv
    )
    raw = pad(data_string.encode('utf-8'), 16)
    aes_bytes = aes.encrypt(raw)
    return binascii.b2a_hex(aes_bytes).decode().upper()

def aes_decrypt(b):
    key_string = "4E2918885FD98109869D14E0231A0BF4"
    key = binascii.a2b_hex(key_string)

    iv_string = "16B17E519DDD0CE5B79D7A63A4DD801C"
    iv = binascii.a2b_hex(iv_string)

    aes = AES.new(
        key=key,
        mode=AES.MODE_CBC,
        iv=iv
    )
    # raw = pad(data_string.encode('utf-8'), 16)
    aes_bytes = aes.decrypt(b)
    print(aes_bytes.decode('utf-8'))


data = "|878975262|d000035rirv|1631615607|mg3c3b04ba|1.3.5|ktjwlm89_to920weqpg|4330701|https://w.yangshipin.cn/|mozilla/5.0 (macintosh; ||Mozilla|Netscape|MacIntel|"
result = aes_encrypt(data)
res = binascii.a2b_hex(result.encode('utf-8'))
print(res)
aes_decrypt(res)

 

 

posted @ 2017-04-22 07:19  武沛齐  阅读(1139)  评论(0编辑  收藏  举报