Important alert: (current site time 7/16/2013 1:08:45 AM EDT)
 

article

[ Making better encryption ]

Email
Submitted on: 7/8/2004 6:00:43 AM
By: D. Rijmenants  
Level: Intermediate
User Rating: By 17 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 30462
author picture
(About the author)
 
     Want to make your own encryption algorithm? Taking your first steps into cryptology? Busy for a while but not too sure about it? Think your encryption is strong? Read this article first and get some good tips!

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				



Guide for better encryption.

I notice that many people are fascinated by cryptology. It's an exciting branch of coding, but many put their first steps into it without any background. This results in huge mistakes in writing and implementing their new 'unbreakable' algorithm. Every cipher can be broken! the only question is: How long does it takes?

Many programmers proclaiming they have good encryption, and i'm sure they believe it. Unfortunately their ciphers are all broken within very short time without any 'brute force attack' or cryptanalysing, but with a bit of examining and maths by anyone who sets his mind to it. This goes surprisingly also for many commercial software! I hope by giving some simple basic tips, they realize how easy it is to break weak ciphers and they will make the code a bit securer, so that not every amateur can break their cipher.

Tip1: Don't thrust yourself
NEVER thrust your own encryption. Yes, you're smart and you don't find a way to break it, but there are always others, smarter than you (unless you're Stephen Hawkins), who will break it. Thats why you allways need others to check your work. The best way to learn writing ciphers, is by first learning how to break them. How can you tell your encryption is safe, if you don't know how to break (read cryptanalyse) it?

Tip 2: The easy way?
Stream ciphers like RC4 are popular because they are easy to implement. Watch out! Streams have some big weak spots. You can only use the key once. If you use it twice, simple cryptanalyse can compromise the key. This very important disadvantage goes for all xor ciphers! If you really want to use xor based ciphers, you should always follow tip 3 and tip 10. Don't get blinded by easy ciphers. A good example is the very simple one-time-pad. Although theoretically unbreakable, it is impossible to implement it in a practical way, due to hughe problems it poses on management and distribution of keys. Often good ciphers are useless because they are implemented the wrong way. It is not because a cipher is unbreakable in theory, that you can also implement it on a unbreakable way.

Tip 3: Easy to enhance
Finished your cipher? Take the next char to encrypt, and xor it with the previous cipher output. On decrypting, just xor the result with the previous decrypted char. This is called chaining. That way, one decryption error is fatal to the rest of the message and thus makes the cipher much stronger. You can chain ciphers, output or input. This is a simple tip, easy to apply, but has a great result!

Tip 4: Don't make it easy to the bad guys
Never use your key or password directly to manipulate bits and bytes in your plain text, or use the bytes of your key one by one, and start all over at the end. That way you link your key directly to the cipher text and the door is wide open to crack the cipher. It's like giving it away for free. About 90 percent of all ciphers, found on PSC, are all kinds of variations on poly-alphabetic rotation or substitution, and easy to break by simple multiple frequency analysis!

Tip 5: Make it hard to follow
Create a cipher where the plain input determins how the algorithm works. A cipher that does the same work all the time is sensitive to attacks. Simplified example: if the next plain text is an A, the cipher will for instance xor it with a value, but if the next plain is a B, it rotates the bits n times left, and is it a C then rotate 2n times right. On xor ciphers, you could use a mask byte, changed on certain conditions as just described, and xor it with each cipher output. You could have the plain input manipulate the key bits, or swap key bits or bytes, so that the key changes all the time during encryption. How does the cipher works? Who knows, it's changes all the time.

Tip 6: The serious work
To create a block cipher the most common way to encrypt data is a feistel network. When you use 64 bit blocks, divide the block in two 32 bit parts where the right part together with the key are the input of a function, and the output is xored with the left part. Next the left part is encrypted with the right and so on. Each step is called a round (DES uses 16 rounds). To decrypt, the whole process is reversed form step 16 to 1. You can find plenty of documentation on the structure of block ciphers on the net.

Tip 7: The basics
A good function uses three important steps:
1. Substitution: the replacing of groups of bits or words by others.
2. Fractionation: breaking up groups of bytes in smaller parts before relocating.
3. Transposition: swap words, bytes or bits from position with each other.
The combination of these three operations results in diffusion. This diffusion is required for any good encryption scheme (see Shannon). This can be applied in a whole text, or within blocks, for instance combined with a feistel network. The way these steps are executed must depend on a secret key.

Tip 8: Help the users
Don't make it possible for the user to use weak keys. Write a routine where you refuse key's as 'aaaa', 'mamama' or '123'. Even good ciphers are useless if you use those, or 'top secret' or 'britney spears' as password. There are many idi*ts that use those keys. Be nice and help them to use only good passwords. Of course, they will always be that negligent to use the same key more than once. So take care that the key isn't compromised by this (see straightforward used streams and xor's). If a key is compromised, and they use it also for other important stuff like banking or account login, this could mean that your weak cipher is responsible for their problems!

