常用加密的解密脚本

TEA

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
#include <stdio.h>
#include <stdint.h>

//加密函数
void encrypt (uint32_t* v, uint32_t* k) {
uint32_t v0=v[0], v1=v[1], sum=0, i; /* set up */
uint32_t delta=0x9e3779b9; /* a key schedule constant */
uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
for (i=0; i < 32; i++) { /* basic cycle start */
sum += delta;
v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
} /* end cycle */
v[0]=v0; v[1]=v1;
}
//解密函数
void decrypt (uint32_t* v, uint32_t* k) {
uint32_t v0=v[0], v1=v[1], sum=0xC6EF3720, i; /* set up */
uint32_t delta=0x9e3779b9; /* a key schedule constant */
uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
for (i=0; i<32; i++) { /* basic cycle start */
v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
sum -= delta;
} /* end cycle */
v[0]=v0; v[1]=v1;
}

int main()
{
uint32_t v[2]={1,2},k[4]={1,2,3,4};
// v为要加密的数据是两个32位无符号整数
// k为加密解密密钥,为4个32位无符号整数,即密钥长度为128位
printf("Before enc: %#x %#x\n",v[0],v[1]);
encrypt(v, k);
printf("After enc: %#x %#x\n",v[0],v[1]);
decrypt(v, k);
printf("After dec: %#x %#x\n",v[0],v[1]);
return 0;
}

XTEA

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
43
44
#include <stdio.h>
#include <stdint.h>

/* take 64 bits of data in v[0] and v[1] and */
/* 128 bits of key[0] - key[3] */
void encrypt(unsigned int num_rounds, uint32_t v[2], uint32_t key[4]){
uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9e3779b9;

for(int i=0; i<num_rounds; i++){
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
sum += delta;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
//printf("%#x %#x\n",v0,v1);
}
v[0] = v0;
v[1] = v1;
}


void decrypt(unsigned int num_rounds, uint32_t v[2], uint32_t key[4]){
uint32_t v0=v[0], v1=v[1], delta=0x9e3779b9, sum=delta*num_rounds;
for(int i=0; i<num_rounds; i++){
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11)&3]);
sum -= delta;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
}
v[0] = v0;
v[1] = v1;
}


int main(void){
uint32_t v[2]={1,2};
uint32_t k[4]={1,2,3,4};
unsigned int r=32;//num_rounds建议取值为32
// v为要加密的数据是两个32位无符号整数
// k为加密解密密钥,为4个32位无符号整数,即密钥长度为128位
printf("%#x %#x\n",v[0],v[1]);
encrypt(r, v, k);
printf("%#x %#x\n",v[0],v[1]);
decrypt(r, v, k);
printf("%#x %#x\n",v[0],v[1]);
return 0;
}

XXTEA

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdint.h>
#define DELTA 0x9e3779b9
#define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (key[(p&3)^e] ^ z)))

void btea(uint32_t *v, int n, uint32_t const key[4]){
uint32_t y, z, sum;
unsigned p, rounds, e;

/* Coding Part */
if (n > 1) {
rounds = 6 + 52/n;
sum = 0;
z = v[n-1];
do{
sum += DELTA;
e = (sum >> 2) & 3;
for (p=0; p<n-1; p++){
y = v[p+1];
z = v[p] += MX;
}
y = v[0];
z = v[n-1] += MX;
}
while (--rounds);
}
else if (n < -1)/* Decoding Part */{
n = -n;
rounds = 6 + 52/n;
sum = rounds*DELTA;
y = v[0];
do{
e = (sum >> 2) & 3;
for (p=n-1; p>0; p--){
z = v[p-1];
y = v[p] -= MX;
}
z = v[n-1];
y = v[0] -= MX;
sum -= DELTA;
}
while (--rounds);
}
}

int main()
{
uint32_t v[2]= {1,2};
uint32_t const k[4]= {2,2,3,4};
int n= 2; //n的绝对值表示v的长度,取正表示加密,取负表示解密
// v为要加密的数据是两个32位无符号整数
// k为加密解密密钥,为4个32位无符号整数,即密钥长度为128位
printf("%#10x %#10x\n",v[0],v[1]);
btea(v, n, k);
printf("%#10x %#10x\n",v[0],v[1]);
btea(v, -n, k);
printf("%#10x %#10x\n",v[0],v[1]);
return 0;
}

实际应用

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
#include<iostream>
#define ut32 unsigned int
#define delta 0x67452301

void Tea_Decrypt(ut32* enc) {
unsigned int sum = 0x67452301 * 0x23;//0x1e73c923;
ut32 v0 = enc[0];
ut32 v1 = enc[1];
for (int i = 0; i < 0x23; i++) {
v1 -= ((v0 << 4) + 0x45) ^ (v0 + sum) ^ ((v0 >> 5) + 0x67);
v0 -= ((v1 << 4) + 1) ^ (v1 + sum) ^ ((v1 >> 5) + 0x23);
sum -= 0x67452301;
}
enc[0] = v0;
enc[1] = v1;
}

