Tag: c

C

How to overcome ERROR_INTERNET_INVALID_CA error in C

ERROR_INTERNET_INVALID_CA (12045) – The function is unfamiliar with the Certificate Authority that generated the server’s certificate. //… DWORD invalidcaFlags, invalidcaFlagsLen = sizeof(invalidcaFlags); requestAgain: if (!CLFU(HttpSendRequestA)(request, headers, headersSize, data, dataSize)) { if (GetLastError() == ERROR_INTERNET_INVALID_CA) {   InternetQueryOption(request, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&Flags, &FlagsLen);   Flags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;   InternetSetOption(request, INTERNET_OPTION_SECURITY_FLAGS, &Flags, sizeof(Flags));   goto requestAgain; }   CLFU(InternetCloseHandle)(request); …

C

How to compress a buffer with zlib in C

To build the following code, you first have to link zlib library, zlibstat.lib file, with your project and define ZLIB_WINAPI in your project’s properties -> c/c++ -> preprocessor definitions. Compression.h #ifndef COMPRESSION_H_ #define COMPRESSION_H_   #include <windows.h> #include <zlib.h>   #define GZIP_ENCODING 16 #define CHUNK 0x4096   int CompressBuffer(unsigned char **dest, const unsigned char *src, …

C

Calculate sha256 with OpenSSL and C

To compile the following code you have to link OpenSSL libeay32.lib with your project. To build OpenSSL for Windows please read this post. Includes and Definitions #include <stdlib.h> #include <stdio.h> #include <windows.h> #include <openssl/evp.h>   int HashSHA256(unsigned char *hashed, const unsigned char *plain, size_t plen);#include <stdlib.h> #include <stdio.h> #include <windows.h> #include <openssl/evp.h> int HashSHA256(unsigned char …

C

Simple XOR encryption in C

Dont’ use XOR encryption only when you encrypt data. It’s weak. Combine it with other methods/algorithms. You can for ex. xor a string and then encrypt it with AES 256. Definitions and Includes #include <stdlib.h> #include <stdio.h> #include <windows.h> int XOR(WCHAR **dest, const WCHAR *src, int slen, const WCHAR *key, int klen);#include <stdlib.h> #include <stdio.h> …

C

Reverse a Unicode String in C

Please don’t reverse a string to obsfucate/encrypt it. Combine various methods/algorithms. Reverse it, xor it, encode it using for ex. base64 and encrypt it using a strong encryption algorithm such as AES 256, RSA etc.. Definitions and Includes: #include <stdlib.h> #include <stdio.h> #include <windows.h> #include <stdbool.h>   bool ReverseString(WCHAR **dest, const WCHAR *src, int slen);#include …

C

Base64 with OpenSSL in C

Assuming that you have already built OpenSSL for Windows. Check this guide for details. Link your project with this library from OpenSSL, libeay32.lib. Definitions and Includes: #include <stdlib.h> #include <stdio.h> #include <windows.h> #include <stdbool.h>   #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/buffer.h>   bool Base64Encode(char **dest, const char *src, unsigned int slen); bool Base64Decode(char **dest, const …

C

How to get the UTC offset in C

#include <stdio.h> #include <stdlib.h> #include <time.h>   int GetUTC(){ time_t now = time(NULL); struct tm *lcl = localtime(&now); time_t local = mktime( lcl ); struct tm *gmt = gmtime(&now); time_t utc = mktime( gmt ); return (int)(difftime(local, utc) / 3600); }   int main(void) { printf("UTC offset: %d\n", GetUTC()); return EXIT_SUCCESS; }#include <stdio.h> #include <stdlib.h> …

C

Get installed firewall in C

You will need to include the libraries ole32.lib, oleaut32.lib and wbemuuid.lib into your project. The following code can be compiled in Code::Blocks and Eclipse, no need for Visual Studio. Includes and Definitions: #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <stdbool.h> #include <wbemidl.h>   #define WIN_VISTA 0x0600   CLSID CLSID_WbemLocator2 = {0x4590F811, 0x1D3A, 0x11D0, {0x89, 0x1F, …