Tip 9: Random and Random
Big mistake of many beginners: random isn't random when it comes out of your computer. Computers are NEVER random, they are to disciplined for that. If you do use a computers rnd function, seed it first with randomly chosen values. Better to use your own rnd-code. But it's very difficult to write a good one. Writing good randoms, as for use in stream ciphers, is an art. You want random? Get a bunch of xy values from the moving of your mouse, and use these to initialize your own rnd code. that's random (unless you have some tic). Never forget that there is a big difference between randomness and crypto secure randomness, so get well informed on the quality of Linear Shift Registers and other pseudorandom generation schemes before getting into the Random bussiness.

Tip 10: Get a good start
Put a bunch of randomly chosen bits and bytes before your real data and encrypt them along with them. This is very effective in a cipher where the algorithm is used in a chaining or feedback mode. That way, others cannot retrieve the key settings at the beginning of the actual data, and every encryption, althoug with the same key, is different. Those rnd bytes are simply disgarded during decryption. Even better is that the number of rnd bytes is also a random quantity, by this hiding the position of the actual start of data, so encrypt your rnd header length also. This tip is an absolute must on xor ciphers like RC4 if you really want to use them.

Tip 11: Stronger stuff
Compress your data BEFORE encryption, this will stenghten your encryption greatly! Ofcourse, don't use a zip-file, but compress it in your code. There are several good compressions in vb. Compression will fraction the data already before any encryption deals with it.

Tip 12: The finish?
Think it's finished? Try to encrypt let's say 100.000 A's with a repetitive key like 'ABC'. Next, take a good look at the encrypted data and do some statistics on it. If there are any repetitions or regular patterns, there's a smelly thing about your algorithm. Back to the drawing board! ALWAYS be paranoia about your own cipher, and NEVER thrust others so called unbreakable ciphers before you have done a good analysing of them. I know, it's easy to copy and use that encryption scheme from mister X. Do you want to use and thrust code that does unknown things? Finally, copy these tips and save them. Read them all over when you finished writing your cipher.

Tip 13: Top Secret?
A good algorithm should always be published. If you wrote some encryption scheme, document it properly, with commonly used notation, so that anyone can understand it. A secrecy system may only depend on the secrecy of the key, NEVER on the secrecy of the algorithm! Any crypto program can be reverse engineerd, so secrecy is useless. A good hint on the quality of the encryption is it's description. Watch out for snakeoil with great names like chaos theoretics, triangulating arrays, cryptonic sublimation, cosmic correlation, bla bla bla: Big crap! If they say it's a secret algorithm, you can be absolutely sure that it's junk or has backdoors.

Tip 14: Think it's too hard?
Never give up, it's a great and exiting art. look around, read crypto papers, check out FIPS's and RFC's on encryption on the net, visit sites, learn from the big ones. Start by learning how the classic ciphers like Vigenere, Auto-key, Bifid and Trifid, ADFGVX, and many more work, and how they were broken. These are the origins of modern cryptology and a good learning base. Good luck !

Tip 15: Some ideas
Check out submissions like 'Cipher Classics' or 'ULTRA file and text encryption', an example that applies many of the tips...and no, I won't say that ULTRA is unbreakable...but it's a strong one ;-)

Tip 16: Some great links
Introduction to Codes, Ciphers and breaking them:
http://www.vectorsite.net/ttcode.html
Basics on classic ciphers and how to break them:
http://www.simonsingh.net/The_Black_Chamber/home.html
Bruce Schneier's site, the crypto and security guru:
http://www.schneier.com/
Handbook of Applied Cryptography, pdf downloads
http://www.cacr.math.uwaterloo.ca/hac/

