我需要一些帮助来将C中的字符解码为UTF8格式。
到目前为止,我的代码是这样工作的:
Achar
用十六进制表示的“password”70617373776F7264
初始化
然后使用3DESDD201F609E49C0609FABA4C8AAFBB1E5
然后使用3DES70617373776F72640808080808080808
在printf("decrypted: %s",dec)
语句中,所有内容看起来都很好,并显示为decrypted: password
但是,在进行字符串比较时,它不匹配。仔细观察这个字符,我可以看到它是以\001password\010\010\010\010\010\010\010\010
的形式出现的(这是由于填充造成的)
有什么方法可以取消pad或者解码成UTF8吗?something similar to this
使用代码编辑:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/des.h>
#include <openssl/rand.h>
#include <ldap.h>
#include "k.h"
#include "hex.h"
#define ULONG unsigned long
#define INT unsigned int
char *encrypt(char *data)
{
int i = 0;
int len = 0;
int nlen = 0;
char *key1 = "1313232323231313";
char *key2 = "6789678967896789";
char *key3 = "1313232323231313";
/* Padding */
char ch = '\0';
unsigned char out[64] = {0};
unsigned char src[64] = {0};
unsigned char *ptr = NULL;
unsigned char block[8] = {0};
DES_key_schedule ks1, ks2, ks3;
/* set password table */
ptr = hex2bin(key1, strlen(key1), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((C_Block *)block, &ks1);
ptr = hex2bin(key2, strlen(key2), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((C_Block *)block, &ks2);
ptr = hex2bin(key3, strlen(key3), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((C_Block *)block, &ks3);
ptr = hex2bin(data, strlen(data), &nlen);
memcpy(src, ptr, nlen);
free(ptr);
len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;
ch = 8 - nlen % 8;
memset(src + nlen, ch, (8 - nlen % 8) % 8);
printf("Raw data: ");
for (i = 0; i < len; i++) {
printf("%02X", *(src + i));
}
printf("\n");
for (i = 0; i < len; i += 8) {
DES_ecb3_encrypt((C_Block *)(src + i), (C_Block *)(out + i), &ks1, &ks2, &ks3, DES_ENCRYPT);
}
printf("Encrypted: ");
for (i = 0; i < len; i++) {
printf("%02X" , *(out + i));
}
printf("\n");
return out;
}
char *decrypt(char *data)
{
int i = 0;
int len = 0;
int nlen = 0;
char *key1 = "1313232323231313";
char *key2 = "6789678967896789";
char *key3 = "1313232323231313";
/* Padding */
int ch = 0;
unsigned char out[64] = {0};
unsigned char src[64] = {0};
unsigned char *ptr = NULL;
unsigned char block[8] = {0};
DES_key_schedule ks1, ks2, ks3;
/* set password table */
ptr = hex2bin(key1, strlen(key1), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((C_Block *)block, &ks1);
ptr = hex2bin(key2, strlen(key2), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((C_Block *)block, &ks2);
ptr = hex2bin(key3, strlen(key3), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((C_Block *)block, &ks3);
ptr = hex2bin(data, strlen(data), &nlen);
memcpy(src, ptr, nlen);
free(ptr);
len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;
ch = 8 - nlen % 8;
memset(src + nlen, ch, (8 - nlen % 8) % 8);
printf("Raw data: ");
for (i = 0; i < len; i++) {
printf("%02X", *(src + i));
}
printf("\n");
for (i = 0; i < len; i += 8) {
DES_ecb3_encrypt((C_Block *)(src + i), (C_Block *)(out + i), &ks1, &ks2, &ks3, DES_DECRYPT);
}
printf("Decrypted: ");
for (i = 0; i < len; i++) {
printf("%02X", *(out + i));
}
printf("\n");
return out;
}
K DES_ecb3_do(K user, K pass,K fl )
{
int res = 0;
int flag = fl->i;
char *usn = user->s;
char *enc = pass->s;
char *decr = "";
char dec[32];
if(flag==1)
{
decr = encrypt(enc);
strcat(dec,decr);
}
else if(flag==0)
{
decr = decrypt(enc);
strcat(dec,decr);
}
int ret;
ret = strcmp(dec, "password");
if(ret==0)
{
printf("they match");
}
else
{
printf("they don't match\n");
return (K) 0;
}
printf("decrypted pass is:%s\n",dec);
return ks(dec);
}
最佳答案
在decrypt
函数中,从调用方作用域之外的局部变量返回解密结果。
将out[64]
更改为static
或全局更改,或将其更改为malloc
。
最后,您的代码调用UB是因为您返回了堆栈区域的地址。
操作评论请求后编辑
你的代码是
char *decrypt(char *data)
{
int i = 0;
int len = 0;
int nlen = 0;
char *key1 = "1313232323231313";
char *key2 = "6789678967896789";
char *key3 = "1313232323231313";
/* Padding */
int ch = 0;
unsigned char out[64] = {0};
unsigned char src[64] = {0};
// stuff....
return out;
}
此代码将数组声明为本地数组,否则为堆栈分配。变量仅在本地范围内可用:在
decrypt
函数内。这意味着当函数返回到调用函数
DES_ecb3_do
时,可能会损坏内存地址。你不应该访问那个地址。要解决此问题,您必须使
out
变量在decrypt
函数作用域之外可用,例如,使用以下方法之一:1)您将
out
定义为global
:unsigned char out[64] = {0};
char *decrypt(char *data)
{
// STUFF
}
2)在函数中定义
out
为static
:char *decrypt(char *data)
{
static unsigned char out[64] = {0};
// STUFF
}
3)您将
out
定义为一个指针,并将其定义为:char *decrypt(char *data)
{
unsigned char *out] = malloc(64);
// STUFF
}
希望足够清楚。