问题
I have a program which takes the input data as a plaintext and then decrypts the message using 3DES method in CBC mode. But the values are hardcoded in the program I want to provide the encrypted value myself which should be decryted. How can I do this in the following program?
int main(void)
{
unsigned char in[BUFSIZE], out[BUFSIZE], back[BUFSIZE];
unsigned char *e = out;
int len;
DES_cblock key;
DES_cblock seed = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10};
DES_cblock ivsetup = {0xE1, 0xE2, 0xE3, 0xD4, 0xD5, 0xC6, 0xC7, 0xA8};
DES_key_schedule keysched;
DES_cblock ivec;
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
memset(back, 0, sizeof(back));
RAND_seed(seed, sizeof(DES_cblock));
DES_random_key(&key);
DES_set_odd_parity(&key);
if (DES_set_key_checked((C_Block *)key, &keysched))
{
fprintf(stderr, "ERROR: Unable to set key schedule\n");
exit(1);
}
/* 64 bytes of plaintext */
/* From here, encryption starts for the plaintext below. */
strcpy(in, "Now is the time for all men to stand up and be counted");
printf("Plaintext: [%s]\n", in);
len = strlen(in);
memcpy(ivec, ivsetup, sizeof(ivsetup));
DES_ncbc_encrypt(in, out, len, &keysched, &ivec, DES_ENCRYPT);
printf("Ciphertext:");
while (*e) printf(" [%02x]", *e++);
printf("\n");
/* Till here, encryption is over. After this we have to decrypt
* the value which has been encoded, but I want to remove all this
* part and to provide my own encrypted message, and get the
* proper output.
*/
memcpy(ivec, ivsetup, sizeof(ivsetup));
/* The problem I am facing is how to provide the value properly
* to the parameter "out" and "keysched", which should be of my
* choice. For "out" I want to provide THIS value:
* "2DC39619B4450A8C27A3976C50DE5799".
*/
DES_ncbc_encrypt(out, back, len, &keysched, &ivec, DES_DECRYPT);
printf("Decrypted Text: [%s]\n", back);
exit(0);
}
Read more: http://blog.fpmurphy.com/2010/04/openssl-des-api.html#ixzz1uqOp1Yhv
回答1:
Read C FAQ 20.10. Hexadecimal is a representation. All numbers are stored in binary internally. Your DES_cblock
is probably a typedef
for an (unsigned, perhaps!) integral type. So, what you have in effect is an array of integers. You can put numbers in decimal, hexadecimal or binary -- but they will all work. Hexadecimal is typically used in cryptography because it has some notational advantages.
回答2:
I got it done. I did it in a childish way for the time being but it is working now. I did it like this.
out[0]=0xA0; out[1]=0x69; out[2]=0x57; out[3]=0x3B;
out[4]=0x70; out[5]=0x26; out[6]=0x1C; out[7]=0xE8;
out[8]=0xEF; out[9]=0xF2; out[10]=0x9F;out[11]=0x60;
out[12]=0x80;out[13]=0x60;out[14]=0xB2;out[15]=0xE5;
Later I will do this thing in a for loop.
回答3:
Create a dump function like this:
hexdump(char *buff, int len) {
int i,tmp;
for(i=0; i < len; i++) {
tmp = buff[i] & 0xff; /** to avoid sign extension */
printf("%02x",tmp);
}
}
And use it.
hexdump(back,len);
If you have to write it in memory, you can use sprintf, but you may have to write your own binary to hex function.
回答4:
Using the nonstandard itoa
function that stores the value it as a string, you can do the following:
char* hexstr = itoa(back,16);
// print out a string
printf("Decrypted Text: [%X]\n", back);
来源:https://stackoverflow.com/questions/10583023/in-a-c-program-how-can-i-store-a-hexadecimal-value-in-a-string-variable