i want to get the certificate serial number using vc++ code.
HANDLE hStoreHandle;
PCCERT_CONTEXT pCertContext=NULL;
PCERT_PUBLIC_KEY_INFO pOldPubKey = NULL;
char fResponse ='n';
hStoreHandle = CertOpenSystemStore(NULL,"MY");
while(pCertContext= CertEnumCertificatesInStore(hStoreHandle,pCertContext))
{
CString strSubVal,strResult,strInput;
BYTE *pbName=pCertContext->pCertInfo->SerialNumber.pbData;
}
i think the above code having theserial number data but i dont know how to get it in CString format.Guide me
copy paste the below code
#include <stdio.h>//yourDialog.cpp file
#include <windows.h>
#include <Wincrypt.h>
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
#pragma comment(lib, "crypt32.lib")
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
#define KEYLENGTH 0x00800000
PCCERT_CONTEXT pCertContext=NULL;
HANDLE hStoreHandle;
PCERT_PUBLIC_KEY_INFO pOldPubKey = NULL;
char fResponse ='n';
hStoreHandle = CertOpenSystemStore(NULL,"MY");
pCertContext= CertEnumCertificatesInStore(hStoreHandle,pCertContext);
PCERT_INFO pCertifInfo = pCertContext->pCertInfo;
BYTE* pbData = pCertifInfo->SerialNumber.pbData;
DWORD cbData = pCertifInfo->SerialNumber.cbData;
char hex_ascii[3];
CString csAscii;
csAscii.Empty();
if (cbData > 0)
{
int i;
CString cs;
for (i=0; i < cbData; i++)
{
BYTE bb = (BYTE) pbData[i];
sprintf(hex_ascii, "%02X", bb);
cs.Format("%s", hex_ascii);
csAscii = cs + csAscii ;
}
}
Not sure if you want this: the code below puts the data into a hexadecimal string (theString)
BYTE *pbName=pCertContext->pCertInfo->SerialNumber.pbData;
CString theString ;
for (int i = 0; i < pCertContext->pCertInfo->SerialNumber.cbData; i++)
{
CString hex ;
hex.Format("%02x", pbName[i]) ;
theString += hex ;
}
The code above will give you the Serial Number 'in reverse'. You can confirm this by viewing the serial number in the certificate, by using the 'certmgr.msc' tool.
To get the serial number in the 'correct' order, just change the direction of the for loop. Below is the modified version of the code given above:
BYTE *pbName=pCertContext->pCertInfo->SerialNumber.pbData;
CString theString ;
for (int i = pCertContext->pCertInfo->SerialNumber.cbData - 1; i >= 0 ; i--)
{
CString hex ;
hex.Format("%02x", pbName[i]) ;
theString += hex ;
}
Related
I have written two similar C programs. How can I make the outputs of both code same by editing one of the ELF files not the actual code?
/**
* prg1.c
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 5;
int b = 10;
int sum;
sum = a + b;
printf("sum is %d\n", sum);
return(0);
}
/**
* prg2.c
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 5;
int b = 20;
int sum;
sum = a + b;
printf("sum is %d\n", sum);
return(0);
}
In your second program's elf file find the occurrence of 20 and change it to 10.
To do that you can do something like this -
Find 14 (hex of 20) in your elf file and change it to A and making sure length is same by adding extra 0.
To do this you can use any elf editor, I use 'Hex Fiend' for mac.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n;
printf("Length? ");
scanf("%d", &n);
getchar();
char* str = (char*)malloc(sizeof(char) * (n+1));
fgets(str,sizeof(str),stdin);
for (int i = 0; i < n; i++)
printf("%c\n", str[i]);
free(str);
}
Process results like this!
Length? 5
abcde
a
b
c
?
(I wanted to upload the result image, but I got rejected since I didn't have 10 reputations)
I can't figure out why 'd' and 'e' won't be showing in the results.
What is the problem with my code??
(wellcome to stackoverflow :) (update #1)
str is a pointer to char instead of a character array therefore sizeof(str) is always 8 on 64-bit or 4 on 32-bit machines, no matter how much space you have allocated.
Demo (compilation succeeds only if X in static_assert(X) holds):
#include <assert.h>
#include <stdlib.h>
int main(void){
// Pointer to char
char *str=(char*)malloc(1024);
#if defined _WIN64 || defined __x86_64__ || defined _____LP64_____
static_assert(sizeof(str)==8);
#else
static_assert(sizeof(str)==4);
#endif
free(str);
// Character array
char arr[1024];
static_assert(sizeof(arr)==1024);
return 0;
}
fgets(char *str, int num, FILE *stream) reads until (num-1) characters have been read
Instead of fgets(str,sizeof(str),stdin) please fgets(str,n+1,stdin)
Fixed version:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int main(void){
int n=0;
printf("Length? ");
scanf("%d",&n);
getchar();
char *str=(char*)calloc((n+1),sizeof(char));
static_assert(
sizeof(str)==sizeof(char*) && (
sizeof(str)==4 || // 32-bit machine
sizeof(str)==8 // 64-bit machine
)
);
fgets(str,n+1,stdin);
for(int i=0;i<n;++i)
printf("%c\n",str[i]);
free(str);
str=NULL;
}
Length? 5
abcde
a
b
c
d
e
I'm currently doing the CS50 Harvard Course and I'm stuck in problem set 2.
I made this program that takes a name and prints the initials, it takes the name in the command line. How can I use get_string() instead of argv, and argc wich is very unorthodox and sloppy, so I can prompt the user to give me her/his name. Thank you.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(int argc, string argv[])
{
//How do I use Get_string() so I don't have to use argv and argc??
//iterate over strings on the vector (words)
for (int i = 0; i < argc; i++)
{
//prints the 0 character of each string, use "toupper" to convert into capital letters
printf("%c", toupper(argv[i][0]));
}
printf("\n");
}
use Array,
let's say for 10 names
string name[10];
for (int i = 0; i < 10; i++)
{
name[i] = get_string("Enter your name: /n");
}
Do you know if exist one program or method to see (secuences of)bytes from a text,html file?
Not to see characters, rather see the complete sequence of bytes.
recommendations?
yes, it is called hex editor... Hundreds of those exist out there.
Here are some: http://en.wikipedia.org/wiki/Comparison_of_hex_editors
A common hex editor allows you to view any file's byte sequence.
If you just want to see the existing bytes (without changing them) you can use a hex-dump program, which is much smaller and simpler than a hex editor. For example, here's one I wrote several years ago:
/* public domain by Jerry Coffin
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
unsigned long offset = 0;
FILE *input;
int bytes, i, j;
unsigned char buffer[16];
char outbuffer[60];
if ( argc < 2 ) {
fprintf(stderr, "\nUsage: dump filename [filename...]");
return EXIT_FAILURE;
}
for (j=1;j<argc; ++j) {
if ( NULL ==(input=fopen(argv[j], "rb")))
continue;
printf("\n%s:\n", argv[j]);
while (0 < (bytes=fread(buffer, 1, 16, input))) {
sprintf(outbuffer, "%8.8lx: ", offset+=16);
for (i=0;i<bytes;i++) {
sprintf(outbuffer+10+3*i, "%2.2X ",buffer[i]);
if (!isprint(buffer[i]))
buffer[i] = '.';
}
printf("%-60s %*.*s\n", outbuffer, bytes, bytes, buffer);
}
fclose(input);
}
return 0;
}
I use
size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
to convert UTF-16BE to GB2312.
inbytesleft is bytes number to be convert. After conversion, inbytesleft is bytes number of not converted.
After one call, I found inbytesleft is -2, according to iconv man page this function should read at most inbytesleft.
Who can tell my why and how to fix this?
code to be convert is
"保单验证"
Thanks
How are you getting the input data into your program?
I've tested the situation using this code and it seems to work:
#include <stdio.h>
#include <iconv.h>
#include <errno.h>
int main(){
char data[10] = {0x4f,0xdd,0x53,0x55,0x9a,0x8c,0x8b,0xc1, 0, 0};
char outdata[20];
char *dataptr;
char *outdataptr;
iconv_t cd;
size_t result;
size_t inbytesleft = 8;
size_t outbytesleft = 20;
int i;
cd = iconv_open("GB2312", "UTF-16BE");
dataptr = data;
outdataptr = outdata;
result = iconv(cd, &dataptr, &inbytesleft, &outdataptr, &outbytesleft);
if(result == -1)
printf("Error: %d\n", errno);
printf(" result: %zd\n", result);
printf(" inbytesleft: %zd\n", inbytesleft);
printf("outbytesleft: %zd\n", outbytesleft);
for(i = 20; i > outbytesleft; i--){
if(i != 20)
printf(",");
printf("0x%02x", *((unsigned char *)&(outdata[20-i])));
}
printf("\n");
return 0;
}
It prints
result: 0
inbytesleft: 0
outbytesleft: 12
0xb1,0xa3,0xb5,0xa5,0xd1,0xe9,0xd6,0xa4
Which appears to be correct.
The array of items in the variable data is the UTF-16BE encoding of 保单验证
If this doesn't help, could you post your code for analysis?