我正在尝试使用 glibc cbc_crypt 函数来加密 c 中的字符串,程序应该与函数一样可移植,这就是我使用这个库的原因
这是我的代码:
#define _GNU_SOURCE
#include <stdio.h>
#include <crypt.h>
#include <string.h>
#include <rpc/des_crypt.h>
int main(int argc, char *argv[]) {
int blah=0;
char plaintext[]="mypass1234test";
char key[]="aBcDeFg1";
char encbuff[] = "87654321";
char decbuff[] = "87645321";
des_setparity(key);
int size=sizeof(plaintext);
printf("plaintext is %s\n",plaintext);
printf("original size is %d\n",size);
while (size % 8 && size < 420)
plaintext[size++]='\0';
printf("new size is %d\n",size);
if (argc>1) {
if (strcmp("encrypt",argv[1])==0) {
blah=cbc_crypt(key,plaintext,size,DES_ENCRYPT | DES_SW,encbuff);
printf("ciphertext is %s\n",plaintext);
size = sizeof(plaintext);
printf("original size is %d\n",size);
while (size % 8 && size < 420)
plaintext[size++]='\0';
printf("new size is %d\n",size);
blah=cbc_crypt(key,plaintext,size,DES_DECRYPT | DES_SW,decbuff);
printf("plaintext is %s\n",plaintext);
}
}
return 0;
}
当我尝试运行该程序时,我得到以下信息:
./a.out encryption
plaintext is mypass1234test
original size is 15
new size is 16
ciphertext is 0ю�sBKX,�7&���8@ @
original size is 15
new size is 16
plaintext is myp`rs12��~�ϖ@ @
我的目标是加密文件和解密文件,我愿意使用其他加密功能,但我需要这个程序是可移植的(我宁愿不使用 openssl,因为我的机器在没有那个库的情况下运行)