放射加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def affine_decrypt(ciphertext, a, b, dictionary):
decrypted = ""
for char in ciphertext:

char_index = dictionary.index(char)
# 计算字符的模逆
char_inverse = mod_inverse(a, len(dictionary))
# 应用仿射解密公式
decrypted_char_index = (char_inverse * (char_index - b)) % len(dictionary)
decrypted += dictionary[decrypted_char_index]

return decrypted


def mod_inverse(a, m):
# 扩展欧几里得算法求模逆
g, x, _ = extended_gcd(a, m)
if g == 1:
return x % m


def extended_gcd(a, b):
if a == 0:
return b, 0, 1
else:
gcd, x, y = extended_gcd(b % a, a)
return gcd, y - (b // a) * x, x


# 自定义字典
dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXY"

# 输入密文和仿射加密的参数
ciphertext = "HIPHSFUPSU"
a = 2
b = 7


plaintext = affine_decrypt(ciphertext, a, b, dictionary)
print("明文:", plaintext)


ASCII转码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
hex_values = "0x10, 0x2C, 0x02, 0xFC, 0xFB, 0x3B, 0x0D, 0x73, 0x6E, 0xBC, 0xB9, 0xA7, 0x6F, 0x2F "

hex_values_list = hex_values.split(", ")

ascii_values = [int(hex_value[:-1], 16) for hex_value in hex_values_list]

ascii_chars = [chr(ascii_value) for ascii_value in ascii_values]

ascii_representation = ''.join(ascii_chars)

print("ASCII representation:", ascii_representation)



byte_sequence = b"\x10\x2C\x02\xFC\xFB\x3B\x0D\x73\x6E\xBC\xB9\xA7\x6F\x2F"
string_representation = byte_sequence.decode('ascii', errors='ignore')

print("字符串表示:", string_representation)

des

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import re

def hex_to_int(hex_str):
hex_str = hex_str.replace("h", "").strip()
return int(hex_str, 16)

def des_decrypt(ciphertext, key):
backend = default_backend()
cipher = Cipher(algorithms.TripleDES(key), modes.ECB(), backend=backend)
decryptor = cipher.decryptor()
plaintext = decryptor.update(ciphertext) + decryptor.finalize()
return plaintext

ciphertext_str = "42h,ACh,43h,D3h,F1h,44h,B1h,36h,"
key_str = "23h, 24h, 31h, 32h, 2Ah, 26h, 5Eh, 29h,"

ciphertext_bytes = bytearray(hex_to_int(x) for x in re.findall(r'[0-9a-fA-F]+', ciphertext_str))
key_bytes = bytearray(hex_to_int(x) for x in re.findall(r'[0-9a-fA-F]+', key_str))

plaintext = des_decrypt(ciphertext_bytes, key_bytes)
plaintext_str = plaintext.decode()

print("解密后的明文:", plaintext_str)

MD5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import hashlib
import itertools

md5_hash = "23d4a52c56357cb705137656744dc055" #散列值
plaintext_length = 4 #明文长度
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" #字典 ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz

# 生成所有可能的明文组合
combinations = itertools.product(charset, repeat=plaintext_length)

# 尝试每个明文组合并进行MD5散列计算
for combination in combinations:
plaintext = "".join(combination)
md5 = hashlib.md5(plaintext.encode()).hexdigest()

# 检查计算的MD5散列值是否与给定的散列值匹配
if md5 == md5_hash:
print("解密成功!明文为:", plaintext)
break
else:
print("未找到匹配的明文。")

RC4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import re

def rc4_decrypt(ciphertext, key):
S = list(range(256))
j = 0
decrypted = bytearray()

# 初始化S盒
for i in range(256):
j = (j + S[i] + key[i % len(key)]) % 256
S[i], S[j] = S[j], S[i]

# 解密数据
i = j = 0
for byte in ciphertext:
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
k = S[(S[i] + S[j]) % 256]
decrypted.append(byte ^ k)

return decrypted

ciphertext_str = "1Bh, 0CAh, 0AEh, 0EFh, 1Eh, 95h, 4Bh, 0C2h, 0D5h, 0E3h, 33h, 76h, 4Fh, 0F9h, 4Fh, 0D2h, 0FCh, 60h"
ciphertext_bytes = bytearray(int(match.group(1), 16) for match in re.finditer(r"([0-9A-Fa-f]+)h", ciphertext_str))

key_str = "52h, 43h, 34h, 6Bh, 65h, 79h"
key_bytes = bytearray(int(match.group(1), 16) for match in re.finditer(r"([0-9A-Fa-f]+)h", key_str))

plaintext = rc4_decrypt(ciphertext_bytes, key_bytes)
print("解密后的明文:", plaintext.decode('latin-1'))

SHA256

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import hashlib

hash_value = "45c6c64ffd31a750bcb2e150519f1963e5424bafd380637cff33075c25d35334"
plaintext_length = 4
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" #字典 ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz

def generate_combinations(length, charset):
if length == 0:
yield ''
else:
for char in charset:
for combination in generate_combinations(length - 1, charset):
yield char + combination

for combination in generate_combinations(plaintext_length, charset):
hashed_combination = hashlib.sha256(combination.encode()).hexdigest()
if hashed_combination == hash_value:
print("Plaintext found:", combination)
break

XOR 异或

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import re

plaintext = "61h, 62h, 63h, 64h, 65h, 66h, 67h, 31h, 32h, 33h"
ciphertext = "28h, 3Dh, 24h, 54h, 0Ah, 12h, 38h, 7Ah, 57h, 4Ah"

plaintext = plaintext.replace(" ", "").split(",")
ciphertext = ciphertext.replace(" ", "").split(",")


result = []
for i in range(len(plaintext)):
hex_plain = re.sub(r'[^0-9a-fA-F]', '', plaintext[i])
hex_cipher = re.sub(r'[^0-9a-fA-F]', '', ciphertext[i])
decrypted = int(hex_plain, 16) ^ int(hex_cipher, 16)
result.append(chr(decrypted))

output = "".join(result)
print(output)