在" C"节目我怎么能存储在一个字符串变量十六进制值? [英] In a "C" program how can I store a hexadecimal value in a string variable?

查看:148
本文介绍了在" C"节目我怎么能存储在一个字符串变量十六进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序来输入数据为明文,然后解密使用3DES方法CBC模式的消息。但值是我想提供加密值自己应该被decryted程序硬codeD。我怎样才能做到这一点下面的程序?

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);
}

了解更多: http://blog.fpmurphy.com /2010/04/openssl-des-api.html#ixzz1uqOp1Yhv

推荐答案

我把它完成。
我这样做是在暂且但现在正在开发一种孩子气的方式。我做了这个样子。

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;

后来,我将做这件事情在for循环中。

Later I will do this thing in a for loop.

这篇关于在" C"节目我怎么能存储在一个字符串变量十六进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