12,635,132 members (23,421 online)
Add your own
alternative version

19.7K views
411 downloads
7 bookmarked
Posted

# Experimental Crypt

, 30 Aug 2010 CPOL
 Rate this:
Please Sign up or sign in to vote.
Experimental crypt

## Introduction

Warning: This article is not about conventional ciphers.

Cryptography is among my interests - from simple substitute ciphers up to public key ciphers and one-way functions, such as modulo. This experimental cipher is very simple, symmetric key using and relies heavily on hash algorithm implementation.

## The Reliability

The cipher is far poorly designed, and will serve in non-security based projects, where security is not a challenge. If you want a really world-wide used cipher, perhaps you should look for Rijndael as current AES symmetric private key cipher, or RSA and PGP, if you want really "Pretty Good Privacy".

## Basic Idea

1. Take password
2. Compute hash from it
3. xor plaintext with that hash (one byte at a time, up to 4 bytes)
4. When all four bytes of hash were used, then compute new hash as:
`HASH = Hash(HASH)`

So we should get another hash, made from the previous one. Although primitive in core, perhaps a bit controversial, but interesting.

More interesting is my CBC (although not real one, just pseudo CBC) that can be created by putting:

`HASH = HASH XOR source_plaintext_byte`

for crypting to the beginning of the loop and...

`HASH = HASH XOR destination_plaintext_byte`

...in decryption loop to the end.

There are two other, minor functions (macros) that are here for experimental purposes. They should've not been taken responsible for failing / securing a ciphertext. Their only purpose is to hide (possible) artefacts from ciphertext. But basic core is hash function itself:

```unsigned int Hash(BYTE *key, int length){
unsigned int hash = 0;
int i;
for (i = 0; i < length; i++){
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}```

`unsigned int `is 32 bit long number, `unsigned long long `would extend our simple poor hash to 64 bits, but that does not solve our problem as well. The hash number is way too short to guarantee anything even similar to "safety". 128 or 256 bit hash would be ideal, but if I am able to code any hash, then I can code any hash, with extended bit length.

I do not remember where I came across this hash, but could be "Jenkins One-at-a-time hash", from Bob Jenkins' article here. But for implementation, see this link.

I think that usage of better hash could produce a better chance that the first key-hash would not be broken. That has to be proven.

## Background

You can learn about CBC, ciphers, private and public keys, patterns, etc. on Wiki pages:

## The Code

This is pure C code with C++ class usage. The algorithm is widely portable, wherever the C is available.

### Base Class

```// base (the only one) class
class Perfect_Crypt{
public:
bool Crypt(BYTE *src, int srclength, BYTE *&dest,
int &destlength,BYTE *password, int passwordlength,
TCryptParam param);
};```

### The Core

```      // core functions - macros
#define neg(I) ( ~I)            // negation
#define rot(I) ( (I << 3) ^ (I >> (32-3)))      // bit rotation by 3
// hash, from an article by Bob Jenkins
unsigned int Hash(BYTE *key, int length){
unsigned int hash = 0;
int i;
for (i = 0; i < length; i++){
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
// crypt
addr = (BYTE *)&HASH;   // get the address of BYTE of our HASH number
while(srcptr != stop){
HASH^=srcChr;            // pseudo cipher block chaining
HashChr = *(addr+four);  // take particular byte from HASH into HashChr
srcChr = (*srcptr);      // copy plaintext char into srcChr
(*destptr) = srcChr ^ HashChr;   // output ciphertext byte as xored
// srcChr and HashChr
four++;          // used when taking bytes from hash number
if (four == 4){                  // if all bytes from hash were
// used 4 for 32 bit integers
HASH = Hash((BYTE*)&HASH, 4);     // make new hash
HASH = neg(HASH);         // negate hash bits
HASH = rot(HASH);         // rotate hash bits
four = 0;             // reset "4 bytes" counter
}
destptr++            // move forward;
srcptr++;
}
```

### Usage

If you want to use and experiment with this cipher, you can call:

```bool Texcrypt::Crypt(BYTE *src, int srclength, BYTE *&dest,
int &destlength, BYTE *password, int passwordlength,
TCryptParam param)```

## Points of Interest

