Cryptography

In the field of communication, cryptography has two goals: to hide the information content of a message; to prevent the forgery of a message. The first goal is obvious as no one wants that, for example, thieves know the access-codes for electronic banking. The importance of the second is less obvious, but in particular banks made the experience that their reputation suffers from a large quantity of forged messages floating around. Public key cryptography tries to achieve today both goals and made the current use of the internet in the way it is done today possible. Although there is no scientific proof that it is secure, everyday experience shows that it is secure enough to enable protected communication and limit the fraud at electronic commerce to a level that makes this business still profitable enough. Nevertheless, it is unknown whether the secret services of the big countries can break it or not; for that reason all electronic communication should be considered only as "almost secure", not as "completely secure". The following homework deal with statistical attacks. That is, as the encoding algorithm will use only a few choice of keys, the goal is to write a decryption algorithm testing all these keys and then automatically selecting that decrypted message which is most reasonable from statistical viewpoint. In other words, any possible key will be tested and a statistical analyzing tool shold evaluate whether the decoded message is written in some language (using the Latin alphabet); if so, it should be displayed on the screen; if not, the next key should be tested.

Tasks
The setting is the following: Suppose a spying agency would have found out some information on the way an opposing country or organization is encrypting or decrypting is messages. The task is now to do the following:
  1. To complete the function decrypt which inverts an encryption with a known code.
    You can study the function encrypt how this is done. Together with the decrypted function, statistical data is gathered and stored into an array. This data mainly chategorizes the characters (= symbols) used according to being a digit, a lower case letter, an upper case letter and so on. These informations should latter be exploited during the code-breaking in order to evaluate whether a decoded message is really a natural text or just a useless sequence of symbols obtained by using the false key. The function symbtype should be used to determine the numerical value assigned to a letter.
  2. To adapte the function "eval" which evaluates the statistical data.
    This function should return a "0" if it supposes that the message is decrypted with the correct key and should return some other number otherwise. These other numbers are displayed so that one can verify why the function thinks that the returned text is not a real message but a wrongly decoded thing, so they help to adjust the weights and cut-off values for the case that some condition is chosen too restrictive. It might be suitable to add more conditions in order to get the function sharp in its judgement.
  3. To exploit a vulnerability of the enemy for decoding four messages.
    The enemy has several people who choose these codes. These people are not very creative and spying permitted to find out that they follow easy patterns. All codes are -- as seen while implementing the function encrypt above -- four numbers. The possible codes have to be stored in an array with name "checkcodes". Here is the intelligence collected on these guys: These informations should be used to select the entries for the codes to be checked. Those are stored one after the other in an array called "checkcodes". The first of these entries (from the guy trusting the Golden Pig) are already implemented:
    checkcodes = new Array(1,4,0,7);
    checkcodes.push(2,0,0,7,2,6,0,7);
    
    The other entries have to be added.
The programmes should be developped step by step. So first write and test the function "decrypt", then the function "eval" and in the last step complete the data for the array "checkcodes". Edit this file only between the two comments
  // DO NOT EDIT THE CODE ABOVE THIS LINE
and
  // DO NOT EDIT THE CODE BELOW THIS LINE
in the Java Script programme. You can and should of course read the function "encrypt" and the subfunctions called there in order to understand how the encryption is done.