-
help wiv encryption
here is what i av written
Code:
/*program encrypts messages*/
#include <stdio.h>
#include <ctype.h>
main()
{
char str[255];
int i;
int delt = 'a' - '9';
printf("Enter a string less than 80 characters:\n");
gets( str );
i = 0;
while (str[i]){
if ((str[i] >= 'a') && (str[i] <= 'z'))
str[i] -= delt;
++i;
}
printf("The entered string is (encrypted simply):\n");
puts( str );
return 0;
}
i need it perfected and mayb fool proof and i need tips on decrypting it also thanks
-
-
using gets() is not recommended at that can lead to overflows! Also what do you think the value of delt is doing here to your string elements? Do you know what the value of delt is? Also are you only trying to encrypt lower case letters?
Just some things to reflect on. Hopefully that helps you.
-
Wiv? Av? It sounds like you know more about encryption than you're letting on!
-
'a' to 'z' aren't guaranteed to be contiguous. Which means that your encryption basically won't work if the user is using a different encoding scheme (rare, I'll grant you, but not impossible).
Here's a simple encryption program I whipped up just for the fun of it. Use isalpha() instead of isprint() for less radical encryption.
Code:
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
int is_encryptable(int c);
void crypt_line(const char *line, const char *map, int map_entries);
int main() {
char map[UCHAR_MAX];
int x, y, map_entries = 0;
char line[BUFSIZ];
for(x = 0; x < UCHAR_MAX; x ++) {
if(is_encryptable(x)) {
map[map_entries ++] = x;
}
}
for(;;) {
printf("crypt> ");
if(!fgets(line, sizeof line, stdin)) break;
printf(" ==> ");
crypt_line(line, map, map_entries);
}
putchar('\n');
return 0;
}
int is_encryptable(int c) {
return isprint(c) && c != '\n';
}
void crypt_line(const char *line, const char *map, int map_entries) {
int x, y;
for(x = 0; line[x]; x ++) {
if(is_encryptable(line[x])) {
/* linear search, ugh */
for(y = 0; map[y]; y ++) {
if(line[x] == map[y]) {
putchar(map[map_entries - y - 1]);
}
}
}
else {
putchar(line[x]);
}
}
}
It simply collects all printable characters into one map. Every time a character is encrypted, it "reverses" the position in the map table and uses that instead. (By reverse I mean length - x - 1.)
Code:
$ ./encrypt
crypt> Greetings.
==> W,99*507+p
crypt> W,99*507+p
==> Greetings.
crypt>
$
Presumably you're aware that this sort of encryption isn't really that secure. :)
Anyway, for more interesting encyption, you can use the bitwise NOT operator (~). But that's even easier to guess and the result frequently isn't printable.
-
Nice use of your map[]. Yeah I see you are using a sequential search, for something like this perhaps a binary search would be in order. But hardly will notice any inpact on an array this small, but if you had to do 1000s of these then parhaps a little bit of a boost here. Since your map[] is sorted, this should work.
Code:
binary_search(map[], int x, int left, int right, int map_entries)
{
while (right >= left)
{
int m = (left + right) /2;
if (x == map[m])
putchar(map[map_entries - m - 1];
/*move up the array */
if ( x < map[m]) right = m - 1;
/*move down the array */
else left = m + 1;
}
return -1;
}
-
Yes, indeed. I was just trying to come up with a simplish encryption method that was sort of similar to the OP's encryption method. :)
Also, BTW, this is C so Boost wouldn't work.