总是得到与CryptoAPI相同的加密字符串

问题描述:

有没有办法从CryptoAPI获得非确定性输出?换句话说,加密字符串时会输出不同的字符串。总是得到与CryptoAPI相同的加密字符串

例如,获得与“密码”密码加密密钥和字符串加密的“A”在使用CALG_AES_256,它总是返回“SnÆwÞ¢大号\ X1E?6FÏLþw”

我有点的n00b使用CryptoAPI,所以任何帮助表示赞赏。

编辑: 下面是来自微软的示例代码密码系统的代码decrypteencrypt这是相同的代码,只是缩短/压实。此代码作为Win32控制台应用程序在VS 2017中编译。 pszSource和pszDest是C:\ temp文件夹中的两个文件。 source.txt有我们试图加密的信件。

时遇到的问题是从的CryptoAPI 这隐窝/解密代码不允许某些字符串来进行加密,然后解密后的(即,n,T,L,P,AA,AB,AC,AD ,ae等)。如果有人能告诉我为什么,那会非常有帮助。

#include <windows.h> 
#include <tchar.h> 
#include <wincrypt.h> 

#define KEYLENGTH 0x00800000 
#define ENCRYPT_BLOCK_SIZE 8 

bool MyDecryptFile(LPTSTR szSource,LPTSTR szDestination,LPTSTR szPassword); 
bool MyEncryptFile(LPTSTR szSource,LPTSTR szDestination,LPTSTR szPassword); 

int _tmain(int argc, _TCHAR* argv[]) 
{ 

    LPTSTR pszSource = L"c:\\temp\\source.txt"; 
    LPTSTR pszDestination = L"c:\\temp\\dest.txt"; 
    LPTSTR pszPassword = L"t"; 

    if (MyEncryptFile(pszSource, pszDestination, pszPassword)) 
     { 
     _tprintf(TEXT("Encryption of the file %s was successful. \n"),pszSource); 
     _tprintf(TEXT("The encrypted data is in file %s.\n"),pszDestination); 
     } 

    if (MyDecryptFile(pszSource, pszDestination, pszPassword)) 
     { 
     _tprintf(TEXT("Encryption of the file %s was successful. \n"),pszSource); 
     _tprintf(TEXT("The encrypted data is in file %s.\n"),pszDestination); 
     } 

    return 0; 
} 

bool MyEncryptFile(LPTSTR pszSourceFile,LPTSTR pszDestinationFile,LPTSTR pszPassword) 
{ 
    bool fReturn = false; 
    HANDLE hSourceFile = INVALID_HANDLE_VALUE, hDestinationFile = INVALID_HANDLE_VALUE; 
    HCRYPTPROV hCryptProv = NULL; 
    HCRYPTKEY hKey = NULL, hXchgKey = NULL; 
    HCRYPTHASH hHash = NULL; 
    PBYTE pbBuffer = NULL; 
    DWORD dwBlockLen, dwBufferLen, dwCount; 

    hSourceFile = CreateFile(pszSourceFile,FILE_READ_DATA,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); 
    if (INVALID_HANDLE_VALUE == hSourceFile) 
     goto Exit_MyEncryptFile; 

    hDestinationFile = CreateFile(pszDestinationFile,FILE_WRITE_DATA,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); 
    if (INVALID_HANDLE_VALUE == hDestinationFile) 
     goto Exit_MyEncryptFile; 

    CryptAcquireContext(&hCryptProv,NULL,MS_ENH_RSA_AES_PROV,PROV_RSA_AES,0); 
    CryptCreateHash(hCryptProv,CALG_SHA_256,0,0,&hHash); 
    CryptHashData(hHash,(BYTE *)pszPassword,lstrlen(pszPassword),0); 
    CryptDeriveKey(hCryptProv,CALG_AES_256,hHash,CRYPT_EXPORTABLE,&hKey); 

    dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE; 
    if (ENCRYPT_BLOCK_SIZE > 1) 
     dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE; 
    else 
     dwBufferLen = dwBlockLen; 
    pbBuffer = (BYTE *)malloc(dwBufferLen); 

    bool fEOF = FALSE; 
    do 
    { 
     if (ReadFile(hSourceFile,pbBuffer,dwBlockLen,&dwCount,NULL)) 
      { 
      if (dwCount < dwBlockLen) 
       fEOF = TRUE; 
      if (CryptEncrypt(hKey,NULL,fEOF,0,pbBuffer,&dwCount,dwBufferLen)) 
       WriteFile(hDestinationFile,pbBuffer,dwCount,&dwCount,NULL); 
      } 
    } 
    while (!fEOF); 

    fReturn = true; 

Exit_MyEncryptFile: 
    if (hSourceFile) CloseHandle(hSourceFile); 
    if (hDestinationFile) CloseHandle(hDestinationFile); 
    if (pbBuffer) free(pbBuffer); 
    if (hHash) {CryptDestroyHash(hHash);hHash = NULL;} 
    if (hKey) CryptDestroyKey(hKey); 
    if (hCryptProv) CryptReleaseContext(hCryptProv, 0); 

    return fReturn; 
} 


