Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *****************************
- <original text>
- 6b c1 be e2 2e 40 9f 96 e9 3d 7e 11 73 93 17 2a
- </original text>
- <encrypted>
- 76 49 ab ac 81 19 b2 46 ce e9 8e 9b 12 e9 19 7d
- 89 64 e0 b1 49 c1 0b 7b 68 2e 6e 39 aa eb 73 1c
- </encrypted>
- <plain text>
- 6b c1 be e2 2e 40 9f 96 e9 3d 7e 11 73 93 17 2a
- </plain text>
- *****************************
- <original text>
- ae 2d 8a 57 1e 03 ac 9c 9e b7 6f ac 45 af 8e 51
- </original text>
- <encrypted>
- 50 86 cb 9b 50 72 19 ee 95 db 11 3a 91 76 78 b2
- 55 e2 1d 71 00 b9 88 ff ec 32 fe ea fa f2 35 38
- </encrypted>
- <plain text>
- ae 2d 8a 57 1e 03 ac 9c 9e b7 6f ac 45 af 8e 51
- </plain text>
- void DumpBytes(const BYTE * pb, ULONG cb, PCSTR msg)
- {
- PSTR pszString = 0;
- DWORD cchString = 0;
- while (CryptBinaryToStringA(pb, cb, CRYPT_STRING_HEX, pszString, &cchString))
- {
- if (pszString)
- {
- DbgPrint("<%s>\n", msg);
- do
- {
- DbgPrint("%.*s", cb = min(0x100, cchString), pszString);
- } while (pszString += cb, cchString -= cb);
- DbgPrint("</%s>\n", msg);
- return;
- }
- pszString = (PSTR)alloca(cchString);
- }
- }
- void aes_dec(const void* pvBlob, ULONG cbBlob, const BYTE* Iv, PBYTE pbEnc, ULONG cbEnc)
- {
- HCRYPTPROV hProv;
- if (CryptAcquireContextW(&hProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
- {
- HCRYPTKEY hKey;
- if (CryptImportKey(hProv, (PBYTE)pvBlob, cbBlob, 0, CRYPT_EXPORTABLE, &hKey))
- {
- DWORD dwAesMode = CRYPT_MODE_CBC;
- if (CryptSetKeyParam(hKey, KP_MODE, (PBYTE)&dwAesMode, 0) &&
- CryptSetKeyParam(hKey, KP_IV, Iv, 0) &&
- CryptDecrypt(hKey, 0, TRUE, 0, pbEnc, &cbEnc))
- {
- DumpBytes(pbEnc, cbEnc, "plain text");
- }
- CryptDestroyKey(hKey);
- }
- CryptReleaseContext(hProv, 0);
- }
- }
- void aes_enc(const void* pvBlob, ULONG cbBlob, const BYTE* Iv, const void* pvTxt, ULONG cbTxt)
- {
- DbgPrint("*****************************\n");
- DumpBytes((PBYTE)pvTxt, cbTxt, "original text");
- HCRYPTPROV hProv;
- if (CryptAcquireContextW(&hProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
- {
- HCRYPTKEY hKey;
- if (CryptImportKey(hProv, (PBYTE)pvBlob, cbBlob, 0, CRYPT_EXPORTABLE, &hKey))
- {
- DWORD dwAesMode = CRYPT_MODE_CBC;
- if (CryptSetKeyParam(hKey, KP_MODE, (PBYTE)&dwAesMode, 0) &&
- CryptSetKeyParam(hKey, KP_IV, Iv, 0))
- {
- BYTE* pbData = 0;
- ULONG dwDataLen, dwBufLen = 0;
- while (CryptEncrypt(hKey, 0, TRUE, 0, pbData, &(dwDataLen = cbTxt), dwBufLen))
- {
- if (pbData)
- {
- DumpBytes(pbData, dwDataLen, "encrypted");
- aes_dec(pvBlob, cbBlob, Iv, pbData, dwDataLen);
- break;
- }
- memcpy(pbData = (PBYTE)alloca(dwBufLen = dwDataLen), pvTxt, cbTxt);
- }
- }
- CryptDestroyKey(hKey);
- }
- CryptReleaseContext(hProv, 0);
- }
- }
- void aes_demo()
- {
- struct CRYPTOAPI_AES128_KEYBLOB : BLOBHEADER {
- DWORD dwKeySize;
- BYTE ucKeyBytes[16];
- };
- static const CRYPTOAPI_AES128_KEYBLOB blob = {
- { PLAINTEXTKEYBLOB, CUR_BLOB_VERSION, 0, CALG_AES_128 }, sizeof(blob.ucKeyBytes),
- { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }
- };
- static const BYTE ucAes128Test1Iv[] = {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
- };
- static const BYTE ucAes128Test2Iv[] = {
- 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d
- };
- static const BYTE ucAes128Test1Ptx[] = {
- 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
- };
- static const BYTE ucAes128Test2Ptx[] = {
- 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
- };
- aes_enc(&blob, sizeof(blob), ucAes128Test1Iv, ucAes128Test1Ptx, sizeof(ucAes128Test1Ptx));
- aes_enc(&blob, sizeof(blob), ucAes128Test2Iv, ucAes128Test2Ptx, sizeof(ucAes128Test2Ptx));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement