Really, the encryption I used is feeble at best. Each character you input would end up with the same character for output, not to mention other shortcomings. To get around that you could do various thing such as seeding the encryption with the length of the input string and using an accumulator:
Code:
itsme@itsme:~/C$ cat encrypt.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int hash(unsigned char ch)
{
return (int)(((ch & 0xF) << 4) + ((ch & 0xF0) >> 4));
}
int main(int argc, char **argv)
{
char alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789^$@#";
char input[BUFSIZ], *output;
int i;
unsigned int val, acc;
size_t len;
if(argc != 2)
{
char *p;
printf("Enter string to encrypt: ");
fflush(stdout);
fgets(input, sizeof(input), stdin);
if((p = strchr(input, '\n')))
*p = '\0';
}
else
{
strncpy(input, argv[1], sizeof(input));
input[sizeof(input) - 1] = '\0';
}
len = strlen(input);
output = malloc(len + 1);
for(i = 0, acc = len;i < len;++i)
{
val = hash(input[i]);
output[i] = alpha[(val + hash((unsigned char)(acc & 0xFF))) % 66];
acc += val;
}
output[i] = '\0';
puts(output);
free(output);
return EXIT_SUCCESS;
}
Code:
itsme@itsme:~/C$ ./encrypt
Enter string to encrypt: hello world
UT8uLuLCW5E
itsme@itsme:~/C$ ./encrypt "hello world\!"
mlA0d0d8oq^j$
itsme@itsme:~/C$
Note that I don't have any real experience in encryption. I'm sure others here could point out other serious defects in the methods used.