了解常用加解密算法并简单逆向识别
本文为看雪论坛优秀文章
看雪论坛作者ID:阿伪
>>>> 历史发展
历史发展
>>>> 基本原理
基本原理
>>>> 逆向初探
逆向初探
>>>> 魔改Base64
魔改Base64
>>>> MD5
MD5
特征
#define F(x, y, z) (((x) & (y)) | ((~(x)) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~(z))))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~(z))))
逆向识别
#!python3
import hashlib
str = input()
str +='www.pediy.com'
hash = hashlib.md5()
hash.update(str.encode('utf-8'))
hash = hash.hexdigest()
code = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
serial = ""
for i in range(0,len(hash),2):
a = '0x' + hash[i]+hash[i+1]
serial = serial+code[eval(a)%32]
serial = serial[:4] + '-' + serial[4:8] +'-' + serial[8:12] +'-'+serial[12:16]
print(serial)
魔改
>>>> SHA算法
SHA算法
SHA-1
特征
#define F0(x,y,z) ((x&y)|((~x)&z))
#define F1(x,y,z) (x^y^z)
#define F2(x,y,z) ((x&y) | (x&z)|(y&z))
#define F3(x,y,z) (x^y^z)
逆向初探SHA-1加密
#!python3
import hashlib
output = []
str = input()
hash = hashlib.sha1()
hash.update(str.encode('utf-8'))
hash = hash.hexdigest()
xor_str_1 = [0x50,0x45, 0x44, 0x49, 0x59, 0x20, 0x46, 0x6F ,0x72 ,0x75, 0x6D, 0x00 ]
xor_str_2 = "pediy.com"
num = 0
# print(hash)
for i in range(0,34,2):
if i > 22:
b = '0x' + (hash[i] + hash [i+1])
a = eval(b) ^ eval(output[int((i/2))-12])
output.append(hex(a))
continue
b = '0x' + (hash[i] + hash [i+1])
a = eval(b) ^ (xor_str_1[int(i/2)])
output.append(hex(a))
for i in range(34,40,2):
b = '0x' + (hash[i] + hash [i+1])
a = eval(b) ^ ord(xor_str_2[int(i/2)-17])
output.append(hex(a))
for i in range(10):
output[10+i] = hex(eval(output[i]) ^ eval(output[10+i]))
for i in range(10,20):
print('{:0>2}'.format((output[i][2:]).upper()),end = "")
>>>> 总结
总结
for (i=0;i<20;i++)
{ /* convert to bytes */
hash[i]=((sh->h[i/4] (8*(3-i%4))) & 0xffL);
}
>>>> RC4
RC4
按照升序0,1,2,3,4.....,254,255初始化一个256字节数组S.
使用密钥填充一个256字节数组T ,长度不够的话,轮转填入,直到填满.
对数组S进行打乱.
int j = 0;
for (i = 0;i<256;i++){
j =(j+S[i]+T[i])%256;
swap(S[i],S[j]);
}
int i, j = 0;
while (data_length--) {
i = (i + 1) % 256;
i = (i + 1) % 256;
j = (j + S[i]) % 256;
swap(S[i], S[j]);
int t = (S[i] + S[j]) % 256;
int k = S[t];
//k为加密密钥,直接进行与数据异或或者存进数组里最后进行异或都可以,
}
using namespace std;
int S[256] = { 0 };
void swap(int& a, int& b) {
int c = a;
a = b;
b = c;
}
void KSA(unsigned char key[], int len) {
for (size_t i = 0; i < 256; i++) S[i] = i;
int j = 0;
for (size_t i = 0; i < 256; i++)
{
j = (j + S[i] + key[i % len]) % 256;
swap(S[i], S[j]);
}
}
void PRGA(unsigned char data[], int len) {
int i = 0, j = 0, num = 0;
int data_length = len;
while (data_length--) {
i = (i + 1) % 256;
j = (j + S[i]) % 256;
swap(S[i], S[j]);
int t = (S[i] + S[j]) % 256;
int k = S[t];
data[num] = (data[num] ^ k);
num++;
}
}
int main() {
unsigned char key[] = "xwdidi.com";
unsigned char data[] = "bbspediycom";
KSA(key, strlen((char*)key));
PRGA(data,strlen((char*)data));
for (size_t i = 0; i < strlen((char*)data); i++)
{
cout << hex << (int)data[i] << " ";
}
return 0;
}
主函数体伪代码
sub_401000
sub_401070
魔改RC4
>>>> 总结
总结
>>>> TEA
TEA
>>>> 基本原理
基本原理
>>>> 空间换时间
空间换时间
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[round*4];
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[round*4+1];
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[round*4+2];
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[round*4+3];
>>>> 逆向初探识别RC4
逆向初探识别RC4
#!python3
from Crypto.Cipher import AES
import hashlib
#"xwdidi" md5
hash = b"\x39\xd7\x8e\xe5\x67\xf3\xf2\x96\xad\x84\x8d\x3f\xcd\xb1\xd4\x61"
key = b"\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c"
cipher = AES.new(key,AES.MODE_ECB)
plaintext = cipher.decrypt(hash)
for i in plaintext:
print(hex(i)[2:].upper(),end="")
>>>> 加密模式与填充模式
加密模式与填充模式
ECB: 需要加密的消息按照块密码的块大小被分为数个块,并对每个块进行独立加密。
CBC : 每 个明文块先与前一个密文块进行异或后,再进行加密
CTR
OCF
CFB
PKCS7Padding:假设数据长度需要填充n(n>0)个字节才对齐,那么填充n个字节,每个字节都是n;如果数据本身就已经对齐了,则填充一块长度为块大小的数据,每个字节都是块大小。
PKCS5Padding:PKCS7Padding的子集,块大小固定为8字节
Zero-Padding用0填充(适合以\ 0结尾的字符串加解密)
>>>> RSA
RSA
基本原理
加密
加密 n^e ≡ c \bmod N,消息解密c^d ≡ n \bmod N \
(只需要证明n^{ed} ≡n\bmod N即可)
$$
逆向初探RSA
#!python3
import binascii
c =input()
a = ""
for i in c:
a = a + hex(ord(i))
# print(a)
a = a.replace("0x","")
e = "0x" + a
# print(e)
result = pow(eval(e),0x651A40B9739117EF505DBC33EB8F442D,0x80C07AFC9D25404D6555B9ACF3567CF1)
print(hex(result)[2:])
>>>> Mircal大数运算库
Mircal大数运算库
MIRACL MAGIC NUMBERS TABLE:
by bLaCk-eye
from an original ideea by bF!^k23
Modified by cnbragon for miracl v5.01
NUMBER OF FUNCTIONS: 96h
innum equ 01h .
otnum equ 02h .
jack equ 03h .
normalise equ 04h .
multiply equ 05h .
divide equ 06h .
incr equ 07h .
decr equ 08h .
premult equ 09h .
subdiv equ 0Ah .
fdsize equ 0Bh .
egcd equ 0Ch .
cbase equ 0Dh .
cinnum equ 0Eh .
cotnum equ 0Fh .
nroot equ 10h .
power equ 11h .
powmod equ 12h .
bigdig equ 13h .
bigrand equ 14h .
nxprime equ 15h .
isprime equ 16h .
mirvar equ 17h .
mad equ 18h .
multi_inverse equ 19h .
putdig equ 1Ah .
add equ 1Bh .
subtract equ 1Ch .
mirsys equ 1Dh .
xgcd equ 1Eh .
fpack equ 1Fh .
dconv equ 20h .
mr_shift equ 21h .
mround equ 22h .
fmul equ 23h .
fdiv equ 24h .
fadd equ 25h .
fsub equ 26h .
fcomp equ 27h .
fconv equ 28h .
frecip equ 29h .
fpmul equ 2Ah .
fincr equ 2Bh .
;null entry
ftrunc equ 2Dh .
frand equ 2Eh .
sftbit equ 2Fh .
build equ 30h .
logb2 equ 31h .
expint equ 32h .
fpower equ 33h .
froot equ 34h .
fpi equ 35h .
fexp equ 36h .
flog equ 37h .
fpowf equ 38h .
ftan equ 39h .
fatan equ 3Ah .
fsin equ 3Bh .
fasin equ 3Ch .
fcos equ 3Dh .
facos equ 3Eh .
ftanh equ 3Fh .
fatanh equ 40h .
fsinh equ 41h .
fasinh equ 42h .
fcosh equ 43h .
facosh equ 44h .
flop equ 45h .
gprime equ 46h .
powltr equ 47h .
fft_mult equ 48h .
crt_init equ 49h .
crt equ 4Ah .
otstr equ 4Bh .
instr equ 4Ch .
cotstr equ 4Dh .
cinstr equ 4Eh .
powmod2 equ 4Fh .
prepare_monty equ 50h .
nres equ 51h .
redc equ 52h .
nres_modmult equ 53h .
nres_powmod equ 54h .
nres_moddiv equ 55h .
nres_powltr equ 56h .
divisible equ 57h .
remain equ 58h .
fmodulo equ 59h .
nres_modadd equ 5Ah .
nres_modsub equ 5Bh .
nres_negate equ 5Ch .
ecurve_init equ 5Dh .
ecurve_add equ 5Eh .
ecurve_mult equ 5Fh .
epoint_init equ 60h .
epoint_set equ 61h .
epoint_get equ 62h .
nres_powmod2 equ 63h .
nres_sqroot equ 64h .
sqroot equ 65h
nres_premult equ 66h .
ecurve_mult2 equ 67h .
ecurve_sub equ 68h .
trial_division equ 69h .
nxsafeprime equ 6Ah .
nres_lucas equ 6Bh .
lucas equ 6Ch .
brick_init equ 6Dh .
pow_brick equ 6Eh .
set_user_function equ 6Fh .
nres_powmodn equ 70h .
powmodn equ 71h .
ecurve_multn equ 72h .
ebrick_init equ 73h .
mul_brick equ 74h .
epoint_norm equ 75h .
nres_multi_inverse equ 76h .
;null entry
nres_dotprod equ 78h .
epoint_negate equ 79h .
ecurve_multi_add equ 7Ah .
ecurve2_init equ 7Bh .
epoint2_init equ 7Ch
epoint2_set equ 7Dh .
epoint2_norm equ 7Eh .
epoint2_get equ 7Fh .
epoint2_comp equ 80h .
ecurve2_add equ 81h .
epoint2_negate equ 82h .
ecurve2_sub equ 83h .
ecurve2_multi_add equ 84h .
ecurve2_mult equ 85h .
ecurve2_multn equ 86h .
ecurve2_mult2 equ 87h .
ebrick2_init equ 88h .
mul2_brick equ 89h .
prepare_basis equ 8Ah .
strong_bigrand equ 8Bh .
bytes_to_big equ 8Ch .
big_to_bytes equ 8Dh .
set_io_buffer_size equ 8Eh .
epoint_getxyz equ 8Fh .
ecurve_double_add equ 90h .
nres_double_inverse equ 91h .
double_inverse equ 92h .
epoint_x equ 93h .
hamming equ 94h .
expb2 equ 95h .
bigbits equ 96h .
>>>> 反汇编识别
反汇编识别
sub_401730:
sub_403BD0:
看雪ID:阿伪
https://bbs.pediy.com/user-779000.htm
推荐文章++++