The algorithm is mainly based on hash algorithm, so should there be any weakness in hash algorithm, it will have a direct impact on crypt reliability. The weakness here is collision production and not complete bits "overwhelming", when we need each bit to be hashed with each one (other).

Surprisingly simple algorithm, suspecting weakness, although, it is no conventional cipher anymore.

One more thing is that the cipher algorithm computes verification code, the summation of 1 in each byte. This is not CRC32, just pseudo CRC, to improve security.

It also includes pseudo cipher block chaining. Basically, CBC is designed to chain each cipherblock with the previous plaintext, so any small change in plaintext (such as error in decryption) would make all next blocks unreadable and "garbage" like in appearance.

Perhaps one will find such a concept useful.

## Additional Information

1. Rijndael, at Wiki http://en.wikipedia.org/wiki/Rijndael
2. RSA, at Wiki http://en.wikipedia.org/wiki/Rsa
3. PGP, at Wiki http://en.wikipedia.org/wiki/Pretty_Good_Privacy

## History

• Update 30.8.2010 - Added a few references and warning about reliability of the cipher
• Update 30.8.2010 - Renamed to Excrypt, that stands for experimental crypt
• Update 29.8.2010 - Added cipher block chaining (two lines of code) and few comments to make few things more clear
• Update 29.8.2010 - Removed one extra `rot()`, why was it there??
• Update 29.8.2010 - Comments

## License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

## About the Author

 Software Developer (Junior) SOMI Systems Slovakia
Got 1st and 2nd degree from computer science in 2011 on university of Matej Bel, city of Banska Bystrica. Currently work for computer-oriented company SOMI Systems in Slovakia.
Working for SOMI Systems a.s., http://somi.sk/

## Comments and Discussions

 First Prev Next
 Hash, Symmetric, Asymmetric [modified] Tomice29-Aug-10 22:16 Tomice 29-Aug-10 22:16
 Re: Hash, Symmetric, Asymmetric Jan Mojzis30-Aug-10 5:58 Jan Mojzis 30-Aug-10 5:58
 Known data [modified] waykohler29-Aug-10 19:05 waykohler 29-Aug-10 19:05
 My vote of 1 Shawn Poulson29-Aug-10 17:29 Shawn Poulson 29-Aug-10 17:29
 My vote of 1 Bething229-Aug-10 12:47 Bething2 29-Aug-10 12:47
 Perfect? imagiro29-Aug-10 7:12 imagiro 29-Aug-10 7:12
 Re: Perfect? Jan Mojzis29-Aug-10 10:06 Jan Mojzis 29-Aug-10 10:06
 brute force attacks are common. Try to use 60 letter password. Here is the key: hash produces unique key, it does not matter how long it is (in bruteforce, only collisions can matter ). Everything is based on hash. If hash is bullet-proof, the cipher is bullet-proof too. You see, this hash key is regenerated every one round (when all 4 bytes were used of it) when it goes to another 4 bytes, but this time with completly new hash. Original hash is replaced by newly created hash from old hash. Thus xoring 4 bytes and hashing previous hash until end of stream is reached. There cannot be same hashes for different 4 byte blocks anywhere. Try finding patterns
 Re: Perfect? Green Fuze29-Aug-10 10:55 Green Fuze 29-Aug-10 10:55
 Re: Perfect? Jan Mojzis29-Aug-10 11:22 Jan Mojzis 29-Aug-10 11:22
 You're missing the point Shawn Poulson29-Aug-10 17:28 Shawn Poulson 29-Aug-10 17:28
 Re: You're missing the point waykohler29-Aug-10 20:30 waykohler 29-Aug-10 20:30
 Re: You're missing the point Jan Mojzis30-Aug-10 6:03 Jan Mojzis 30-Aug-10 6:03
 Re: You're missing the point Shawn Poulson30-Aug-10 8:51 Shawn Poulson 30-Aug-10 8:51
 Re: You're missing the point Jan Mojzis30-Aug-10 10:04 Jan Mojzis 30-Aug-10 10:04
 Last Visit: 31-Dec-99 19:00     Last Update: 10-Dec-16 11:32 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 30 Aug 2010
Article Copyright 2010 by Jan Mojzis
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid