Click here to Skip to main content
16,022,190 members

Comments by Member 14511380 (Top 5 by date)

Member 14511380 30-Sep-24 1:10am View    
Hi @OriginalGriff, I tried this, but I am stuck in this step
wget -O gn http://storage.googleapis.com/chromium-gn/<gn.sha1>

Please let me know the correct procedure to get the SHA1. I tried using my personal gmail id at the mentioned link
https://chromium.googlesource.com/chromium/buildtools/+/master/linux64/gn.sha1
But I got the "Requested entity cannot be found error"
Member 14511380 25-Sep-24 7:59am View    
This is entirely done on ubuntu 24.04
Member 14511380 26-Jun-19 5:14am View    
If you have Expert grade code for the same problem discussed above, please share the same, if permitted that is., while I continue to debug the issue.
Thanks.
Member 14511380 26-Jun-19 2:37am View    
Deleted
return 1;
}

LocalFree(pbBuffer);
pbBuffer = (LPBYTE) LocalAlloc(0, dwBufferLen);
if (!CryptStringToBinaryA(pemPublicKey, 0, CRYPT_STRING_BASE64HEADER, pbBuffer, &dwBufferLen, NULL, NULL))
{
printf("Failed to convert BASE64 public key. Error 0x%.8X\n", GetLastError());
return 1;
}

if (!CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, RSA_CSP_PUBLICKEYBLOB, pbBuffer, dwBufferLen, 0, NULL, NULL, &cbKeyBlob))
{
printf("Failed to parse public key. Error 0x%.8X\n", GetLastError());
return 1;
}

LocalFree(pbKeyBlob);
pbKeyBlob = (LPBYTE) LocalAlloc(0, cbKeyBlob);
if (!CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, RSA_CSP_PUBLICKEYBLOB, pbBuffer, dwBufferLen, 0, NULL, pbKeyBlob, &cbKeyBlob))
{
printf("Failed to parse public key. Error 0x%.8X\n", GetLastError());
return 1;
}

HCRYPTKEY key = NULL;
if (!CryptImportKey(hProv, pbKeyBlob, cbKeyBlob, 0, 0, &key)) {
printf("Failed to import public key. Error 0x%.8X\n", GetLastError());
return 1;
}

DWORD dwKeySize;
DWORD dwParamSize = sizeof(DWORD);
CryptGetKeyParam(key, KP_KEYLEN, (BYTE*) &dwKeySize, &dwParamSize, 0);

dwKeySize = dwKeySize / 8;

BYTE *data = new BYTE[dwKeySize];
DWORD dataLen = 16;
memcpy(data, "0123456789012345", dataLen);
CryptEncrypt(key, 0, TRUE, 0, data, &dataLen, dwKeySize);
// now convert it to big endian (for the rest of the world)
for (int i = 0; i < (dwKeySize / 2); i++) {
BYTE c = data[i];
data[i] = data[dwKeySize - 1 - i];
data[dwKeySize - 1 - i] = c;
}

CloseHandle(hOutFile);
// return 0;

HCRYPTHASH hHash;
if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash)){
dwStatus = GetLastError();
// // RETAILMSG(1, (L"CryptCreateHash failed: %x\n", dwStatus));
CryptReleaseContext(hProv, 0);
return dwStatus;
}

if (!CryptHashData(hHash, (BYTE*)default_key, len, 0)) {
DWORD err = GetLastError();
// // RETAILMSG(1, (L"CryptHashData failed: %x\n", err));
return (-1);
}
// // RETAILMSG(1, (L"CryptHashData Success\n"));

HCRYPTKEY hKey;
if (!CryptDeriveKey(hProv, CALG_AES_256, hHash, 0,&hKey)){
dwStatus = GetLastError();
// // RETAILMSG(1, (L"CryptDeriveKey failed: %x\n", dwStatus));
CryptReleaseContext(hProv, 0);
return dwStatus;
}
// // RETAILMSG(1, (L"CryptDeriveKey Success\n"));

const size_t chunk_size = BLOCK_LEN;
BYTE chunk[chunk_size] = { 0 };
DWORD out_len;
BOOL isFinal = FALSE;
DWORD readTotalSize = 0;
DWORD inputSize = GetFileSize(hInpFile, NULL);

CloseHandle(hOutFile);

while (bResult = ReadFile(hInpFile, chunk, chunk_size, &out_len, NULL)) {
if (0 == out_len){
break;
}
readTotalSize += out_len;
if (readTotalSize == inputSize) {
isFinal = TRUE;
}

if (!CryptEncrypt(hKey, NULL, isFinal, 0, chunk, &out_len, chunk_size)) {
// // RETAILMSG(1, (L"CryptEncrypt failed\n"));
break;
}

written = 0;
if (!WriteFile(hOutFile, chunk, out_len, &written, NULL)) {
// // RETAILMSG(1, (L"writing failed!\n"));
break;
}
memset(chunk, 0, chunk_size);
}

