当我尝试使用 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);
}
但是,我从解密中得到的纯文本与我用来创建密文的纯文本不同。