Basic encryption

R

Richard

I'm looking to do my own basic encryption. I've been tyring to do a
concept such as:

I pass this function the string, key and number of rounds I want to do
the encryption. because im round shifting the bits the decryption
doesn't work.. :p
Am I going about this all wrong or does anybody know an easier way
(besides getting an open source algo class such as Crypto++)

I'm super new to fooling with the bits/encryption

for(;num of rounds;)
{
char ^ Key;
Round bit shift;
}
 
I

Ivan Vecerina

| I'm looking to do my own basic encryption. I've been tyring to do a
| concept such as:
|
| I pass this function the string, key and number of rounds I want to do
| the encryption. because im round shifting the bits the decryption
| doesn't work.. :p
| Am I going about this all wrong or does anybody know an easier way
| (besides getting an open source algo class such as Crypto++)
|
| I'm super new to fooling with the bits/encryption
|
| for(;num of rounds;)
| {
| char ^ Key;
| Round bit shift;
| }

What type of encryption you want to implement is not
really clear to me.

Just a few comments I can throw in:

Watch that the bit-shift operators (<< and >>) actually lose
data: they shift bits, and do not roll them.
To implement rolling on a e.g. 32-bit value, you can use
something like:
*dst = (*src << nbits) | (*src >> (32-nbits) );
Where *dst and *src shall be 32-bit unsigned integers,
and nbits must be in [1..31].


Not sure if it helps, but a 'simple' encryption source
code can be found at: http://ivan.vecerina.com/code/coopfish/ .
(implements the blowfish algorithm in a C++ fashion).


hth-Ivan
 
J

J. Campbell

I'm looking to do my own basic encryption. I've been tyring to do a
concept such as:

I pass this function the string, key and number of rounds I want to do
the encryption. because im round shifting the bits the decryption
doesn't work.. :p
Am I going about this all wrong or does anybody know an easier way
(besides getting an open source algo class such as Crypto++)

I'm super new to fooling with the bits/encryption

for(;num of rounds;)
{
char ^ Key;
Round bit shift;
}

I just made a similar program, as vehicle to use to (try) to learn
cpp). The code is zipped up at:

http://home.bellsouth.net/p/PWP-brightwave

To compile it, include all the CPP files into a project then build it.
int main() is in j_crypt.cpp. Or...you can drop a file on the exe
file if you don't want to compile it yourself. This code will *not*
modify/overwrite any files...it always gives them a new name.
Anyway...this is what I came up with. slicerclass is a PRNG that can
be used stand-alone, once initialized, the member function get_rand()
returns a "random" unsigned long int.

My prog may be a little more complicated than what you have in mind. I
made use of a custom file-format that carries the original filename
(encrypted) and a hash-value for the original file (also encrypted),
so that authentication can be performed.

I do not present this as model code, or as an example of learned
cryptography. It happens to be where I stand in late 2003 on the
learning curve. What this crypto will do is...take any file (even one
filled with null bytes) and generate a stream of data
indistinguishable from "random" that contains the file info. Having a
copy of both the plain and cypher-text won't help an attacker get the
password. Successfully unencrypted files are authenticated. There is
an inbuilt mechanism designed to make a dictionary attack difficult.
cheers.
 
R

Richard

Thanks guys,
Ya i took into consideration when you shift you loose bits. I was
justORing it with 0x80 for char to test if bit was 1/0 so i could tack
it on the end i was shifting into.

I should be able to get it right by trial and error.
 
E

Ekkehard Morgenstern

Hi Richard,

Richard said:
I'm looking to do my own basic encryption. I've been tyring to do a
concept such as:

Am I going about this all wrong or does anybody know an easier way

I've written numerous quite safe encryption (scrambling) algorithms. Don't
use something too simple if you want to be safe. To test your encryption you
could write a program that prints or displays image data. Then, encrypt an
image and see what it displays then. If the original image is "shining thru"
the algorithm is bad.

I've had much success by using random numbers and loops that depend on
previous data. You need at least two or more variables that are combined
with the source data.

You could do something like this:

void simple_encrypt( int* dest, int* src, int count, int& v3, int& v4 )
{
int v1 = 0XFC03AB9D;
int v2 = 0X4903CB0A;
v3 = rand();
v4 = rand();
for ( int i=0; i<count; ++i ) {
int v = *src++;
v = ( ( v1 ^ v ) - ( v2 ^ v ) + ( v3 - v1 ) - ( v1 + v4 ) );
v1 ^= v2 + v3;
v2 ^= v1 - v4;
v3 -= v1 + v2;
v4 += v2 - v1;
*dest++ = v;
}
}

You would have to keep the values of v3 and v4 elsewhere to be able to
decode the data later on. The encoding should be designed such of course
that you can reverse all operations.

The code I gave is just an example how you could do it, just play around
with various combinations of +, - and XOR (^). Those are easier to do in
reverse than other operations.

Have fun! :)
I hope this helped! :)

Regards,
Ekkehard Morgenstern.
 
E

Ekkehard Morgenstern

BTW, for random number generation, you could (if you're programming for the
IA-32 architecture, like Pentium 1-4), use the processor's uptime counter in
the seed value:

__int64 GetProcessorClock( void ) { _asm rdtsc; }
void SetRandomSeed( void ) { srand( (unsigned)( time(0) ^
GetProcessorClock() ) ); }

The value from RDTSC is unsuitable for using in loops, because it increments
with each processor cycle. If your routine uses a fixed amount of cycles,
the results might be predictable. But you can use it in an initial value.
;-)
 
I

Ivan Vecerina

| I've written numerous quite safe encryption (scrambling) algorithms. Don't
| use something too simple if you want to be safe. To test your encryption
you
| could write a program that prints or displays image data. Then, encrypt an
| image and see what it displays then. If the original image is "shining
thru"
| the algorithm is bad.
|
| I've had much success by using random numbers and loops that depend on
| previous data. You need at least two or more variables that are combined
| with the source data.

The "Secure Programming Cookbook" of John Viega & Matt Messier has
a free chapter about random numbers available online:
http://www.oreilly.com/catalog/secureprgckbk/ (book page)
http://www.oreilly.com/catalog/secureprgckbk/chapter/ch11.pdf
I also vaguely remember, from reading TAoCP, Knuth stating something
like "Don't use a random approach to generate random numbers".

For both encryption and random number generation, which are closely
related, I would rather rely on published and well-studied algorithms.

I'm not a cryptanalyst, and I am confident that an expert can do much
better than me. Too many commercial (and free) applications/OSes
suffered security loopholes because of weak random number generation.
I don't trust myself to invent something better...


This said: the OP was asking for 'basic encryption' -- and your post
and suggestions are very helpful and fully relevant in this context :)


Kind regards,
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top