高手请进?关于加密解密的

17809958 2003-10-17 09:48:32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crypt.h>
#include <unistd.h>

int main()
{
char key[65];
char str[1028];
char *s;
int i;
strcpy(str,"WJC1 DES!1223");
printf("before[%s]\n",str);
strcpy(key,"1234");
setkey(key);
s=str;
encrypt(s,0);//加密
printf("[%s]\n",s);

strcpy(key,"1234");
setkey(key);
encrypt(s,1);//解密,
printf("[%s]\n",s);

return 0;
}
cncrypt()是unix系统函数。
为何解密不出原数据呢?
...全文
39 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
17809958 2003-10-17
  • 打赏
  • 举报
回复
那我的参数该怎样写呢?
ari 2003-10-17
  • 打赏
  • 举报
回复
The block argument to encrypt() is an array of length 64
bytes containing only the bytes with numerical value of 0
and 1

读懂这句话.
shishiXP 2003-10-17
  • 打赏
  • 举报
回复
void encrypt(char block[64], int edflag);
The block argument to encrypt() is an array of length 64
bytes containing only the bytes with numerical value of 0
and 1. The array is modified in place to a similar array
using the key set by setkey(3C). If edflag is 0, the argu-
ment is encoded. If edflag is 1, the argument may be decoded
(see the USAGE section below); if the argument is not
decoded, errno will be set to ENOSYS.
你的参数不对吧
fierygnu 2003-10-17
  • 打赏
  • 举报
回复
USAGE
In some environments, decoding may not be implemented. This
is related to U.S. Government restrictions on encryption and
decryption routines: the DES decryption algorithm cannot be
exported outside the U.S.A. Historical practice has been to
ship a different version of the encryption library without
the decryption feature in the routines supplied. Thus the
exported version of encrypt() does encoding but not decod-
ing.

Because encrypt() does not return a value, applications
wishing to check for errors should set errno to 0, call
encrypt(), then test errno and, if it is non-zero, assume an
error has occurred.
在第二个调用后用perror,大概是Operation not applicable。
17809958 2003-10-17
  • 打赏
  • 举报
回复
encrypt是一个unix系统函数
Bandry 2003-10-17
  • 打赏
  • 举报
回复
把encrypt这个函数贴出来啊
要不然谁知道哪儿有问题,真是的
17809958 2003-10-17
  • 打赏
  • 举报
回复
????????
17809958 2003-10-17
  • 打赏
  • 举报
回复
我加了,运行结果是一样的。
我的问题关键在于encrypt(s,1);//解密,这一语句,执行后,S中的值没有发生变化。
实在不知是什么原因引起,还请各位指教.
晨星 2003-10-17
  • 打赏
  • 举报
回复
把key和str数组在使用前都清空。
对于数据和密钥来说,加密和解密针对的都是整个分组,而不仅仅是数组中“字符串”。事实上,密钥也好,数据也好,都不一定是Ascii字符串,也可能是一般的二进制流,比如多媒体文件的内容。

memset(key , 0 , 65);
memset(str , 0 , 1028);
17809958 2003-10-17
  • 打赏
  • 举报
回复
In the international version of crypt(S) a flag argument of 1 to encrypt()
or des_encrypt() is not accepted, and errno is set to [ENOSYS] to indicate
that the functionality is not available.
ari 2003-10-17
  • 打赏
  • 举报
回复
In some environments, decoding may not be implemented. This
is related to U.S. Government restrictions on encryption and
decryption routines

你的操作系统decoding没有实现.
17809958 2003-10-17
  • 打赏
  • 举报
回复
我的第二个函数运行出错,不知是什么原因
encrypt(s,1);//解密,
errno=[89][Function not implemented]
Hot_Forever 2003-10-17
  • 打赏
  • 举报
回复
setkey和encrypt都应该是带64个字节的字符串
ari 2003-10-17
  • 打赏
  • 举报
回复
#include <errno.h>
...
encrypt(s,1);//解密,
if ( errno != 0 ) {
printf("errno=[%d][%s]\n", errno, strerror(errno));
}
...
在hp-ux没有问题,sco没有实现.测试一下你的环境.

69,369

社区成员

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

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