I'm trying to use the glibc cbc_crypt function to encrypt strings in c the program should be as portable as function which is why I'm using this library
here's my code:
#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;
}
when I try to run this program I get the following:
./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��~�ϖ@ @
My goal is to encrypt files and decrypt files, I'm open to use other cryptographic functions but I need this program to be portable( I would rather not use openssl as I have machines running without that library)