2013-10-14 86 views
0

我正在解密加密的MS Excel(RC4加密與SHA1),密碼已經知道了。在vs2010中,我可以正確解密,但是,我的程序已經在Win和linux.And我也沒辦法讓linux下的加密密鑰,現在,這是贏下類似如下:解密的MS辦公室

int getEncrypKey(HCRYPTKEY *hKey, int blocknum) 
{ 
    //------------------------H0 = H(salt, password)----- 
    BYTE *pbSaltandPwdHash = NULL; 
    DWORD dwSaltandPwdLen = 0; 

    pbSaltandPwdHash = SHA1_2(psalt, 16, ppwd, strlen(pwd)/2, &dwSaltandPwdLen); 
    printf("SHA1 of SaltandPwd:\n"); 
    for(DWORD i = 0 ; i < dwSaltandPwdLen ; i++) { 
    printf("%2.2x ",pbSaltandPwdHash[i]); 
    } 
    printf("\n"); 
    //------------------------H0 = H(salt, password)----- 

    //------------------------Hfinal = H(H0, block)----- 
    HCRYPTHASH hHash1 = 0; 

    CryptCreateHash(hCryptProv, CALG_SHA1, 0, 0, &hHash1) ; 
    CryptHashData(hHash1, pbSaltandPwdHash, dwSaltandPwdLen, 0) ; 
    CryptHashData(hHash1, (unsigned char*)&blocknum, sizeof(blocknum), 0) ; 
    //------------------------Hfinal = H(H0, block)----- 

    CryptDeriveKey(hCryptProv, CALG_RC4, hHash1, 0x00280000, hKey); 

    if(hHash1 != 0) CryptDestroyHash(hHash1); 
    if(pbSaltandPwdHash != NULL) free(pbSaltandPwdHash); 

    return 0; 
} 

我知道如何讓H0 linux下,但我不知道如何讓hHash1hKey

+0

1)這與以前的問題有何不同? 2)從MS的加密API移植到另一個加密API(例如OpenSSL)的哪一部分會對您造成問題? – CodesInChaos

+0

@CodesInChaos我認爲我必須像'CryptCreateHash','CryptHashData'..and和'HCRYPTKEY'同樣的結構來解密MS加密的office文檔一樣。但是,我現在理解並解決它。感謝您的關注。 –

回答

1

這個職位聽起來就像是做同樣的事情:Implement Windows CryptoAPI CryptDeriveKey Using OpenSSL APIs

生成的OpenSSL散列的更一般的方法是如下:

在你做任何事情:

#include <ssl/evp.h> 

int main(int argc, char argv[]) // or in an "initialise" type function 
{ 
    OpenSSL_add_all_digests() 
    ... 
} 

然後生成散列(錯誤檢查省略):

const EVP_MD *digest; 
EVP_MD_CTX context; 
unsigned char hash[EVP_MAX_MD_SIZE]; 
unsigned int hash_len; 

digest = EVP_get_digestbyname("sha1"); /* choose the hash type here */ 

EVP_MD_CTX_init(&context); 
EVP_DigestInit_ex(&contxt, digest, NULL); 
EVP_DigestUpdate(&context, pbSaltandPwdHash, dwSaltandPwdLen); 
EVP_DigestUpdate(&context, &blocknum, sizeof(blocknum)); 
EVP_DigestFinal_ex(&context, hash, &hash_len); 
EVP_MD_CTX_cleanup(&context); 

/* Now use hash and hash_len as required */ 
+0

是的,這幾乎是我想要的,獲得散列後的解密密鑰。所以一些像'RC4_set_key'這樣的調用得到我的答案.thx爲您的答案。 –