2,640
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <Windows.h>
#include <tchar.h>
#pragma comment(lib, "Crypt32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
BYTE pKey[] = "123";
//BYTE pIV[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
BYTE pData[256] = "abc";
DWORD dwDataLen = 3;
HCRYPTPROV hProv;
if (CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
HCRYPTHASH hHash;
if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
CryptHashData(hHash, pKey, sizeof(pKey), 0);
HCRYPTKEY hKey;
if (CryptDeriveKey(hProv, CALG_DES, hHash, 0x00380000, &hKey))
{
//CryptSetKeyParam(hKey, KP_IV, pIV, 0);
CryptEncrypt(hKey, 0, TRUE, 0, pData, &dwDataLen, _countof(pData));
TCHAR szBase64[256];
DWORD dwBase64Len = sizeof(szBase64) / sizeof(szBase64[0]);
CryptBinaryToString(pData, dwDataLen, CRYPT_STRING_BASE64, szBase64, &dwBase64Len);
_tprintf(szBase64);
CryptDestroyKey(hKey);
}
CryptDestroyHash(hHash);
}
CryptReleaseContext(hProv, 0);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
struct
{
BLOBHEADER hdr;
DWORD cbKeySize;
BYTE rgbKeyData[8];
} keyBlob;
keyBlob.hdr.bType = PLAINTEXTKEYBLOB;
keyBlob.hdr.bVersion = CUR_BLOB_VERSION;
keyBlob.hdr.reserved = 0;
keyBlob.hdr.aiKeyAlg = CALG_DES;
keyBlob.cbKeySize = 8;
BYTE Key[8] = {0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31};
CopyMemory(keyBlob.rgbKeyData, Key, keyBlob.cbKeySize);
BYTE pData[256] = {0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31};
DWORD dwDataLen = 8;
HCRYPTPROV hProv;
if (CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
HCRYPTKEY hKey;
if (CryptImportKey(hProv, (BYTE*)(&keyBlob), sizeof(keyBlob), 0, 0, &hKey))
{
DWORD dwMode = CRYPT_MODE_CBC;
CryptSetKeyParam(hKey, KP_MODE, (BYTE*)(&dwMode), 0);
CryptEncrypt(hKey, 0, TRUE, 0, pData, &dwDataLen, _countof(pData));
for (int i = 0; i != dwDataLen; ++i)
_tprintf(_T("%02x "), pData[i]);
_tprintf(_T("\r\n"));
CryptDestroyKey(hKey);
}
CryptReleaseContext(hProv, 0);
}
return 0;
}
#include <iostream>
#include <Windows.h>
#include <tchar.h>
#pragma comment(lib, "Crypt32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
struct keyBlob
{
BLOBHEADER hdr;
DWORD cbKeySize;
BYTE rgbKeyData[8];
} keyBlob;
keyBlob.hdr.bType = PLAINTEXTKEYBLOB;
keyBlob.hdr.bVersion = CUR_BLOB_VERSION;
keyBlob.hdr.reserved = 0;
keyBlob.hdr.aiKeyAlg = CALG_DES;
keyBlob.cbKeySize = 8;
BYTE Key[8] = {0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31};
CopyMemory(keyBlob.rgbKeyData, Key, keyBlob.cbKeySize);
//BYTE pIV[8] = {0x0d, 0x3a, 0x62, 0xdd, 0xfe, 0xcb, 0x2b, 0xba};
BYTE pData[256] = {0x31};
DWORD dwDataLen = 1;
HCRYPTPROV hProv;
if (CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
HCRYPTKEY hKey;
if (CryptImportKey(hProv, (BYTE*)(&keyBlob), sizeof(keyBlob), 0, CRYPT_EXPORTABLE, &hKey))
{
//CryptSetKeyParam(hKey, KP_IV, pIV, 0);
CryptEncrypt(hKey, 0, TRUE, 0, pData, &dwDataLen, _countof(pData));
for (int i = 0; i != dwDataLen; ++i)
_tprintf(_T("%02x "), pData[i]);
_tprintf(_T("\r\n"));
CryptDestroyKey(hKey);
}
CryptReleaseContext(hProv, 0);
}
return 0;
}
GBool KeyEncryptionAlgorithm::decrypt( Guchar *pdata, Gulong *dataLen, Guchar* key, Gulong keyLen)
{
GBool ok = gFalse;
#ifdef _WIN32
HCRYPTPROV hCryptProv = NULL;
HCRYPTKEY hKey = NULL;
Gulong errcode;
struct {
BLOBHEADER hdr;
Gulong len;
BYTE key[32];
} key_blob;
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
return gFalse;
}
key_blob.hdr.bType = PLAINTEXTKEYBLOB;
key_blob.hdr.bVersion = CUR_BLOB_VERSION;
key_blob.hdr.aiKeyAlg = Algid;
key_blob.hdr.reserved = 0;
key_blob.len = keyLen;
memcpy(key_blob.key, key, keyLen);
if (!CryptImportKey(hCryptProv, (BYTE*)&key_blob, sizeof(key_blob), NULL, 0, &hKey))
{
errcode = GetLastError();
goto err;
}
if (Params)
{
if (Algid == CALG_3DES)
{
if (!CryptSetKeyParam(hKey, KP_IV, Params, 0))
{
errcode = GetLastError();
}
}
}
// Decrypt data.
if(!CryptDecrypt(hKey, NULL, gTrue, 0, pdata, dataLen))
{
errcode = GetLastError();
goto err;
}
ok = gTrue;
err:
if(hKey)
CryptDestroyKey(hKey);
if(hCryptProv)
CryptReleaseContext(hCryptProv, 0);
#else
#endif
return ok;
}
GBool KeyEncryptionAlgorithm::encrypt( Guchar *pdata, Gulong *dataLen, Gulong bufLen, Guchar* key, Gulong keyLen )
{
#ifdef _WIN32
HCRYPTPROV hCryptProv = NULL;
HCRYPTKEY hKey = NULL;
GBool ok = gFalse;
Gulong errcode;
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
return gFalse;
}
if (Algid == CALG_RSA_KEYX)
{
struct {
PUBLICKEYSTRUC hdr;
RSAPUBKEY rsapubkey;
Guchar modulus[130];
} key_blob;
key_blob.hdr.bType = PUBLICKEYBLOB;
key_blob.hdr.bVersion = CUR_BLOB_VERSION;
key_blob.hdr.aiKeyAlg = Algid;
key_blob.hdr.reserved = 0;
key_blob.rsapubkey.bitlen = 1024;
key_blob.rsapubkey.magic = 0x31415352;
Guchar *pubkey = key;
if (!IsSequence(*pubkey++))
goto err;
Gulong len1;
pubkey = getLength(pubkey, &len1);
pubkey = getLittleEndianOctetInteger(pubkey, key_blob.modulus, &len1);
getInteger(pubkey, &key_blob.rsapubkey.pubexp);
if (!CryptImportKey(hCryptProv, (Guchar*)&key_blob, sizeof(key_blob), NULL, 0, &hKey))
{
errcode = GetLastError();
goto err;
}
}
else
{
struct {
BLOBHEADER hdr;
Gulong len;
BYTE key[32];
} key_blob;
key_blob.hdr.bType = PLAINTEXTKEYBLOB;
key_blob.hdr.bVersion = CUR_BLOB_VERSION;
key_blob.hdr.aiKeyAlg = Algid;
key_blob.hdr.reserved = 0;
key_blob.len = keyLen;
memcpy(key_blob.key, key, keyLen);
if (!CryptImportKey(hCryptProv, (BYTE*)&key_blob, sizeof(key_blob), NULL, 0, &hKey))
{
errcode = GetLastError();
goto err;
}
}
if (Params)
{
if (Algid == CALG_3DES)
{
if (!CryptSetKeyParam(hKey, KP_IV, Params, 0))
{
errcode = GetLastError();
}
}
}
if(!CryptEncrypt(hKey, NULL, gTrue, 0, pdata, dataLen, bufLen))
{
errcode = GetLastError();
goto err;
}
ok = gTrue;
err:
if(hKey)
CryptDestroyKey(hKey);
if(hCryptProv)
CryptReleaseContext(hCryptProv, 0);
return ok;
#else
return gFalse;
#endif
}