p115cipher 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- LICENSE +21 -0
- p115cipher/__init__.py +8 -0
- p115cipher/common.py +122 -0
- p115cipher/const.py +51 -0
- p115cipher/fast.py +74 -0
- p115cipher/normal.py +120 -0
- p115cipher/py.typed +0 -0
- p115cipher-0.0.1.dist-info/LICENSE +21 -0
- p115cipher-0.0.1.dist-info/METADATA +47 -0
- p115cipher-0.0.1.dist-info/RECORD +11 -0
- p115cipher-0.0.1.dist-info/WHEEL +4 -0
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
p115cipher/__init__.py
ADDED
p115cipher/common.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
+
__all__ = [
|
|
6
|
+
"Buffer", "RSA_encrypt", "from_bytes", "to_bytes",
|
|
7
|
+
"bytes_xor", "bytes_xor_reverse", "xor", "gen_key",
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
from functools import partial
|
|
11
|
+
from typing import Final
|
|
12
|
+
|
|
13
|
+
from Crypto.Cipher import PKCS1_v1_5
|
|
14
|
+
from Crypto.PublicKey import RSA
|
|
15
|
+
from ecdsa import ECDH, NIST224p, SigningKey # type: ignore
|
|
16
|
+
from iterutils import acc_step
|
|
17
|
+
|
|
18
|
+
from .const import G_kts, ECDH_REMOTE_PUBKEY, NIST224P_BASELEN, RSA_PUBLIC_KEY
|
|
19
|
+
|
|
20
|
+
try:
|
|
21
|
+
from collections.abc import Buffer # type: ignore
|
|
22
|
+
except ImportError:
|
|
23
|
+
from abc import ABC, abstractmethod
|
|
24
|
+
from array import array
|
|
25
|
+
|
|
26
|
+
def _check_methods(C, *methods):
|
|
27
|
+
mro = C.__mro__
|
|
28
|
+
for method in methods:
|
|
29
|
+
for B in mro:
|
|
30
|
+
if method in B.__dict__:
|
|
31
|
+
if B.__dict__[method] is None:
|
|
32
|
+
return NotImplemented
|
|
33
|
+
break
|
|
34
|
+
else:
|
|
35
|
+
return NotImplemented
|
|
36
|
+
return True
|
|
37
|
+
|
|
38
|
+
class Buffer(ABC): # type: ignore
|
|
39
|
+
__slots__ = ()
|
|
40
|
+
|
|
41
|
+
@abstractmethod
|
|
42
|
+
def __buffer__(self, flags: int, /) -> memoryview:
|
|
43
|
+
raise NotImplementedError
|
|
44
|
+
|
|
45
|
+
@classmethod
|
|
46
|
+
def __subclasshook__(cls, C):
|
|
47
|
+
if cls is Buffer:
|
|
48
|
+
return _check_methods(C, "__buffer__")
|
|
49
|
+
return NotImplemented
|
|
50
|
+
|
|
51
|
+
Buffer.register(bytes)
|
|
52
|
+
Buffer.register(bytearray)
|
|
53
|
+
Buffer.register(memoryview)
|
|
54
|
+
Buffer.register(array)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
RSA_encrypt: Final = PKCS1_v1_5.new(RSA_PUBLIC_KEY).encrypt
|
|
58
|
+
to_bytes = partial(int.to_bytes, byteorder="big", signed=False)
|
|
59
|
+
from_bytes = partial(int.from_bytes, byteorder="big", signed=False)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def bytes_xor(v1: Buffer, v2: Buffer, /, size: int = 0) -> Buffer:
|
|
63
|
+
if size:
|
|
64
|
+
v1 = v1[:size]
|
|
65
|
+
v2 = v2[:size]
|
|
66
|
+
else:
|
|
67
|
+
size = len(v1)
|
|
68
|
+
return to_bytes(from_bytes(v1) ^ from_bytes(v2), size)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def bytes_xor_reverse(v1: Buffer, v2: Buffer, /, size: int = 0) -> Buffer:
|
|
72
|
+
if size:
|
|
73
|
+
v1 = v1[:size]
|
|
74
|
+
v2 = v2[:size]
|
|
75
|
+
else:
|
|
76
|
+
size = len(v1)
|
|
77
|
+
return to_bytes(from_bytes(v1) ^ from_bytes(v2), size, "little")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def xor(src: Buffer, key: Buffer, /) -> bytearray:
|
|
81
|
+
src = memoryview(src)
|
|
82
|
+
key = memoryview(key)
|
|
83
|
+
secret = bytearray()
|
|
84
|
+
if i := len(src) & 0b11:
|
|
85
|
+
secret += bytes_xor(src, key, i)
|
|
86
|
+
for i, j, s in acc_step(i, len(src), len(key)):
|
|
87
|
+
secret += bytes_xor(src[i:j], key[:s])
|
|
88
|
+
return secret
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def gen_key(
|
|
92
|
+
rand_key: Buffer,
|
|
93
|
+
sk_len: int = 4,
|
|
94
|
+
/,
|
|
95
|
+
) -> bytearray:
|
|
96
|
+
xor_key = bytearray()
|
|
97
|
+
if rand_key and sk_len > 0:
|
|
98
|
+
length = sk_len * (sk_len - 1)
|
|
99
|
+
index = 0
|
|
100
|
+
for i in range(sk_len):
|
|
101
|
+
x = (rand_key[i] + G_kts[index]) & 0xff
|
|
102
|
+
xor_key.append(G_kts[length] ^ x)
|
|
103
|
+
length -= sk_len
|
|
104
|
+
index += sk_len
|
|
105
|
+
return xor_key
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def generate_ecdh_pair() -> tuple[bytes, bytes]:
|
|
109
|
+
sk = SigningKey.generate(NIST224p)
|
|
110
|
+
pk = sk.verifying_key
|
|
111
|
+
ecdh = ECDH(NIST224p)
|
|
112
|
+
ecdh.load_private_key(sk)
|
|
113
|
+
ecdh.load_received_public_key_bytes(ECDH_REMOTE_PUBKEY)
|
|
114
|
+
public = pk.pubkey.point.to_bytes()
|
|
115
|
+
x, y = public[:NIST224P_BASELEN], public[NIST224P_BASELEN:]
|
|
116
|
+
pub_key = bytes((NIST224P_BASELEN + 1, 0x02 + (from_bytes(y) & 1))) + x
|
|
117
|
+
# NOTE: Roughly equivalent to
|
|
118
|
+
# n = int((ecdh.public_key.pubkey.point * from_bytes(sk.to_string())).x())
|
|
119
|
+
# secret = to_bytes(n, (n.bit_length() + 0b111) >> 3)
|
|
120
|
+
secret = ecdh.generate_sharedsecret_bytes()
|
|
121
|
+
return pub_key, secret
|
|
122
|
+
|
p115cipher/const.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
+
|
|
6
|
+
from typing import Final
|
|
7
|
+
|
|
8
|
+
from Crypto.PublicKey import RSA
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
CRC_SALT: Final = b"^j>WD3Kr?J2gLFjD4W2y@"
|
|
12
|
+
MD5_SALT: Final = b"Qclm8MGWUv59TnrR0XPg"
|
|
13
|
+
ECDH_REMOTE_PUBKEY: Final = bytes((
|
|
14
|
+
0x57, 0xA2, 0x92, 0x57, 0xCD, 0x23, 0x20, 0xE5, 0xD6, 0xD1, 0x43, 0x32, 0x2F, 0xA4, 0xBB, 0x8A,
|
|
15
|
+
0x3C, 0xF9, 0xD3, 0xCC, 0x62, 0x3E, 0xF5, 0xED, 0xAC, 0x62, 0xB7, 0x67, 0x8A, 0x89, 0xC9, 0x1A,
|
|
16
|
+
0x83, 0xBA, 0x80, 0x0D, 0x61, 0x29, 0xF5, 0x22, 0xD0, 0x34, 0xC8, 0x95, 0xDD, 0x24, 0x65, 0x24,
|
|
17
|
+
0x3A, 0xDD, 0xC2, 0x50, 0x95, 0x3B, 0xEE, 0xBA,
|
|
18
|
+
))
|
|
19
|
+
G_key_l: Final = bytes((
|
|
20
|
+
0x78, 0x06, 0xad, 0x4c, 0x33, 0x86, 0x5d, 0x18, 0x4c, 0x01, 0x3f, 0x46,
|
|
21
|
+
))
|
|
22
|
+
G_key_s: Final = bytes((0x29, 0x23, 0x21, 0x5e))
|
|
23
|
+
G_kts: Final = bytes((
|
|
24
|
+
0xf0, 0xe5, 0x69, 0xae, 0xbf, 0xdc, 0xbf, 0x8a, 0x1a, 0x45, 0xe8, 0xbe, 0x7d, 0xa6, 0x73, 0xb8,
|
|
25
|
+
0xde, 0x8f, 0xe7, 0xc4, 0x45, 0xda, 0x86, 0xc4, 0x9b, 0x64, 0x8b, 0x14, 0x6a, 0xb4, 0xf1, 0xaa,
|
|
26
|
+
0x38, 0x01, 0x35, 0x9e, 0x26, 0x69, 0x2c, 0x86, 0x00, 0x6b, 0x4f, 0xa5, 0x36, 0x34, 0x62, 0xa6,
|
|
27
|
+
0x2a, 0x96, 0x68, 0x18, 0xf2, 0x4a, 0xfd, 0xbd, 0x6b, 0x97, 0x8f, 0x4d, 0x8f, 0x89, 0x13, 0xb7,
|
|
28
|
+
0x6c, 0x8e, 0x93, 0xed, 0x0e, 0x0d, 0x48, 0x3e, 0xd7, 0x2f, 0x88, 0xd8, 0xfe, 0xfe, 0x7e, 0x86,
|
|
29
|
+
0x50, 0x95, 0x4f, 0xd1, 0xeb, 0x83, 0x26, 0x34, 0xdb, 0x66, 0x7b, 0x9c, 0x7e, 0x9d, 0x7a, 0x81,
|
|
30
|
+
0x32, 0xea, 0xb6, 0x33, 0xde, 0x3a, 0xa9, 0x59, 0x34, 0x66, 0x3b, 0xaa, 0xba, 0x81, 0x60, 0x48,
|
|
31
|
+
0xb9, 0xd5, 0x81, 0x9c, 0xf8, 0x6c, 0x84, 0x77, 0xff, 0x54, 0x78, 0x26, 0x5f, 0xbe, 0xe8, 0x1e,
|
|
32
|
+
0x36, 0x9f, 0x34, 0x80, 0x5c, 0x45, 0x2c, 0x9b, 0x76, 0xd5, 0x1b, 0x8f, 0xcc, 0xc3, 0xb8, 0xf5,
|
|
33
|
+
))
|
|
34
|
+
RSA_PRIVATE_KEY: Final = RSA.construct((
|
|
35
|
+
0x8C81424BC166F4918756E9F7B22EFAA03479B081E61896872CB7C51C910D7EC1A4CE2871424D5C9149BD5E08A25959A19AD3C981E6512EFDAB2BB8DA3F1E315C294BD117A9FB9D8CE8E633B4962E087C629DC6CA3A149214B4091EF2B0363CB3AE6C7EE702377F055ED3CD93F6C342256A76554BBEA7F203437BBE65F2DA2741,
|
|
36
|
+
0x10001,
|
|
37
|
+
0x3704DAB00D80C25E464FFB785A16D95F688D0A5823811758C16308D5A1DB55FA800D967A9B4AEDE79AA783ADFFDCDB23541C80B8D436901F172B1CCCA190B224DBE777BF18B96DD9A30AACE8780350793A4F90A645A7747EF695622EADBE23A4C6D88F22E87842B43B35486C2D1B5B1FA77DB3528B0910CA84EDB7A46AFDBED1,
|
|
38
|
+
))
|
|
39
|
+
RSA_PUBLIC_KEY: Final = RSA.construct((
|
|
40
|
+
0x8686980c0f5a24c4b9d43020cd2c22703ff3f450756529058b1cf88f09b8602136477198a6e2683149659bd122c33592fdb5ad47944ad1ea4d36c6b172aad6338c3bb6ac6227502d010993ac967d1aef00f0c8e038de2e4d3bc2ec368af2e9f10a6f1eda4f7262f136420c07c331b871bf139f74f3010e3c4fe57df3afb71683,
|
|
41
|
+
0x10001,
|
|
42
|
+
))
|
|
43
|
+
RSA_PUBLIC_KEY_e = RSA_PUBLIC_KEY.e
|
|
44
|
+
RSA_PUBLIC_KEY_n = RSA_PUBLIC_KEY.n
|
|
45
|
+
AES_KEY: bytes = b"\xfb\x1a\x19\xd6R\xf5\xaa\xf7\xbce\x1d\x0fi\xbfB/"
|
|
46
|
+
AES_IV: bytes = b"i\xbfB/I\x96\x05P\xa0\xadD\xec4F\xcbL"
|
|
47
|
+
|
|
48
|
+
NIST224P_BASELEN: Final = 28
|
|
49
|
+
RSA_BLOCK_SIZE: Final = RSA_PUBLIC_KEY.size_in_bytes()
|
|
50
|
+
RSA_KEY_SIZE: Final = RSA_BLOCK_SIZE // 8
|
|
51
|
+
|
p115cipher/fast.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
+
__all__ = ["rsa_encode", "rsa_decode", "ecdh_aes_encode", "ecdh_aes_decode", "ecdh_encode_token"]
|
|
6
|
+
|
|
7
|
+
from base64 import b64decode, b64encode
|
|
8
|
+
from binascii import crc32
|
|
9
|
+
|
|
10
|
+
from iterutils import acc_step
|
|
11
|
+
from lz4.block import decompress as lz4_block_decompress # type: ignore
|
|
12
|
+
from Crypto.Cipher import AES
|
|
13
|
+
|
|
14
|
+
from .const import RSA_PUBLIC_KEY, RSA_PUBLIC_KEY_e, RSA_PUBLIC_KEY_n, AES_KEY, AES_IV
|
|
15
|
+
from .common import Buffer, RSA_encrypt, gen_key, from_bytes, to_bytes, xor
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def rsa_encode(data: Buffer, /) -> bytes:
|
|
19
|
+
"把数据用 RSA 公钥加密"
|
|
20
|
+
xor_text: Buffer = bytearray(16)
|
|
21
|
+
tmp = memoryview(xor(data, b"\x8d\xa5\xa5\x8d"))[::-1]
|
|
22
|
+
xor_text += xor(tmp, b"x\x06\xadL3\x86]\x18L\x01?F")
|
|
23
|
+
cipher_data = bytearray()
|
|
24
|
+
xor_text = memoryview(xor_text)
|
|
25
|
+
for l, r, _ in acc_step(0, len(xor_text), 117):
|
|
26
|
+
cipher_data += RSA_encrypt(xor_text[l:r])
|
|
27
|
+
return b64encode(cipher_data)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def rsa_decode(cipher_data: Buffer, /) -> bytearray:
|
|
31
|
+
"把数据用 RSA 公钥解密"
|
|
32
|
+
cipher_data = memoryview(b64decode(cipher_data))
|
|
33
|
+
data = bytearray()
|
|
34
|
+
for l, r, _ in acc_step(0, len(cipher_data), 128):
|
|
35
|
+
p = pow(from_bytes(cipher_data[l:r]), RSA_PUBLIC_KEY_e, RSA_PUBLIC_KEY_n)
|
|
36
|
+
b = to_bytes(p, (p.bit_length() + 0b111) >> 3)
|
|
37
|
+
data += memoryview(b)[b.index(0)+1:]
|
|
38
|
+
m = memoryview(data)
|
|
39
|
+
key_l = gen_key(m[:16], 12)
|
|
40
|
+
tmp = memoryview(xor(m[16:], key_l))[::-1]
|
|
41
|
+
return xor(tmp, b"\x8d\xa5\xa5\x8d")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def ecdh_aes_encode(data: Buffer, /) -> bytes:
|
|
45
|
+
"用 AES 加密数据,密钥由 ECDH 生成"
|
|
46
|
+
pad_size = -len(data) & 15
|
|
47
|
+
return AES.new(AES_KEY, 2, AES_IV).encrypt(
|
|
48
|
+
data + to_bytes(pad_size) * pad_size)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def ecdh_aes_decode(cipher_data: Buffer, /, decompress: bool = False) -> Buffer:
|
|
52
|
+
"用 AES 解密数据,密钥由 ECDH 生成"
|
|
53
|
+
data = AES.new(AES_KEY, 2, AES_IV).decrypt(
|
|
54
|
+
memoryview(cipher_data)[:len(cipher_data) & -16])
|
|
55
|
+
data = memoryview(data)
|
|
56
|
+
if decompress:
|
|
57
|
+
size = data[0] + (data[1] << 8)
|
|
58
|
+
data = lz4_block_decompress(data[2:size+2], 0x2000)
|
|
59
|
+
else:
|
|
60
|
+
padding = data[-1]
|
|
61
|
+
if data[-padding:] == bytes(data[-1:]) * padding:
|
|
62
|
+
data = data[:-padding]
|
|
63
|
+
return data
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def ecdh_encode_token(timestamp: int, /) -> bytes:
|
|
67
|
+
"用时间戳生成 token,并包含由 ECDH 生成的公钥"
|
|
68
|
+
token = bytearray()
|
|
69
|
+
token += b"\x1d\x03\x0e\x80\xa1x\xdc\xee\xce\xcd\xa3w\xde\x12\x8d\x00s\x00\x00\x00"
|
|
70
|
+
token += to_bytes(timestamp, 4, "little")
|
|
71
|
+
token += b"\x8e\xd9\xdd\xcfU\xaea\xedF\xea\x12\x1a\x1c\xfc\x81\x00\x01\x00\x00\x00"
|
|
72
|
+
token += to_bytes(crc32(b"^j>WD3Kr?J2gLFjD4W2y@" + token), 4, "little")
|
|
73
|
+
return b64encode(token)
|
|
74
|
+
|
p115cipher/normal.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
+
__all__ = ["P115RSACipher", "P115ECDHCipher"]
|
|
6
|
+
|
|
7
|
+
from base64 import b64decode, b64encode
|
|
8
|
+
from binascii import crc32
|
|
9
|
+
from itertools import pairwise
|
|
10
|
+
from random import randrange
|
|
11
|
+
|
|
12
|
+
from lz4.block import decompress as lz4_block_decompress # type: ignore
|
|
13
|
+
from Crypto import Random
|
|
14
|
+
from Crypto.Cipher import AES
|
|
15
|
+
|
|
16
|
+
from .const import G_key_l, CRC_SALT, RSA_PUBLIC_KEY_e, RSA_PUBLIC_KEY_n, RSA_BLOCK_SIZE, RSA_KEY_SIZE
|
|
17
|
+
from .common import Buffer, RSA_encrypt, gen_key, from_bytes, to_bytes, xor, generate_ecdh_pair
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class P115RSACipher:
|
|
21
|
+
|
|
22
|
+
def __init__(self, /):
|
|
23
|
+
self.rand_key: bytes = Random.new().read(RSA_KEY_SIZE)
|
|
24
|
+
self.key: bytes = gen_key(self.rand_key)
|
|
25
|
+
|
|
26
|
+
def encode(self, text: bytes | bytearray | str, /) -> bytes:
|
|
27
|
+
if isinstance(text, str):
|
|
28
|
+
text = bytes(text, "utf-8")
|
|
29
|
+
tmp = xor(text, self.key)[::-1]
|
|
30
|
+
xor_text = self.rand_key + xor(tmp, G_key_l)
|
|
31
|
+
block_size = RSA_BLOCK_SIZE - 11
|
|
32
|
+
cipher_text = bytearray()
|
|
33
|
+
for l, r in pairwise(range(0, len(xor_text) + block_size, block_size)):
|
|
34
|
+
cipher_text += RSA_encrypt(xor_text[l:r])
|
|
35
|
+
return b64encode(cipher_text)
|
|
36
|
+
|
|
37
|
+
def decode(self, cipher_text: bytes | bytearray | str, /) -> bytes:
|
|
38
|
+
cipher_text = b64decode(cipher_text)
|
|
39
|
+
text = bytearray()
|
|
40
|
+
for l, r in pairwise(range(0, len(cipher_text) + RSA_BLOCK_SIZE, RSA_BLOCK_SIZE)):
|
|
41
|
+
n = from_bytes(cipher_text[l:r])
|
|
42
|
+
m = pow(n, RSA_PUBLIC_KEY_e, RSA_PUBLIC_KEY_n)
|
|
43
|
+
b = to_bytes(m, (m.bit_length() + 0b111) >> 3)
|
|
44
|
+
text += b[b.index(0)+1:]
|
|
45
|
+
rand_key = text[0:RSA_KEY_SIZE]
|
|
46
|
+
text = text[RSA_KEY_SIZE:]
|
|
47
|
+
key_l = gen_key(rand_key, 12)
|
|
48
|
+
tmp = xor(text, key_l)[::-1]
|
|
49
|
+
return bytes(xor(tmp, self.key))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class P115ECDHCipher:
|
|
53
|
+
|
|
54
|
+
def __init__(self):
|
|
55
|
+
pub_key, secret = generate_ecdh_pair()
|
|
56
|
+
self.pub_key: bytes = pub_key
|
|
57
|
+
# NOTE: use AES-128
|
|
58
|
+
self.aes_key: bytes = secret[:16]
|
|
59
|
+
self.aes_iv: bytes = secret[-16:]
|
|
60
|
+
|
|
61
|
+
def encode(self, text: bytes | bytearray | str, /) -> bytes:
|
|
62
|
+
"加密数据"
|
|
63
|
+
if isinstance(text, str):
|
|
64
|
+
text = bytes(text, "utf-8")
|
|
65
|
+
pad_size = 16 - (len(text) & 15)
|
|
66
|
+
return AES.new(self.aes_key, AES.MODE_CBC, self.aes_iv).encrypt(
|
|
67
|
+
text + to_bytes(pad_size) * pad_size)
|
|
68
|
+
|
|
69
|
+
def decode(
|
|
70
|
+
self,
|
|
71
|
+
cipher_text: bytes | bytearray,
|
|
72
|
+
/,
|
|
73
|
+
decompress: bool = False,
|
|
74
|
+
) -> bytes:
|
|
75
|
+
"解密数据"
|
|
76
|
+
data = AES.new(self.aes_key, AES.MODE_CBC, self.aes_iv).decrypt(
|
|
77
|
+
cipher_text[:len(cipher_text) & -16])
|
|
78
|
+
if decompress:
|
|
79
|
+
size = data[0] + (data[1] << 8)
|
|
80
|
+
data = lz4_block_decompress(data[2:size+2], 0x2000)
|
|
81
|
+
else:
|
|
82
|
+
padding = data[-1]
|
|
83
|
+
if all(c == padding for c in data[-padding:]):
|
|
84
|
+
data = data[:-padding]
|
|
85
|
+
return data
|
|
86
|
+
|
|
87
|
+
def encode_token(self, /, timestamp: int) -> bytes:
|
|
88
|
+
"接受一个时间戳(单位是秒),返回一个 token,会把 pub_key 和 timestamp 都编码在内"
|
|
89
|
+
r1, r2 = randrange(256), randrange(256)
|
|
90
|
+
token = bytearray()
|
|
91
|
+
ts = to_bytes(timestamp, (timestamp.bit_length() + 0b111) >> 3)
|
|
92
|
+
if isinstance(self, P115ECDHCipher):
|
|
93
|
+
pub_key = self.pub_key
|
|
94
|
+
else:
|
|
95
|
+
pub_key = self
|
|
96
|
+
token.extend(pub_key[i]^r1 for i in range(15))
|
|
97
|
+
token.append(r1)
|
|
98
|
+
token.append(0x73^r1)
|
|
99
|
+
token.extend((r1,)*3)
|
|
100
|
+
token.extend(r1^ts[3-i] for i in range(4))
|
|
101
|
+
token.extend(pub_key[i]^r2 for i in range(15, len(pub_key)))
|
|
102
|
+
token.append(r2)
|
|
103
|
+
token.append(0x01^r2)
|
|
104
|
+
token.extend((r2,)*3)
|
|
105
|
+
crc = crc32(CRC_SALT+token) & 0xffffffff
|
|
106
|
+
h_crc32 = to_bytes(crc, 4)
|
|
107
|
+
token.extend(h_crc32[3-i] for i in range(4))
|
|
108
|
+
return b64encode(token)
|
|
109
|
+
|
|
110
|
+
@staticmethod
|
|
111
|
+
def decode_token(data: str | bytes) -> tuple[bytes, int]:
|
|
112
|
+
"解密 token 数据,返回 pub_key 和 timestamp 的元组"
|
|
113
|
+
data = b64decode(data)
|
|
114
|
+
r1 = data[15]
|
|
115
|
+
r2 = data[39]
|
|
116
|
+
return (
|
|
117
|
+
bytes(c ^ r1 for c in data[:15]) + bytes(c ^ r2 for c in data[24:39]),
|
|
118
|
+
from_bytes(bytes(i ^ r1 for i in data[20:24]), byteorder="little"),
|
|
119
|
+
)
|
|
120
|
+
|
p115cipher/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: p115cipher
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: 115 cipher module.
|
|
5
|
+
Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/p115cipher
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: 115,webdisk,cipher
|
|
8
|
+
Author: ChenyangGao
|
|
9
|
+
Author-email: wosiwujm@gmail.com
|
|
10
|
+
Requires-Python: >=3.10,<4.0
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
|
+
Classifier: Topic :: Software Development
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Requires-Dist: ecdsa
|
|
25
|
+
Requires-Dist: lz4
|
|
26
|
+
Requires-Dist: pycryptodome
|
|
27
|
+
Requires-Dist: python-iterutils (>=0.0.7)
|
|
28
|
+
Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/p115cipher
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# 115 cipher module
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
You can install via [pypi](https://pypi.org/project/cipher115/)
|
|
36
|
+
|
|
37
|
+
```console
|
|
38
|
+
pip install -U cipher115
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from cipher115.fast import *
|
|
45
|
+
from cipher115.normal import *
|
|
46
|
+
```
|
|
47
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
2
|
+
p115cipher/__init__.py,sha256=eCP8DTS9BexYsm6MVxaPfsKN-6uSCWj6qEaMUSrMOIg,168
|
|
3
|
+
p115cipher/common.py,sha256=I5uf-2qER5OR7a9x_0rMZRJ7jtJnAfzCRlD6Crf9NYY,3547
|
|
4
|
+
p115cipher/const.py,sha256=ZT_KIx7D-bCW8ohh9BNVcDkn8cGdXYs--UAAqBdpfdo,2969
|
|
5
|
+
p115cipher/fast.py,sha256=miqtHWGfAGQp-feGOLPwqCcn5TsXRFurF7jfkc-PsQg,2778
|
|
6
|
+
p115cipher/normal.py,sha256=hyznPhRsGHbnfMglBVXGq3xBry0ofBpQurOevpcZ6m0,4402
|
|
7
|
+
p115cipher/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
p115cipher-0.0.1.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
9
|
+
p115cipher-0.0.1.dist-info/METADATA,sha256=2oaZTNMiKqh9OK-ipkwSeGZ9MaZM7b6Haa8u2OaPMo4,1454
|
|
10
|
+
p115cipher-0.0.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
|
11
|
+
p115cipher-0.0.1.dist-info/RECORD,,
|