关于加密我们常常会听到rsa 和aes ,
其实就是非对称加密和对称加密。对称加密秘钥一定,非对称加密存在公钥和私钥。
我在对账号系统下的接口进行接口测试的时候,遇到他们将账号密码等参数进行rsa加密了,在fidder抓包中 raw里面可以看到一个加密的字符串。
复制代码
下面给出两种测试方法:
(1)方法:
找开发要到加密的demo,例如加密的方法函数等等。拿到了这些东西,就可以利用java
中的spring boot框架,自己搞个加密解密接口。然后自己来调加密解密接口,得到加密内容和解密内容。
复制代码
(2)方法
自己通过python或者其他语言,自己手写一个加密解密函数。
1.先新建一个config.py 来放入我们的公钥或者私钥
复制代码
2.加密函数
复制代码
# -*- coding:utf-8-*-
import base64
import json
from config import *
import requests
from Crypto.Hash import MD5
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA
def get_encrypt_data(params):
"""分段加密"""
params = json.dumps(params)
params = params.encode("utf-8")
length = len(params)
print(f"加密内容为{params},加密的长度为:{length}")
default_length = 117
if length < default_length:
print(base64.b64encode(encrypt_data(params)))
return base64.b64encode(encrypt_data(params))
offset = 0
params_lst = []
while length - offset > 0:
if length - offset > default_length:
params_lst.append(encrypt_data(params[offset:offset + default_length]))
else:
params_lst.append(encrypt_data(params[offset:]))
offset += default_length
new_str = params_lst[0] + params_lst[1]
return base64.b64encode(new_str).decode("utf-8")
def encrypt_data(params):
"""使用公钥对数据加密"""
# 测试环境
key = f"""-----BEGIN RSA PUBLIC KEY-----\n{public_key}
\n-----END RSA PUBLIC KEY-----\n"""
# 海外
key = f"""-----BEGIN RSA PUBLIC KEY-----\n{public}
\n-----END RSA PUBLIC KEY-----\n"""
rakes = RSA.importKey(key)
cipher = Cipher_pkcs1_v1_5.new(rakes)
text = cipher.encrypt(params)
return text
复制代码
写好之后 ,自己调这个加密函数就可以请求测试环境的接口了。。至此结束
ps 当遇到那种,rsa加密时,没有偏移量的时候,即是每次加密的结果都是一样的。 python中nopadding 模式
no_padding.py
```
# -*- coding: utf-8 -*-
import base64
import requests
import rsa
from Crypto.PublicKey import RSA
from config import *
class RsaNodding:
def __init__(self):
key = f"""-----BEGIN RSA PUBLIC KEY-----\n{public}
\n-----END RSA PUBLIC KEY-----\n"""
self.pubkey = RSA.importKey(key)
def padding(self, s):
b = bytes(s.encode())
for i in range(128 - len(b)):
b = b'\x00'+b
return b
def encrypt(self, message):
kLen = rsa.common.byte_size(self.pubkey.n)
msg = self.padding(message)
_b = rsa.transform.bytes2int(msg)
_i = rsa.core.encrypt_int(_b, self.pubkey.e, self.pubkey.n)
result = rsa.transform.int2bytes(_i, kLen)
result = (base64.b16decode(result.hex().upper()))
print(base64.b64encode(result))
return base64.b64encode(result)
复制代码
**关于aes加密**
复制代码
class AESCipher:
def __init__(self, key):
# self.key = key.decode("base64")
self.key = key
print key
def encrypt(self, raw, iv='1234567890123456'):
"""
Returns hex encoded encrypted value! MODE_ECB
"""
BS = 16
# pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) # 对要加密的内容按16位进行补位
# iv = Random.new().read(AES.block_size)
iv = iv
# counter = Counter.new(128, initial_value=bytes_to_long(iv))
ctr_e = Crypto.Util.Counter.new(128, initial_value=long(iv.encode('hex'), 16))
# cipher = AES.new(self.key, AES.MODE_ECB, iv)
cipher = AES.new(self.key, AES.MODE_CTR, counter=ctr_e)
# raw = pad(raw)
return base64.b64encode(str(cipher.encrypt(raw)))
# return str(cipher.encrypt(raw)).encode("base64").strip()
def decrypt(self, enc, iv='1234567890123456'):
"""
Requires hex encoded param to decrypt
"""
# enc = urllib.unquote(enc).decode('utf-8') # 特殊字符(+ = ..)转换一下
enc = enc.decode("base64")
# iv = enc[:16]
iv = iv
# cipher = AES.new(self.key, AES.MODE_ECB, iv)
ctr_e = Crypto.Util.Counter.new(128, initial_value=long(iv.encode('hex'), 16))
# ctr_e = Crypto.Util.Counter.new(128, initial_value=0)
cipher = AES.new(self.key, AES.MODE_CTR, counter=ctr_e)
dec_str = cipher.decrypt(enc).strip('\x10')
return dec_str
复制代码
复制代码