// 在使用openssl加密时,为了让Windows crypt32
// API能很方便的解密,需要同步CryptDeriveKey的功能。
// 适用于win api端CryptCreateHash指定为CALG_MD5的情况。
// https://learn.microsoft.com/zh-cn/windows/win32/api/wincrypt/nf-wincrypt-cryptderivekey?redirectedfrom=MSDN
unsigned char *crypt32DerivedKey(unsigned char *md5hash, int k /*hash长度*/,
int keyBitSize) {
/**step 1*/
unsigned char buffer1[64];
for (int i = 0; i < 64; i++) {
buffer1[i] = 0x36;
}
// int k = n;
for (int i = 0; i < k; i++) {
buffer1[i] = buffer1[i] ^ md5hash[i];
}
/*step 2*/
unsigned char buffer2[64];
for (int i = 0; i < 64; i++) {
buffer2[i] = 0x5C;
}
for (int i = 0; i < k; i++) {
buffer2[i] = buffer2[i] ^ md5hash[i];
}
/*step 3*/
unsigned char hash1[MD5_DIGEST_LENGTH];
MD5(buffer1, 64, hash1);
/*step 4*/
unsigned char hash2[MD5_DIGEST_LENGTH];
MD5(buffer2, 64, hash2);
/*step 5*/
int resultLen = keyBitSize / 8;
unsigned char *outputkey = (unsigned char *)malloc(resultLen);
for (int i = 0; i < resultLen; i++) {
if (i < 16)
outputkey[i] = hash1[i];
else
outputkey[i] = hash2[i - 16];
}
return outputkey;
}
openssl和Windows crypt32 AES加解密互通
由
·
发表回复