CryptReleaseContext(hProv, 0);
CryptDestroyKey(hKey);
CryptDestroyHash(hHash);
CloseHandle(hInpFile);
CloseHandle(hOutFile);
// // RETAILMSG(1, (L"Crypting HP.LOG successfully completed\n"));
return 0;
}

int wmain( int argc, wchar_t *argv[])
{
int a = Encrypt();
printf("Finished. Processed");
system("pause");
return 0;
}
Member 14511380 26-Jun-19 2:31am View    
Deleted
Hey OriginalGriff, Thanks for your comments
I am new to Encryption. However I understand the basic concepts.
I need help urgently. I got the below code from my predecessor and he isn't available as of now.

What I can understand from the code is the following steps
1. Generate the IV and the AES key

2. Use the Public key to encrypt the AES key (Assymetric). Thats what we achieve with the below call, and it works fine
CryptEncrypt(key, 0, TRUE, 0, data, &dataLen, dwKeySize);

3. After that the plain text file hInpFile is read in chunks of 256 bytes and encrypted using the AES key. We are trying to achieve that with the below call
CryptEncrypt(hKeyTest, NULL, isFinal, 0, chunk, &out_len, chunk_size)
This call fails and when I debug out the error code, it returns Error 87: Incorrect parameter.


I have pasted the code below.


// CryptAES.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#pragma comment(lib, "crypt32.lib")
using namespace std;
typedef unsigned char BYTE;

#define BLOCK_LEN 256

const char* pemPublicKey =
"-----BEGIN RSA PUBLIC KEY-----"
"MIIBCgKCAQEAv1uJqBCBeN0dJHBAbNeA"
"ycFAlFVsfc1l+SOBeAvriayK0VcNu7xF8BSu0s2ZFTUxmAnS0hArceHI8MzX/H1L"
"whG4sOTqsIYxzuDDQWqR8/zAg1UBjGqyfh93fLAwiGe5Tq+w2EjqCaVpXnFulOp1"
"Xfy3Mfbh//RMlF5P+HlmtriNqebOJl/FMUoh3nd1VAOkmaWmQR+hxXdOtnLyphsU"
"oPqw/NHr9w5iNvNArKeRFAvUai+Jq3kQ3zpab0v5YZpj7aEGPZ9uVBgqtzmCVhHv"
"5RgYcLl0sqPcOoUimqCf4yBnBpJa+HUnJ7rznH0NTpnYFqE9Hu9BD9v4BLKembns"
"RQIDAQAB"
"-----END RSA PUBLIC KEY-----";

int Encrypt() {
printf("Starting main");

char filename1[] = "D:\\Security\\HPLogInput.zip"; //please create the directory and file for testing
char filename2[] = "D:\\Security\\HPLOG.jdb";
//char *key_str = default_key;
wchar_t wtext1[100];
wchar_t wtext2[100];
wchar_t info[] = L"Microsoft Enhanced RSA and AES Cryptographic Provider";
BOOL isDecrypt = FALSE;

//size_t len = strlen(key_str);
size_t len = 0; // strlen(default_key);
mbstowcs(wtext1, filename1, strlen(filename1)+1);
LPCWSTR sourcepath = wtext1;
HANDLE hInpFile = CreateFileW(sourcepath, GENERIC_READ,FILE_SHARE_READ, NULL,OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);

if (hInpFile == INVALID_HANDLE_VALUE) {
// RETAILMSG(1, (L"Cannot open input file!\n"));
return (-1);
}

mbstowcs(wtext2, filename2, strlen(filename2)+1);
LPCWSTR destPath = wtext2;
HANDLE hOutFile = CreateFileW(destPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

if (hOutFile == INVALID_HANDLE_VALUE) {
// RETAILMSG(1, (L"Cannot open input file!\n"));
return (-1);
}

DWORD written = 0;
// To write header
WriteFile(hOutFile, "JDP1\0\0\0\0\0\0\0\0\0\0\0\0", 16, &written, NULL);

DWORD dwStatus = 0;
BOOL bResult = FALSE;

HCRYPTPROV hProv;
if (!CryptAcquireContextW(&hProv, NULL, info, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)){
dwStatus = GetLastError();
// RETAILMSG(1, (L"CryptAcquireContext failed: %x\n", dwStatus));
CryptReleaseContext(hProv, 0);
return dwStatus;
}

// Get 16 bytes of random data for IV
char iv[16];
CryptGenRandom(hProv, 16, (BYTE*)&iv);
WriteFile(hOutFile, iv, 16, NULL, NULL);

// Get 256 bytes of random data for the symmetric key
char aesKey[256];
CryptGenRandom(hProv, 256, (BYTE*)&aesKey);
WriteFile(hOutFile, aesKey, 256, NULL, NULL);

// Import the PEM public key
DWORD dwBufferLen = 0, cbKeyBlob = 0;
LPBYTE pbBuffer = NULL, pbKeyBlob = NULL;
if (!CryptStringToBinaryA(pemPublicKey, 0, CRYPT_STRING_BASE64HEADER, NULL, &dwBufferLen, NULL, NULL))
{
printf("Failed to convert BASE64 public key. Error 0x%.8X\n", GetLastError());