📄 stress.c
字号:
{
return(TRUE);
}
if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "RC2", "ECB"))
{
return(TRUE);
}
if (cArg > 1)
printf("Compare data ");
if (memcmp(bbuf, bRanbuf, bsize) != 0)
{
printf("FAIL\n");
return(TRUE);
}
if (cArg > 1)
printf("SUCCEED\n");
//
// Change mode to CFB
//
*pData = CRYPT_MODE_CFB;
if (RCRYPT_FAILED(CryptSetKeyParam(hKey, KP_MODE, pData, 0)))
{
if (cArg > 1)
printf("failed = %x\n", GetLastError());
else
printf("CryptSetKeyParam FAIL\n");
return(TRUE);
}
BLen = bsize;
BLen2 = bsize + 8;
if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "RC2", "CFB"))
{
return(TRUE);
}
if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "RC2", "CFB"))
{
return(TRUE);
}
if (cArg > 1)
printf("Compare data ");
if (memcmp(bbuf, bRanbuf, bsize) != 0)
{
printf("FAIL\n");
return(TRUE);
}
if (cArg > 1)
printf("SUCCEED\n");
if (RCRYPT_FAILED(CryptDestroyKey(hKey)))
{
if (cArg > 1)
printf("failed = %x\n", GetLastError());
else
printf("CryptDestroyKey FAIL\n");
return(TRUE);
}
return(FALSE);
}
BOOL TestRC4(int cArg, CHAR *bbuf, DWORD bsize)
{
//
// Generate a RC4 key
//
if (RCRYPT_FAILED(CryptGenKey(hMe, CALG_RC4, 0, &hKey)))
{
if (cArg > 1)
printf("failed = %x\n", GetLastError());
else
printf("CryptGenKey FAIL\n");
return(TRUE);
}
//
// Encrypt and Decrypt
//
BLen = bsize;
BLen2 = bsize + 8;
if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "RC4", ""))
{
return(TRUE);
}
if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "RC4", ""))
{
return(TRUE);
}
if (cArg > 1)
printf("Compare data ");
if (memcmp(bbuf, bRanbuf, bsize) != 0)
{
printf("FAIL\n");
return(TRUE);
}
if (cArg > 1)
printf("SUCCEED\n");
if (RCRYPT_FAILED(CryptDestroyKey(hKey)))
{
if (cArg > 1)
printf("failed = %x\n", GetLastError());
else
printf("CryptDestroyKey FAIL\n");
return(TRUE);
}
return(FALSE);
}
#ifdef TEST_VERSION
BOOL TestDES(int cArg, CHAR *bbuf, DWORD bsize)
{
//
// Generate a DES key
//
if (RCRYPT_FAILED(CryptGenKey(hMe, CALG_DES, 0, &hKey)))
{
if (cArg > 1)
printf("failed = %x\n", GetLastError());
else
printf("CryptGenKey FAIL\n");
return(TRUE);
}
//
// Encrypt and Decrypt using CBC default mode
//
BLen = bsize;
BLen2 = bsize + 8;
if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "DES", "CBC"))
{
return(TRUE);
}
if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "DES", "CBC"))
{
return(TRUE);
}
if (cArg > 1)
printf("Compare data ");
if (memcmp(bbuf, bRanbuf, bsize) != 0)
{
printf("FAIL\n");
return(TRUE);
}
if (cArg > 1)
printf("SUCCEED\n");
//
// Change mode to ECB
//
*pData = CRYPT_MODE_ECB;
if (RCRYPT_FAILED(CryptSetKeyParam(hKey, KP_MODE, pData, 0)))
{
if (cArg > 1)
printf("failed = %x\n", GetLastError());
else
printf("CryptSetKeyParam FAIL\n");
return(TRUE);
}
BLen = bsize;
BLen2 = bsize + 8;
if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "DES", "ECB"))
{
return(TRUE);
}
if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "DES", "ECB"))
{
return(TRUE);
}
if (cArg > 1)
printf("Compare data ");
if (memcmp(bbuf, bRanbuf, bsize) != 0)
{
printf("FAIL\n");
return(TRUE);
}
if (cArg > 1)
printf("SUCCEED\n");
//
// Change mode to CFB
//
*pData = CRYPT_MODE_CFB;
if (RCRYPT_FAILED(CryptSetKeyParam(hKey, KP_MODE, pData, 0)))
{
if (cArg > 1)
printf("failed = %x\n", GetLastError());
else
printf("CryptSetKeyParam FAIL\n");
return(TRUE);
}
BLen = bsize;
BLen2 = bsize + 8;
if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "DES", "CFB"))
{
return(TRUE);
}
if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "DES", "CFB"))
{
return(TRUE);
}
if (cArg > 1)
printf("Compare data ");
if (memcmp(bbuf, bRanbuf, bsize) != 0)
{
printf("FAIL\n");
return(TRUE);
}
if (cArg > 1)
printf("SUCCEED\n");
if (RCRYPT_FAILED(CryptDestroyKey(hKey)))
{
if (cArg > 1)
printf("failed = %x\n", GetLastError());
else
printf("CryptDestroyKey FAIL\n");
return(TRUE);
}
return(FALSE);
}
#endif
BOOL TestHash(int cArg, CHAR *bbuf, DWORD bsize)
{
if (cArg > 1)
printf("Hash, compare with MD4 & MD5 ");
BHashLen1 = 50;
BHashSignLen1 = MAXKEYLEN;
if (Hash(cArg, bbuf, bsize, pHashData1, &BHashLen1, pHashSignData1,
&BHashSignLen1, CALG_MD4))
{
return(TRUE);
}
if (memcmp(bbuf, bRanbuf, bsize) != 0)
{
printf("Data corrupted check 1 in Hash test\n");
return(TRUE);
}
BHashLen2 = 50;
BHashSignLen2 = MAXKEYLEN;
if (Hash(cArg, bbuf, bsize, pHashData2, &BHashLen2, pHashSignData2,
&BHashSignLen2, CALG_MD4))
{
return(TRUE);
}
if (memcmp(bbuf, bRanbuf, bsize) != 0)
{
printf("Data corrupted check 2 in Hash test\n");
return(TRUE);
}
if (BHashLen1 != BHashLen2)
{
printf("Hash lengths don't match MD4 FAIL\n");
return(TRUE);
}
if (memcmp(pHashData1, pHashData2, BHashLen1) != 0)
{
printf("Hash data doesn't compare MD4 FAIL\n");
return(TRUE);
}
if (BHashSignLen1 != BHashSignLen2)
{
printf("Hash signatures lengths don't match MD4 FAIL\n");
return(TRUE);
}
if (memcmp(pHashSignData1, pHashSignData2, BHashLen1) != 0)
{
printf("Hash signature data doesn't compare MD4 FAIL\n");
return(TRUE);
}
BHashLen1 = 50;
BHashSignLen1 = MAXKEYLEN;
if (Hash(cArg, bbuf, bsize, pHashData1, &BHashLen1, pHashSignData1,
&BHashSignLen1, CALG_MD5))
{
return(TRUE);
}
if (memcmp(bbuf, bRanbuf, bsize) != 0)
{
printf("Data corrupted check 3 in Hash test\n");
return(TRUE);
}
BHashLen2 = 50;
BHashSignLen2 = MAXKEYLEN;
if (Hash(cArg, bbuf, bsize, pHashData2, &BHashLen2, pHashSignData2,
&BHashSignLen2, CALG_MD5))
{
return(TRUE);
}
if (memcmp(bbuf, bRanbuf, bsize) != 0)
{
printf("Data corrupted check 4 in Hash test\n");
return(TRUE);
}
if (BHashLen1 != BHashLen2)
{
printf("Hash lengths don't match MD5 FAIL\n");
return(TRUE);
}
if (memcmp(pHashData1, pHashData2, BHashLen1) != 0)
{
printf("Hash data doesn't compare MD5 FAIL\n");
return(TRUE);
}
if (BHashSignLen1 != BHashSignLen2)
{
printf("Hash signatures lengths don't match MD5 FAIL\n");
return(TRUE);
}
if (memcmp(pHashSignData1, pHashSignData2, BHashSignLen1) != 0)
{
printf("Hash signature data doesn't compare MD5 FAIL\n");
return(TRUE);
}
if (cArg > 1)
printf("SUCCEED\n");
return(FALSE);
}
BOOL Hash(int cArg, CHAR *bbuf, DWORD bsize, BYTE *pHashOut,
DWORD *pHashLenOut, BYTE *pSigData, DWORD *pdwSigLen, DWORD Algid)
{
if (RCRYPT_FAILED(CryptCreateHash(hMe, Algid, 0, 0, &hHash)))
{
if (cArg > 1)
printf("CryptCreateHash failed = %x\n", GetLastError());
else
printf("CryptCreateHash FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptHashData(hHash, bbuf, bsize, 0)))
{
if (cArg > 1)
printf("CryptUpDataHash failed = %x\n", GetLastError());
else
printf("CryptHashData FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptSignHash(hHash, AT_SIGNATURE, 0, 0, pSigData,
pdwSigLen)))
{
if (cArg > 1)
printf("CryptSignHash failed = %x\n", GetLastError());
else
printf("CryptSignHash FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptDestroyHash(hHash)))
{
if (cArg > 1)
printf("CryptDestroyHash failed = %x\n", GetLastError());
else
printf("CryptDestroyHash FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptCreateHash(hMe, Algid, 0, 0, &hHash)))
{
if (cArg > 1)
printf("CryptCreateHash failed = %x\n", GetLastError());
else
printf("CryptCreateHash FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptHashData(hHash, bbuf, bsize, 0)))
{
if (cArg > 1)
printf("CryptUpDataHash failed = %x\n", GetLastError());
else
printf("CryptHashData FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptGetUserKey(hMe, AT_SIGNATURE, &hKey2)))
{
if (cArg > 1)
printf("cguk failed = %x", GetLastError());
else
printf("CryptGetUserKey FAIL\n");
return (TRUE);
}
if (RCRYPT_FAILED(CryptVerifySignature(hHash, pSigData, *pdwSigLen,
hKey2, 0, 0)))
{
if (cArg > 1)
printf("CryptVerifySignature failed = %x\n", GetLastError());
else
printf("CryptVerifySignature FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptDestroyKey(hKey2)))
{
if (cArg > 1)
printf("CryptDestroyKey failed = %x\n", GetLastError());
else
printf("CryptDestroyKey FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptDestroyHash(hHash)))
{
if (cArg > 1)
printf("CryptDestroyHash failed = %x\n", GetLastError());
else
printf("CryptDestroyHash FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptCreateHash(hMe, Algid, 0, 0, &hHash)))
{
if (cArg > 1)
printf("CryptCreateHash failed = %x\n", GetLastError());
else
printf("CryptCreateHash FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptHashData(hHash, bbuf, bsize, 0)))
{
if (cArg > 1)
printf("CryptUpDataHash failed = %x\n", GetLastError());
else
printf("CryptHashData FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptGetHashParam(hHash, HP_HASHVAL, pHashOut,
pHashLenOut, 0)))
{
if (cArg > 1)
printf("CryptGetHashParam failed = %x\n", GetLastError());
else
printf("CryptGetHashParam FAIL\n");
return(TRUE);
}
if (RCRYPT_FAILED(CryptDestroyHash(hHash)))
{
if (cArg > 1)
printf("CryptDestroyHash failed = %x\n", GetLastError());
else
printf("CryptDestroyHash FAIL\n");
return(TRUE);
}
return(FALSE);
}
BOOL Logon(int cArg)
{
strcpy(pszMyName, "stress");
if (cArg > 1)
printf("CryptAcquireContext ");
if (RCRYPT_FAILED(CryptAcquireContext(&hMe, pszMyName,
MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)))
{
if (cArg > 1)
printf("FAIL Error = %x\n", GetLastError());
return(TRUE);
}
else
{
if (cArg > 1)
printf("SUCCEED\n");
}
if (cArg > 1)
printf("CryptGenKeys ");
if (RCRYPT_FAILED(CryptGenKey(hMe, CALG_RSA_SIGN,
CRYPT_EXPORTABLE, &hClientKey)))
{
if (cArg > 1)
printf("FAIL Error = %x\n", GetLastError());
return(TRUE);
}
CryptDestroyKey(hClientKey);
if (RCRYPT_FAILED(CryptGenKey(hMe, CALG_RSA_KEYX,
CRYPT_EXPORTABLE, &hClientKey)))
{
if (cArg > 1)
printf("FAIL Error = %x\n", GetLastError());
return(TRUE);
}
if (cArg > 1)
printf("SUCCEED\n");
CryptDestroyKey(hClientKey);
return(FALSE);
}
BOOL TEncrypt(HCRYPTKEY hTKey, HCRYPTHASH hTHash, BOOL FinalFlag,
DWORD dwFlags, BYTE *Tbuf, DWORD *pBLen, DWORD pdwBufLen,
int cArg, CHAR *szAlgid, CHAR *szmode)
{
if (cArg > 1)
printf("CryptEncrypt %s-%s ", szAlgid, szmode);
if (RCRYPT_FAILED(CryptEncrypt(hTKey, hTHash, FinalFlag, dwFlags,
Tbuf, pBLen, pdwBufLen)))
{
if (cArg > 1)
printf("failed = %x\n", GetLastError());
else
printf("CryptEncrypt FAIL\n");
return(TRUE);
}
if (cArg > 1)
printf("SUCCEED\n");
return(FALSE);
}
BOOL TDecrypt(HCRYPTKEY hTKey, HCRYPTHASH hTHash, BOOL FinalFlag,
DWORD dwFlags, BYTE *Tbuf, DWORD *pBLen, int cArg,
CHAR *szAlgid, CHAR *szmode)
{
if (cArg > 1)
printf("CryptDecrypt %s-%s ", szAlgid, szmode);
if (RCRYPT_FAILED(CryptDecrypt(hTKey, hTHash, FinalFlag, dwFlags,
Tbuf, pBLen)))
{
if (cArg > 1)
printf("failed = %x\n", GetLastError());
else
printf("CryptDecrypt FAIL\n");
return(TRUE);
}
if (cArg > 1)
printf("SUCCEED\n");
return(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -