Skip to content

Commit

Permalink
fix: rsa
Browse files Browse the repository at this point in the history
  • Loading branch information
TRHX committed Sep 28, 2024
1 parent 5cc5626 commit 41d8b4c
Showing 1 changed file with 84 additions and 2 deletions.
86 changes: 84 additions & 2 deletions docs/03.辅助查询/04.加密算法/07.rsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,9 @@ PKCS#1、PKCS#5、PKCS#7、PKCS#8 到底是什么?:[https://blog.csdn.net/we
```javascript
// ✅ 安装依赖:npm install jsencrypt

// ❗ 如果在 Node 环境中使用,会遇到报错 window is not defined,需要在 \node_modules\jsencrypt\bin\jsencrypt.js 添加代码:window = global;
// ❗ 如果在 Node 环境中使用,会遇到报错 window is not defined,处理方法:
// 1.在当前 JS 文件顶部添加代码:window = global;
// 2.在 \node_modules\jsencrypt\bin\jsencrypt.js 添加代码:window = global;
// ❌ 不支持对私钥进行签名、加密
// ❌ 密钥输出格式不支持 der、jwk、openssh,仅支持 pem
// ❌ 公钥语法标准不支持 pkcs1,仅支持 spki
Expand Down Expand Up @@ -1801,6 +1803,84 @@ OAEP (Optimal asymmetric encryption padding):[https://en.wikipedia.org/wiki/Op
print("RSA_PKCS1_PADDING 解密结果: ", resultDecrypted.decode())
```
</code-block>

<code-block title="加密解密(手动分组支持超长加密) 4️⃣">
```python
# ✅ 安装依赖 pip install pycryptodome,官方文档:https://pycryptodome.readthedocs.io/

# ❌ padding 填充方式只支持 RSA_PKCS1_PADDING、RSA_PKCS1_OAEP_PADDING,不支持 RSA_NO_PADDING、RSA_PKCS1_PSS_PADDING
# 本例由雕虫小技二群粉丝提供,ID:曹晶 Eric Cao🇨🇳


import base64
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 # 即 RSA_PKCS1_PADDING
from Crypto.Cipher import PKCS1_OAEP # 即 RSA_PKCS1_OAEP_PADDING


# 一次 RSA 加密对明文长度是有限制,实际上,RSA 算法本身要求加密内容也就是明文长度 M 必须 0 < M < N(模数)
# 如果要加密一个长度大于模数 N 的明文块,就需要对明文进行分块以满足要求
# RSA_PKCS1_PADDING,每次加密的明文必须比 RSA 密钥的模长至少少 11 个字节
# RSA_PKCS1_OAEP_PADDING,每次加密的明文必须比 RSA 密钥的模长至少少 42 个字节
# NO_PADDING,每次加密的明文与 RSA 的密钥的模长一样,或更短


def rsa_encrypt(plaintext):
plaintext = plaintext.encode('utf-8')
chunk_size = public_key.size_in_bytes() - 11 # 设置每个小段的长度,加密时要减去一个长度,PKCS1_PADDING: 至少-11, PKCS1_OAEP_PADDING: 至少-42
chunks = [plaintext[i:i + chunk_size] for i in range(0, len(plaintext), chunk_size)]
encrypted_chunks = [PKCS1_v1_5.new(public_key).encrypt(chunk) for chunk in chunks]
encrypted_data = b''.join(encrypted_chunks)
return encrypted_data


def rsa_decrypt(ciphertext):
chunk_size = private_key.size_in_bytes() # 设置每个小段的长度
chunks = [ciphertext[i:i + chunk_size] for i in range(0, len(ciphertext), chunk_size)]
decrypted_chunks = [PKCS1_v1_5.new(private_key).decrypt(chunk, None) for chunk in chunks]
decrypted_data = b''.join(decrypted_chunks)
return decrypted_data.decode('utf-8')


# 注意 """ 之后要紧跟着密钥标头,如果换行可能会因为无法解析导致报错:ValueError: RSA key format is not supported
# pkcs1 标准 pem 格式的公钥
public_key_pkcs1_pem = """-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBALLA/aUElSxqvPUnlC4A8UaNh6txyrjlLshW4/wUgWZIBBbvp7yVj4Oi
C5mbbbyvOqElQNhkaeV8EF2rZqy9m5whs+jIx1tUdyJPJXLyZCNr3Q8t4sQyek6q
1+imN6KUEIHrLveuYq1Lvmw3lt52vRzvWwucYV0EHSbx3G0b6hErAgMBAAE=
-----END RSA PUBLIC KEY-----
"""

# pkcs1 标准 pem 格式的私钥
private_key_pkcs1_pem = """-----BEGIN RSA PRIVATE KEY-----
MIICYAIBAAKBgQCywP2lBJUsarz1J5QuAPFGjYerccq45S7IVuP8FIFmSAQW76e8
lY+DoguZm228rzqhJUDYZGnlfBBdq2asvZucIbPoyMdbVHciTyVy8mQja90PLeLE
MnpOqtfopjeilBCB6y73rmKtS75sN5bedr0c71sLnGFdBB0m8dxtG+oRKwIDAQAB
AoGADOrlaYdrQEp2XUD+9FQdG3we0cTnYXUubEvbLdxNfZa2tPGcrYQhWuHa9L7G
r1v6YKrQhNK81Fii1iIMvBnzkVIakhSYvC294yyEZG+fYJcXuZWzoF0+YUrlFP0b
vG5JOpDcUN4YG+tG924AbhyPL0AJkX0BEElSWaFhVtnTxAECRQC4fMLIzE2hCq9F
UmR59YDSHasPUVDWuq0eXG+P/0Kp9x5ZUDjZcojcBhpsJBQbx9P4ygyiYj4uUw3O
SA8X/fZHxqmg/QI9APgLR5Uz5Veaj1PguhkoKB3384PnrrKoFD+wgc6H7Qib9aKG
KT0x5dKswZefyE/PF+Qi7hMWiO18FK2HRwJEb/eQREvyhV21uE3kGzzL3ToSWq6Q
cHJFpVqWfv9+Fyea7LvKdPaVZ2vw11ciOSYTWThPaLJVaMoRY/PJ7Va+BRvmUYUC
PQCO06fnYReZRxVq3gcqvzXCc+kTtg+hLilitxc3vosdnvhlGk2awCWvFuzuiwID
+qKuKfLeqOY1oXIirbcCRGtG4PspKLAQMO6jI7bFUc5if9UKo6TGeu39E3qtv4jW
NW6qObPBJ9R1gurMqPUT9g630xjZdZxz6ac9Z7FfgjpMV0Fe
-----END RSA PRIVATE KEY-----
"""

public_key = RSA.import_key(public_key_pkcs1_pem)
private_key = RSA.import_key(private_key_pkcs1_pem)

data = "SpiderAPI - 虫术 - 汇总各种爬虫逆向常用 API,涉及各种网络请求库,自动化框架,爬虫框架,HOOK 脚本,ADB 命令等。SpiderAPI - Insect Techniques - Summarize various commonly used reverse APIs for web crawlers, involving various network request libraries, automation frameworks, web crawling frameworks, HOOK scripts, ADB commands, etc."
resultEncrypted = rsa_encrypt(data)
resultDecrypted = rsa_decrypt(resultEncrypted)

print("原文长度: ", len(data))
print("RSA_PKCS1_OAEP_PADDING 加密结果: %s" % base64.b64encode(resultEncrypted).decode())
print("RSA_PKCS1_OAEP_PADDING 解密结果: %s" % resultDecrypted)
```
</code-block>
</code-group>

### JavaScript <Badge text="Node.js"/> <Badge text="ECMAScript 5.1+"/>
Expand Down Expand Up @@ -1936,7 +2016,9 @@ OAEP (Optimal asymmetric encryption padding):[https://en.wikipedia.org/wiki/Op
```javascript
// ✅ 安装依赖:npm install jsencrypt,官方文档:https://github.com/travist/jsencrypt

// ❗ 如果在 Node 环境中使用,会遇到报错 window is not defined,需要在 \node_modules\jsencrypt\bin\jsencrypt.js 添加代码:window = global;
// ❗ 如果在 Node 环境中使用,会遇到报错 window is not defined,处理方法:
// 1.在当前 JS 文件顶部添加代码:window = global;
// 2.在 \node_modules\jsencrypt\bin\jsencrypt.js 添加代码:window = global;
// ❌ padding 填充方式默认为 RSA_PKCS1_PADDING,不支持其他填充方式


Expand Down

0 comments on commit 41d8b4c

Please sign in to comment.