gpt4 book ai didi

c - OpenSSL ECB 非 64 位多纯文本

转载 作者:行者123 更新时间:2023-11-30 14:52:50 25 4
gpt4 key购买 nike

当我尝试使用 OpenSSL ECB 问题加密和解密非 64 位多个纯文本时遇到问题。

我有两个 .c 文件,一个要加密,另一个要解密。

这是第一个。

// FILE ENCRYPTION
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <openssl/des.h>
#include <sys/types.h>

void merror(char *msg) {
perror(msg);
exit(0);
}

char *Encriptar(char *Key, char *Msg, int size) {
static char* Res;
int n=0;
DES_cblock Key2;
DES_key_schedule schedule;
Res = ( char * ) malloc( size );

memcpy( Key2, Key,8);
DES_set_odd_parity( &Key2 );
DES_set_key_checked( &Key2, &schedule );
DES_ecb_encrypt((unsigned char *)Msg, (unsigned char *)Res, &schedule,DES_ENCRYPT);
return (Res);
}

#define LINELEN 8

int main(int argc, char *argv[]) {
int n, fdp, fdc;
char key[]="password";
unsigned char buf[LINELEN];

if (argc < 3) {fprintf(stderr,"USO %s <fileP> <fileC>\n",argv[0]);exit(0);}
if ((fdp = open (argv[1], O_RDONLY)) == -1)
merror ("Open FDP");
if ((fdc = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 00744)) == -1)
merror ("Open FDC");
while ((n = read(fdp, buf, LINELEN)) > 0)
write (fdc, Encriptar(key, buf, n), n);

close (fdp);
close (fdc);
exit (0);
}

这是第二个

//FILE DECRYPTION

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <openssl/des.h>
#include <sys/types.h>

void merror(char *msg) {
perror(msg);
exit(0);
}

char *Decriptar(char *Key, char *Msg, int size) {
static char* Res;
int n=0;
DES_cblock Key2;
DES_key_schedule schedule;
Res = ( char * ) malloc( size );
memcpy( Key2, Key,8);
DES_set_odd_parity( &Key2 );
DES_set_key_checked( &Key2, &schedule );
DES_ecb_encrypt((unsigned char *)Msg, (unsigned char *)Res,&schedule,DES_DECRYPT);
return (Res);
}

#define LINELEN 8
int main(int argc, char *argv[]) {
int n, fdp, fdc;
char key[]="password";
unsigned char buf[LINELEN];

if (argc<3) {fprintf(stderr,"USO %s <fileC> <fileP>\n", argv[0]); exit(0);}

if ((fdc = open (argv[1], O_RDONLY)) == -1)
merror ("Open FDP");
if ((fdp = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 00744)) == -1)
merror ("Open FDC");

while ((n = read(fdc, buf, LINELEN)) > 0)
write (fdp, Decriptar(key, buf, n), n);

close (fdp);
close (fdc);

exit (0);
}

但是,我从解密得到的明文与我用来创建密文的明文不同。

最佳答案

DES ECB 是一种具有 64 位(或 8 个八位字节) block 的 block 模式密码,设计为仅适用于 block 大小倍数的数据强>。另外,OpenSSL DES_ecb_encrypt 并不是真正的 ECB 模式,而是 DES block 原始:它加密或解密一个恰好 64 位的 block ,并且不多。你试图做的事情不应该也不可能起作用。

需要处理可变长度数据的正确设计的密码系统(许多系统都这样做)要么使用流密码、流模式(如 CTR),要么使用带有填充的 block 模式(但不是 ECB,见下文)——有多种标准可供选择; OpenSSL 的 EVP_{Encrypt,Decrypt,Cipher}* 模块默认使用 PKCS5/7 填充,但您可以将其关闭。

<小时/>

注意 DES 已经被破坏和过时了二十年,并且在大多数使用 ECB 模式的应用中,即使具有良好的原语,它也会允许降低或消除安全性的攻击。如果您打算真正保护某些东西,请放弃它并使用由知道自己在做什么的人设计的程序 - 但这对于 SO 来说是题外话。

<小时/>

更多关于SO的主题,您的程序使用错误的指针类型来调用DES_ecb_encrypt,您的编译器应该已经检测到并警告您;然而,由于 C 定义数组的方式,任何实现都不太可能因为这个错误而实际失败。此外,您没有 #include exit malloc memcpy 所需的原型(prototype),这可能会导致 size_t 大于 int 的系统出现错误或崩溃>.

关于c - OpenSSL ECB 非 64 位多纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47524230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com