求个C/C++的 3des ecb PKCS5Padding code.

Freeze_Z 2012-06-25 03:43:34
求个C/C++的 3des ecb PKCS5Padding code.
数据请先用以下数据验证下.

data : 450521198506130531$13670267607$100175034231$33526005
key : 030153561100123456788
加密后: 30DDB9272A8C524FB3493E90D2C1F986578F393D60024A3ACE763F577BB99A05294EE28CE58483AEDFE7AC628D0AE9382FACADB97DB8E70E

可基于openssl.



...全文
584 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamnxh1 2013-05-13
  • 打赏
  • 举报
回复
data,key 是string类型么,还是16进制呢
iamnxh1 2013-05-13
  • 打赏
  • 举报
回复
请问 data : 450521198506130531$13670267607$100175034231$33526005 key : 030153561100123456788 对应的java是什么
Freeze_Z 2012-06-26
  • 打赏
  • 举报
回复
在线等答案.
java解密.
可是加密要用C.
Freeze_Z 2012-06-26
  • 打赏
  • 举报
回复
原来是key补全方式不匹配.

3des的注意的地方.
1.mode <ecb, cbc其他?>
1.data补齐方式. (java一般使用DESede/ECB/PKCS5Padding. 用其他语言实现与java对接时, 需依次.)
2.key补齐方式.(一般会具体规定. 补0或者 字符'0').

此次出现不匹配是因为规定用字符'0', 但是C code中却使用 0 补齐.




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/des.h>



/************************************************************************
** 本例采用:
** 3des-ecb加密方式;
** 24位密钥,不足24位的右补'0';
** 加密内容8位补齐,补齐方式为:少1位补一个0x01,少2位补两个0x02,...
** 本身已8位对齐的,后面补八个0x08。
************************************************************************/
int xxx_3des_encrypt(const char* datain, char* dataout, const unsigned char* keyin, int keyin_len)
{
int docontinue = 1;

int data_len;
int data_rest;
unsigned char ch;

unsigned char *src = NULL; /* 补齐后的明文 */
unsigned char *dst = NULL; /* 解密后的明文 */
int len;
unsigned char tmp[8];
unsigned char in[8];
unsigned char out[8];

int key_len;
#define LEN_OF_KEY 24
unsigned char key[LEN_OF_KEY]; /* 补齐后的密钥 */
unsigned char block_key[9];
DES_key_schedule ks,ks2,ks3;

/* 构造补齐后的密钥 */
key_len = strlen((const char*)keyin);
memcpy(key, keyin, key_len);
memset(key + key_len, '0', LEN_OF_KEY - key_len);

/* 分析补齐明文所需空间及补齐填充数据 */
data_len = strlen(datain);
data_rest = data_len % 8;
len = data_len + (8 - data_rest);
ch = 8 - data_rest;

src = malloc(len);
dst = malloc(len);
if (NULL == src || NULL == dst)
{
docontinue = 0;
}
if (docontinue)
{
int count;
int i;

/* 构造补齐后的加密内容 */
memset(src, 0, len);
memcpy(src, datain, data_len);
memset(src + data_len, ch, 8 - data_rest);

/* 密钥置换 */
memset(block_key, 0, sizeof(block_key));
memcpy(block_key, key + 0, 8);
DES_set_key_unchecked((const_DES_cblock*)block_key, &ks);
memcpy(block_key, key + 8, 8);
DES_set_key_unchecked((const_DES_cblock*)block_key, &ks2);
memcpy(block_key, key + 16, 8);
DES_set_key_unchecked((const_DES_cblock*)block_key, &ks3);

/* 循环加密/解密,每8字节一次 */
count = len / 8;
for (i = 0; i < count; i++)
{
memset(tmp, 0, 8);
memset(in, 0, 8);
memset(out, 0, 8);
memcpy(tmp, src + 8 * i, 8);

/* 加密 */
DES_ecb3_encrypt((const_DES_cblock*)tmp, (DES_cblock*)(dst+8*i), &ks, &ks2, &ks3, DES_ENCRYPT);

}

}

int j = 0;
for(j=0; j<len; j++)
{
snprintf(dataout+ j*2, 3, "%02X", dst[j]);
}

if (NULL != src)
{
free(src);
src = NULL;
}
if (NULL != dst)
{
free(dst);
dst = NULL;
}

return 0;
}
qiang81020 2012-06-25
  • 打赏
  • 举报
回复
解不出来,搞不懂。。。
Freeze_Z 2012-06-25
  • 打赏
  • 举报
回复
必须自顶下先.
在网上找的基于openssl的和一个自行实现的.得到的结果都不符合.
LibTomCrypt is a fairly comprehensive, modular and portable cryptographic toolkit that provides developers with a vast array of well known published block ciphers, one-way hash functions, chaining modes, pseudo-random number generators, public key cryptography and a plethora of other routines. LibTomCrypt has been designed from the ground up to be very simple to use. It has a modular and standard API that allows new ciphers, hashes and PRNGs to be added or removed without change to the overall end application. It features easy to use functions and a complete user manual which has many source snippet examples. LibTomCrypt is free for all purposes under the public domain. This includes commercial use, redistribution and even branching. Sports the following Public domain and open source. Written entirely in portable ISO C source (except for things like RNGs for natural reasons) Builds out of the box on virtually every box. All that is required is GCC for the source to build. Includes a 180+ page user manual in PDF format (with working examples in it) Block Ciphers Ciphers come with an ECB encrypt/decrypt, setkey and self-test interfaces. All ciphers have the same prototype which facilitates using multiple ciphers at runtime. Some of the ciphers are flexible in terms of code size and memory usage. Ciphers Supported. Blowfish XTEA RC5 RC6 SAFER+ Rijndael (aka AES) Twofish SAFER (K64, SK64, K128, SK128) RC2 DES, 3DES CAST5 Noekeon Skipjack Anubis (with optional tweak as proposed by the developers) Khazad KASUMI SEED Chaining Modes Modes come with a start, encrypt/decrypt and set/get IV interfaces. Mode supported. ECB CBC OFB CFB CTR IEEE LRW mode F8 Chaining Mode One-Way Hash Functions Hashes come with init, process, done and self-test interfaces. All hashes use the same prototypes for the interfaces. Hashes supported. MD2 MD4 MD5 SHA-1 SHA-224/256/384/512 TIGER-192 RIPE-MD 128/160/256/320 WHIRLPOOL Message Authenticat

70,026

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