void Tea_Encrypt(ut32* src) {
ut32 sum = 0;
ut32 v0 = src[0];
ut32 v1 = src[1];
for (int i = 0; i < 0x23; i++) {
sum += 0x67452301;
v0 += ((v1 << 4) +1) ^ (v1 + sum) ^ ((v1 >> 5) + 0x23);
v1 += ((v0 << 4) + 0x45) ^ (v0 + sum) ^ ((v0 >> 5) + 0x67);
}
printf("%08x\n", sum);
src[0] = v0;
src[1] = v1;
}
int main() {
ut32 enc[8] = { 0xde087143,0xc4f91bd2,0xdaf6dadc,0x6d9ed54c,0x75eb4ee7,0x5d1ddc04,0x511b0fd9,0x51dc88fb };
for (int i = 0; i < 8; i += 2) {
Tea_Decrypt(enc+i);
}

for (int i = 0; i < 32; i++) {
printf("%c",*((unsigned char*)enc+i));
}
//327a6c4304ad5938eaf0efb6cc3e53dc
return 0;
}

RC4

1
2
3
4
5
6
7
8
9
10
11
from Crypto.Cipher import ARC4

arr = [0x56,0x61,0x63,0xA4,0x22,0xA4,0x50,0x7D,0xCD,0x8D,0x13,0x3D,0x4A,0x4F,0x0D,0x62,0x88,0xAB,0xFC,0xE9,0xBB,0x1E,0xA0,0x90 ]

print(len(arr))
key = 'D0g3'
# 需要修改 ARC4 key 长度 rang(4,...)
cipher = ARC4.new(bytes(key))
p = cipher.decrypt(bytes(arr))

print(p)

hash

sha256

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
import hashlib
import itertools
from pwn import *
import re

#log_level = debug
#p = remote('120.78.131.38', 10001)
#sha256 = str(p.recvuntil("equation:"))
alpha_bet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
strlist = itertools.permutations(alpha_bet, 4)
sha256 = '69f3db5a6f27bee0d4e9db2eca0ac8ea2110b8cbc9228012fb93898c43b64d4b'
flag = 0
for i in strlist:
data=i[0] + i[1] + i[2] + i[3] + 'CmLpYdZpGthThgVe'
data_sha = hashlib.sha256(data.encode('utf-8')).hexdigest()
if(data_sha == sha256):
flag = 1
print(i[0]+i[1]+i[2]+i[3])
break
#p.sendline(byte(data[-4:]))
#p.interactive()
if flag == 0:
print("Not found")

#SHA256(XXXX + CmLpYdZpGthThgVe):69f3db5a6f27bee0d4e9db2eca0ac8ea2110b8cbc9228012fb93898c43b64d4b

换表base64

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

#str1 = "zMXHz3TIgnxLxJhFAdtZn2fFk3lYCrtPC2l9".swapcase()#对字符串的字符大小写进行转换
s = [0x36, 0x33, 0x31, 0x16, 0x03, 0x32, 0x1F, 0x36, 0x3C, 0x22,
0x35, 0x32, 0x01, 0x3C, 0x3D, 0x0A, 0x01, 0x33, 0x07, 0x13,
0x07, 0x0B, 0x0C, 0x53, 0x01, 0x0A, 0x2D, 0x1F, 0x36, 0x0C,
0x58, 0x58, 0x65]
q = ''
for i in range(len(s)):
q += chr(s[i] ^ 0x65)

print(q)

string1 = "+/EFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCD"
string2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

print (base64.b64decode(q.translate(str.maketrans(string1,string2))))

base58

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
def b58encode(tmp:str) -> str:
tmp = list(map(ord,tmp))
temp = tmp[0]
base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
for i in range(len(tmp)-1):
temp = temp * 256 + tmp[i+1]
tmp = []
while True:
tmp.insert(0,temp % 58)
temp = temp // 58
if temp == 0:
break
temp = ""
for i in tmp:
temp += base58[i]
return temp

def b58decode(tmp:str) -> str:
import binascii
base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
temp = []
for i in tmp:
temp.append(base58.index(i))
tmp = temp[0]
for i in range(len(temp)-1):
tmp = tmp * 58 + temp[i+1]
return binascii.unhexlify(hex(tmp)[2:].encode("utf-8")).decode("UTF-8")

print(b58encode("ABDCDEFGA"))
print(b58decode("qBLiPgShKjap"))

AES

1
2
3
4
5
6
7
8
9
from Crypto.Cipher import AES
from ast import Bytes

ENtext = [0x49, 0x67, 0xeb, 0x32, 0x2e, 0x2e, 0x61, 0xda, 0xdb, 0x2e, 0xd7, 0x5a, 0xb9, 0x2e, 0xb2, 0x46]
key = b'goodlucksmartman'
print(key)
aes = AES.new(key,AES.MODE_ECB)
text = aes.decrypt(bytes(ENtext))
print(text)

常用加密的解密脚本
http://example.com/2023/04/25/常用加密的解密脚本/
Author
Eutop1a
Posted on
April 25, 2023
Licensed under