【发布时间】:2012-05-14 12:08:52
【问题描述】:
我有一个程序,它将输入数据作为明文,然后在 CBC 模式下使用 3DES 方法解密消息。但是这些值在程序中是硬编码的,我想自己提供应该解密的加密值。如何在以下程序中做到这一点?
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
【问题讨论】:
-
你只想打印值吗?
-
没有。负责加密和解密的函数“DES_ncbc_encrypt”是一个库函数。我只需要将值正确地传递给函数。做不到。
-
我使用 %s 打印了一次 "out" 的值,并通过迭代每个地址在 while 循环中打印一次,然后使用 %x 再次显示。我得到以下输出: Key= ffbfe7f0 Plaintext: [qwertyuiop[]asdf] 明文长度:16 ivec 加密前:ttxA 0 \ J d Ciphertext 1 : S ? j A D. [53] [96] [19] [fb] [ce] [6a] [f8] [87] [d3] [41] [df] [ed] [44] [2e] [8c] [ c5] 密文 2 : ffbfea00 ivec 解密前: ttxA 0 \ J d 解密文本: [qwertyuiop[]asdf]
-
为了进行三重 DES 加密/解密,我使用了 openssl 库函数,例如 des_ncbc_encrypt(),它使用 16 个字符的密钥和 16 个字符的向量并实现单个 DES。我可以调用它 3 次并传递参数值并使其作为三重 DES 工作。我正在做的是删除与随机化相关的功能,并以这种方式使用我自己的密钥和向量的硬编码值进行加密:
-
要进行 3DES 加密/解密,我正在使用 openssl 库函数,例如 des_ncbc_encrypt(),它使用一个密钥和一个 16 个字符的向量并实现单个 DES。我可以调用它 3 次并传递参数值并使其像 3DES 一样工作。我正在做的是我以这种方式使用自己的密钥和向量的硬编码值进行加密: if (DES_set_key_checked((C_Block *)key, &keysched)) { fprintf(stderr, "ERROR1: Unable to set key计划\n");退出(1); } DES_ncbc_encrypt(out, back, len, &keysched, &ivec2,DES_DECRYPT);
标签: c encryption des