A cryptographic compendium:
http://fn2.freenet.edmonton.ab.ca/~jsavard/crypto/intro.htm
Claude Shannon Theory Of Secrecy, the basics on crypto:
http://www.cs.ucla.edu/~jkong/research/security/shannon1949.pdf
A crypto dictionary:
http://www.cryptnet.net/fdp/crypto/crypto-dict.html

Never forget:
A weak encryption is more dangerous
than being careful without encryption !!!

Happy codings from Dirk ;-)

PS: and pleaaaase don't call your ciphers unbreakable any more, even in best cases, call it strong... :-)


Other 28 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
7/8/2004 8:27:51 AMrlblanch

It would seem that one should be equally versed on encryption as well as decrytion to even attempt anything.
(If this comment was disrespectful, please report it.)

 
7/8/2004 2:38:22 PMD. Rijmenants

Yep, how can you say you have a good encryption if you never tried to crack it first!
(If this comment was disrespectful, please report it.)

 
7/12/2004 4:53:20 PMOlivaq

do you have any tips on how to do a thorough analyse of an encryption?
(If this comment was disrespectful, please report it.)

 
8/2/2004 1:12:50 PMD. Rijmenants

There is no fixed method to analyse encryption, other than 'if it can be broken, it's bad'. It all depends on the type of encryption. For instance on substitutions they use frequency analysing to see relations between the code and common used chars, and then start assembling the words. Many ciphers are mathematical that simple that they can be reversed, or have too much known factors. Pseudorandoms are tested mainly by statistics, checking how random it is, and if there are any patterns to detect. More complicated ciphers cannot be simply analysed by a known methods and it depends on the sturcture of the algorithm. There is to be searched for weak spots, relations between code and key, paterns etc. Each new cipher is a new quest for new methodes to reverse it. There are methods that make the work less complicated or faster and literature about that you can find on the net. Good tip? The books from Bruce Schneier, THE crypto guru (www.counterpane.com)
(If this comment was disrespectful, please report it.)

 
8/2/2004 1:15:45 PMD. Rijmenants

To give an idea of how simple it can be to analyse a weak cipher I 'broke' the submission "[SickAnimations] Low-Level Encryption Easy BUT Safe". (Check all my comments there). To get an idea about wath it took to break many known cipers in history, check out Simon Singh 's 'The Code Book' on www.simonsingh.net/The_Code_Book.html and visit on his site 'The black chamber', a must to get an idea about very basic cryptanalysing. Very interesting and shilling. Lots of ciphers, lots of methods...
(If this comment was disrespectful, please report it.)

 
8/26/2004 4:02:23 AM

asd
(If this comment was disrespectful, please report it.)

 
9/11/2004 11:55:29 PMMatthew Kernes

Very nice. I'll will take this all into mind on my next project. That compression statement I read was very similar to one of my thoughts.
(If this comment was disrespectful, please report it.)

 
9/23/2004 6:22:40 AM4Deus

You are very aware of your crpyts, you must be working on encryption all the time, very nice talents...
(If this comment was disrespectful, please report it.)

 
9/23/2004 11:35:22 AMD. Rijmenants

No talents involved, Knowing alot about encryption is nice, but doesn't make you a good 'cryptologist'. I'm just a very tiny litle fish trying to do it's best, but there are people much much better and smarter. Thats someting you may never forget. :-)
(If this comment was disrespectful, please report it.)

 
12/29/2004 9:53:29 PM

It should be clear that the need for an elaborate encryption algorithm is a function the amount of data encrypted and the value of that data.

If I keep a diary of my progress in keeping my weight down, and it is about 1,000 words long, then a very simple encryption algorithm will suffice.

If a large number of people are sending a large volume of diplomatic communications on a sensitive topic on a daily basis, then you can't be elegant enough!

So I have created some programs that I believe to be secure based on the volume and importance of the data.

I wouldn't "waste time" on any of the complicated methods you presented as such security is unwarented. It is like having a paper shredder. If your work is sensitive enough, sure, you want to triple-shred and mash and wet it down and make paper soup. But such a machine is unnecessary for ordinary usage.

Mac

(If this comment was disrespectful, please report it.)

 
12/30/2004 4:13:14 AMD. Rijmenants

Very correct. A cipher is secure when the amount of time, effort and cost is greater than the benefit. No one will spend such an effort for a diary (unless you're Bill Clinton ;-), but there is a problem. Today's people don't realize how easy it is to retrieve sensitive things like pin codes or other personal stuff. If you start writing your pin codes in your diary, you do have a problem. And the problem is that people don't realize how many leaks there are in there behaviuour.
(If this comment was disrespectful, please report it.)

 
1/15/2005 8:48:50 AMVincent Hahn

Ok I'll admit I'm low level at best, mainly because programming's a hobby for me. Just to make sure I'm understanding your article correctly, is what you're saying basically something along the lines of... no matter what your encryption can be broken, what makes it effective is to make it so people who want to crack it will feel it's not worth the effort to try to, meaning more steps involved. By the way, I found your article quite interesting, regardless that I couldn't understand some of it, such as "xor" stuff, again I'm a beginner.
(If this comment was disrespectful, please report it.)

 
1/15/2005 8:59:40 AMD. Rijmenants

Well, if you want to break a code, that gives you acces to 100,000 dollar, and you must invest 200,000 dollar to crack the code, it's a wast to try cracking it. BUT, once you did this investment, and you can automate cracking 1000 of those codes, it's worth it. (see f.i. creditcard skimming) So a code must be that strong that it is'nt worth cracking it. Everyone can crack Blowfish, the best there is, by brute force, just try all possible codes, but you'll need a zillion supercomputers working a zillion years to do it. (That is with current computertechnology. (DES was unbreakable until a few years ago)
(If this comment was disrespectful, please report it.)

 
2/14/2005 3:28:46 AM

Ok , its a nice script
alot of thing to do to encrypt data
but this is good
(If this comment was disrespectful, please report it.)

 
3/6/2005 2:57:32 PM

VERY good ideas.. I am trying to write some encryption methods for a IM program and I want to make secure communication.. would you have any ideas about going about that??
(If this comment was disrespectful, please report it.)

 
3/25/2005 12:38:47 PMTomás

D.Rijmenats surprises me once and once and once more..
Checked your crypto programs. Very nice.
Im not a math geek. But programs work smooth. And i found they are very useful.
Specially the one time pad program.
Nice Work, Rijmenats!
(If this comment was disrespectful, please report it.)

 
6/12/2005 5:12:29 AM

terima kasih
(If this comment was disrespectful, please report it.)

 
11/5/2005 4:49:41 AMvampyr

nice tutorial, nice crypto programs, tho would you try to remove the random grammar mistakes please;) 5 globes from me
(If this comment was disrespectful, please report it.)

 
11/9/2005 10:45:35 AMDann Winter

I really enjoy read this small tutorial thanks for sharing your knowledge.
(If this comment was disrespectful, please report it.)

 
12/2/2005 6:49:11 AMMike Douglas

Nice. I can help with STEP 8. My code for password strength checking could be used to enforce good key creation from users; http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=47749&lngWId=1
(If this comment was disrespectful, please report it.)

 
12/2/2005 6:57:35 AMMike Douglas

Vampyr; Dirk is from Belgium, his English is probably better than your Dutch. Besides, being from Oklahoma, I never noticed any Gramatical errors. :)
(If this comment was disrespectful, please report it.)

 
4/8/2006 2:00:53 PMMTIG

Has anyone considered the possibility that the author is trying to "tell us something" in the not-so-random grammatical errors? ;) I am always amazed at human intelligence. I must say that all I really want to do is protect very sensitive data, not invent a new encryption method. My hat is off to all of you who make that possible!
(If this comment was disrespectful, please report it.)

 
7/18/2007 3:22:55 PMcincode5

Very eniightening with lots to consider, but as MTIG points out, the point is to protect sensitive data. If you can generate a strong encryption, most hackers wont even bother to try and crack it. The path of least resistance would generally prevail and they'd move on to something less complex - yes?
(If this comment was disrespectful, please report it.)

 
9/18/2007 8:32:07 AMilias

>>but there are always others, smarter than you (unless you're Stephen Hawkins)...
I will add to that:
There are always others that are "not smart" (the real word was rejected by a "not smart" rejecting system) and hence they can see thinks from a different view and so they can find holes that a smart person cannot see.
(If this comment was disrespectful, please report it.)

 
10/31/2007 11:13:19 AMHugo

I like this encryption because I poor
(If this comment was disrespectful, please report it.)

 
7/22/2010 11:30:52 AMmuthuvel T

very good
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.