Simple Encryption with strings

WeeZer51402

Registered
ok every body what i want to do is create a little simple encryption scheme, i have one here but id like to add on to it.



'#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

char string[100],yn;
int c,a=0,offset;

int encrypt(void);

int main (void)
{


encrypt();

return 0;
}

int encrypt(void)
{
printf("> ");
scanf("%s", &string);
printf("Offset by > ");
scanf("%d", &offset);
for(c=0;c<(strlen(string));c++)
{
a=(int)string[c];
string[c]=(char)(a+offset);
}
printf("%s\n", string);
printf("Run again? y/n: ");
scanf("%s", &yn);
if(yn=='y')
encrypt();
else

return 0;
}'



what this program does is it gets a string from the user, converts the characters to there decimal ascii value adds what the user wishes to off set the value by and converts it back to a character, heres what id like to do is say the user enters 'hello' (string[0]='h', string[1]='e', string[2]='l', string[3]='l', string[4]='o')
id like to be able to have the program take each char encrypt it but then add some extra stuff to the string so for instance hello would then = (string[0]='h', string[1]='a', string[2]='e'', string[3]='b', string[4]='l', string[5]='c', string[6]='l', string[7]='d', string[8]='o', string[9]='e')
in other words replacing every other character or every 2 characters or whatever, hopefully user specefied , with just some random garbage,as opposed to 'a,b,c,d,e', that can be encrypted as well the actual contents of the string. any help would be greatly appreceated thanks.
-mike
 
I wish I knew more about programming, as I'm a bit obsessed with enryption.

I would try to create a rolling encryption method - where the encryption offset changes with every character.

So, maybe something like this:

new number=(normal number)+(offset)+(5-(2*Line number)+character number)
 
Originally posted by kilowatt
might want to check this out:
http://www.jwz.org/hacks/scrmable.pl
Are you serious... I'd have a hard time copying code from someone who spells like this:
# Premssioin to use, cpoy, mdoify, drusbiitte, and slel this stafowre and its
# docneimuatton for any prsopue is hrbeey ganrted wuihott fee, prveodid taht
# the avobe cprgyioht noicte appaer in all coipes and that both taht
# cohgrypit noitce and tihs premssioin noitce aeppar in suppriotng
# dcoumetioantn. No rpeersneatiotns are made about the siuatbliity of tihs
# srofawte for any puorpse. It is provedid "as is" wiuotht exerpss or
# ilmpied waanrrty.
I hope that was meant to be funny....
 
See this for what the program does:

slashdot.org


I think the license will make more sence. Or, you could just run the program :gasp!: and then you'd get it :p


But yes, its a bit of a joke
 
hmmm, im having some trouble decrypting that rolling encryption method, i changed it around a bit, it looks like this now '
for(c=0;c<(strlen(string));c++)
{
a=(int)string[c];
string[c]=(char)(a+offset+(2+(2*(strlen(string)))));
}'
 
Make sure you don't exceed char(255) or it'll roll over.

I don't know (offhand) if the OSX compiler defaults to signed or unsigned chars, but this could impact your logic.

XOR can sometimes be more useful than addition for encryption
 
What exactly do you mean, "offset the array"?

You want to make each character's position within the array part of the encryption? In your example, just make the loop variable c part of the encryption.

Or do you want to shift the data around within the array? A 2-step memcpy might be easiest.

IF you wanted to get really jiggy you could read about the G4 vector operations, like vec_perm.
 
Originally posted by WeeZer51402
heres what id like to do is say the user enters 'hello' (string[0]='h', string[1]='e', string[2]='l', string[3]='l', string[4]='o')
id like to be able to have the program take each char encrypt it but then add some extra stuff to the string so for instance hello would then = (string[0]='h', string[1]='a', string[2]='e'', string[3]='b', string[4]='l', string[5]='c', string[6]='l', string[7]='d', string[8]='o', string[9]='e')
in other words replacing every other character or every 2 characters or whatever, hopefully user specefied , with just some random garbage,as opposed to 'a,b,c,d,e', that can be encrypted as well the actual contents of the string. any help would be greatly appreceated thanks.
-mike
 
You want to add extra "dummy" data to your string?

Well, it'd have this basic form:
garble(in, out)
{
int a,b,c,d,e;

out[0]='\0';

for(a=0; a(LESSTHAN)strlen(in); a++)
{
// 1 by 1, adds a char from in, then a random char
sprintf(out,"%s%c%c",out,in[a], (char)(rand()%256));

}
}

//Note that this site does not allow the actual less-than symbol
// So where you see (LESSTHAN) I naturally mean the real less-than symbol
 
LESSTHAN?

a &lt; b

Just use HTML code: &amp;lt;

BTW: Your loop evaluates strlen() in each iteration, however, strlen() is a quite expensive call.

int len;
...
len = strlen(in);
for (a = 0; a &lt; len; a++)
...

(just my .02) :)
 
You're right. It also rebuilds 'out' from scratch every iteration. It's far from optimized -- I just slapped it together to demonstrate the concept.

Thanks for the tip about &lt
 
Why not use the builtin encryption like blowfish, much better than trying to create your own. Encryption which is a bit of a fine art if you want to do it right. Do a "man blowfish" or "man EVP_EncryptInit" to find out how to use it.
 
Back
Top