bool MyDecryptFile(LPTSTR pszSourceFile,LPTSTR pszDestinationFile,LPTSTR pszPassword) 
{ 
    bool fReturn = false; 
    HANDLE hSourceFile = INVALID_HANDLE_VALUE, hDestinationFile = INVALID_HANDLE_VALUE; 
    HCRYPTKEY hKey = NULL; 
    HCRYPTHASH hHash = NULL; 
    HCRYPTPROV hCryptProv = NULL; 
    PBYTE pbBuffer = NULL; 
    DWORD dwCount, dwBlockLen, dwBufferLen; 

    hSourceFile = CreateFile(pszDestinationFile,FILE_READ_DATA,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); 
    if (INVALID_HANDLE_VALUE == hSourceFile) 
     goto Exit_MyDecryptFile; 

    hDestinationFile = CreateFile(pszSourceFile,FILE_WRITE_DATA,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); 
    if (INVALID_HANDLE_VALUE == hDestinationFile) 
     goto Exit_MyDecryptFile; 

    CryptAcquireContext(&hCryptProv,NULL,MS_ENH_RSA_AES_PROV,PROV_RSA_AES,0); 
    CryptCreateHash(hCryptProv,CALG_SHA_256,0,0,&hHash); 
    CryptHashData(hHash,(BYTE *)pszPassword,lstrlen(pszPassword),0); 
    CryptDeriveKey(hCryptProv,CALG_AES_256,hHash,CRYPT_EXPORTABLE,&hKey); 

    dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE; 
    dwBufferLen = dwBlockLen; 

    pbBuffer = (PBYTE)malloc(dwBufferLen); 
    bool fEOF = false; 
    do 
    { 
     if (!ReadFile(hSourceFile,pbBuffer,dwBlockLen,&dwCount,NULL)) 
      goto Exit_MyDecryptFile; 

     if (dwCount <= dwBlockLen) 
      fEOF = TRUE; 

     LONG rv = CryptDecrypt(hKey,0,fEOF,0,pbBuffer,&dwCount); 
     if (rv==0) 
      { 
      DWORD dwErr = GetLastError();  // <--- fails if password and string are n, t, L, p, aa, ab, ac, ad , ae 
      goto Exit_MyDecryptFile; 
      } 

     if (!WriteFile(hDestinationFile,pbBuffer,dwCount,&dwCount,NULL)) 
      goto Exit_MyDecryptFile; 
    } 
    while (!fEOF); 

    fReturn = true; 

Exit_MyDecryptFile: 

    if (pbBuffer) free(pbBuffer); 
    if (hSourceFile) CloseHandle(hSourceFile); 
    if (hDestinationFile) CloseHandle(hDestinationFile); 
    if (hHash) {CryptDestroyHash(hHash);hHash = NULL;} 
    if (hKey) CryptDestroyKey(hKey); 
    if (hCryptProv) CryptReleaseContext(hCryptProv, 0); 

    return fReturn; 
} 

如何使用它来获得KP_IV选项?

BOOL bRV; 
bRV = CryptAcquireContextW(&hCryptProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0); 
bRV = CryptGenKey(hCryptProv, CALG_AES_256,0,&hKey); 
DWORD dwMode = CRYPT_MODE_CBC; 
bRV = CryptSetKeyParam(hKey,KP_MODE,(BYTE*)&dwMode,0); 
BYTE pbData[16]; 
memcpy(pbData,"n",sizeof("n")); // <--- Hard coded password 
bRV = CryptSetKeyParam(hKey,KP_IV,pbData,0); 
enter code here 
+1

当保存密码验证程序只是使用哈希函数是不够的,只是添加盐对提高安全性没有多大作用。相反,用随机盐迭代HMAC约100ms持续时间,然后用散列表保存盐。更好的是使用诸如'PBKDF2','Rfc2898DeriveBytes','password_hash','Bcrypt','passlib.hash'或类似函数的函数。关键是要让攻击者花费大量时间通过暴力破解密码。 – zaph

+1

散列函数是确定性的,这是它们的一个主要属性,为了获得一个随机输出,你需要提供一个通常用密码安全伪随机数生成器[CSPRNG]获得的随机输入(https://en.wikipedia。组织/维基/ Cryptographically_secure_pseudorandom_number_generator)。 – zaph

+0

谢谢。旧的capicom.dll文件做了256个并且每次都给出了不同的字符串,但每次都正确解密。 – JeffR

如果你想用相同的密钥加密同一明文时获得不同的cypertext,你必须使用操作的CBC模式: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation

为了与CBC正确加密,您需要每次生成不同的随机初始化向量(IV)。 为了解密,您需要知道加密过程中使用的IV。因此,IV必须与密文相关联(清楚地)。

关于你例如,调用CryptDeriveKey功能时,CBC是默认模式,但它使用的IV设置为零,并且该无效的CBC操作模式的效用: https://msdn.microsoft.com/en-us/library/windows/desktop/aa379916(v=vs.85).aspx

为了设置你需要调用CryptSetKeyParam功能,接受KP_IV PARAM随机IV: https://msdn.microsoft.com/en-us/library/windows/desktop/aa380272(v=vs.85).aspx

再见 乔瓦尼

+0

我已经把CryptSetKeyParam的问题中的示例代码。该示例代码是使用CryptSetKeyParam的正确方法吗?它正确地加密,但是当我在CryptDecrypt之前使用相同的代码时,CryptDecrypt将GetLastEror设置为NTE_BAD_DATA。 – JeffR

+0

使用零IV制作的CBC并不安全,但在隐藏模式方面优于ECB模式,请参阅[ECB模式](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29),向下滚动至企鹅。除了CBC之外,还有其他的模式使用随机的IV或IV替代品。 – zaph

+0

我可以用零IV来加密它,但我无法解密它。你能帮助我吗? – JeffR