[toc]
## Basic Definitions ##
What is cryptography? Cryptography is the science which studies hiding of information. Hiding of information involves the process of manipulating the information so that it becomes unreadable or indecipherable to anyone who is not the intended recipient of information. The conversion of the information from its readable state to unreadable state is referred to as *encryption*. The reverse process of converting the indecipherable information from unreadable state to readable state is referred to as ''decryption''.
In addition to encryption and decryption, modern cryptography deals with identity authentication and verification. Identity authentication allows us to establish beyond any doubt that some person (or even device) really is who he/she claims to be. Identity authentication is primarily established through the use public key infrastructure (more on that in the following chapters). Important part of this is also a digital signature (serving the purpose of verifying the integrity of some content).
Encryption and decryption methods vary in nature, complexity, and security. The parties which are attempting to exchange encrypted messages usually must arrange on what methods they'll employ during this exchange.
In the simplest form encryption could be achieved by replacing one set of symbols with another set of symbols. In order for this method to be reversible, and to have a decryption method for it, it's necessary to make sure that each symbol from the first set maps to a unique symbol in the other set (i.e. no two symbols from the first set can map into same symbol of the second set and vice-versa).
Practical example would be substituting letters in some plain text document (let's say in English language) with different letters. A message encrypted in such a way would be transmitted over to its destination where the receiver would use the reversed mapping to get the original message.
The above example is very simple and trivial. Unfortunately, in reality this kind of cryptographic algorithm is unusable, and it's prone to *cryptographic attacks*.
Cryptographic attack is a means by which a third party, which is not supposed to receive an unencrypted message, attempts to break the encryption method employed on the message in order to obtain the original, unencrypted message. Following our example above, it's pretty obvious that the encryption method used above would be easy to crack even with a paper and pen.
The method listed above is a good example of a cryptographic method which depends on an existence of a *pre-shared key* (explained in more details below), which is known to both the sender and the receiver of the encrypted message.
## Pre-shared Keys ##
Pre-shared key is a secret that is known by all the parties interested in maintaining the secrecy of information (in some cases this can also mean a single entity - for example for the purpose of archiving one's notes). When using a pre-shared key it is assumed that both parties are using the same cryptographic method in order to manipulate with the data - both for encryption and decryption. In a way, the pre-shared key provides uniqueness to the method, making it reusable with different keys. The data is supposed to be safe from eavesdropping as long as this pre-shared key is kept secret.
Probably the most wide-spread examples of pre-shared keys are *passwords*. Every person using services on the Internet, such as e-mail and the like, knows a number of passwords they use for accessing these services. It is required for both the service and the person accessing to have the same password in order to validate the person's identity.
One of the major flaws of passwords is that they're usually comprised out of a limited set of characters (letters, digits, punctuation signs), and they're very often short (this is a good idea to ask yourself - is my password long enough?). As such those standard passwords are usually very vulnerable to cryptographic attacks, most often by using brute force (attempting multiple combinations of characters until the right password is found).
A natural question that arises from this is whether there are methods to improve the security of passwords? Are there alternatives? Are there maybe other forms of pre-shared keys that could be used? Fortunately for all of us, the answer is yes.
One way is certainly to increase the length of the password. Another way is to increase the variety of characters the password uses. But probably the best way is to create such an algorithm that it's almost impossible to deduce the password, no matter what it is - in other words, to introduce better encryption and decryption methods. Usually these three methods are combined together, though, leading to much longer pre-shared keys with more random data and much harder algorithms to break.
There are many examples of such strong pre-shared key algorithms that are being used today, some of them being AES (in several variants), Blowfish, DES, Triple DES, etc... Lots of information can be found about them throughout the Internet, so this book won't delve much into their (gory) details.
Although use of such advanced algorithms is a huge improvement, they're still limited in one thing that they share with the passwords - the pre-shared key *must* be conveyed between the communicating parties through a safe mechanism which will prevent any kind of eavesdropping. In other words, the two parties still must be able to maintain the secrecy of the pre-shared key. Very often this means that the key must be distributed directly, hand to hand, between the parties. It cannot be transmitted in public.
Another flaw of a pre-shared key cryptography is the fact that if both parties have the same key they both can decrypt *any* data which had been previously encrypted with that key. There is also the inability of parties to separate their identities when using such keys. They cannot deduce which one of them encrypted which data. In other words, use of pre-shared keys eliminates the possibility of identity authentication and digital signature.
This leads us to a second set of questions:
*Is there any way to transmit some kind of secret over an insecure communications line, and is there any way to somehow identify who had sent the encrypted message?*
In short - yes. The solution lies in *public-key cryptography*.
## Public Key Cryptography ##
The core of the public-key cryptography relies on having a pair of two distinct keys, each used for a different purpose. The first key is usually called *private key*. The second key is referred to as a ''public key''. Those two keys are not selected at random, though. Those two keys are actually generated in such a way that they are ''connected'' with each other.
Private key is generated at random, while the public key is deduced from it. The methods used in this process are such that reverse is not possible. Private key cannot be deduced from the public key (or, to be more precise, it's too difficult and time-consuming to do so).
Private key can be used for encrypting, decrypting, and signing data. On the other hand, public key has limited functionality. It can only be used for encrypting data and verifying data.
The main advantage of such mechanism is that public key can be, by any means, be distributed to anyone over any kind of secure or insecure channel without risking the private key compromise. The use of public key is limited to *non-destructive* operations (security-wise). It cannot be used for decrypting content, nor cant it be used for generating false signatures.
A small example is in order. As it usually happens with this kind of things, Alice and Bob will be our guinea pigs.
Let's say that Alice and Bob wish to exchange some important business messages between each-other. Due to secret nature of these messages, they do not wish anyone else to be able to listen to those messages. They decide to use public-key cryptography to this end.
Alice and Bob both generate their own pair of private and public keys. Alice generates her private key P(A), and from it derives her public key U(A). Similarly Bob performs the same action, obtaining his private and public key - P(B) and U(B).
Now Alice and Bob arrange to exchange their public keys. They do this over insecure line, verifying upon reception that they have indeed obtained each-others public keys (let's say by a telephone). Now Alice has the P(A), U(A), and U(B) keys. Bob has the P(B), U(B), and U(A) keys.
At some point Alice decides to send Bob a message. Upon finishing the writing, she first uses Bob's public key - U(B) - to encrypt the message. The message can now be only read by Bob using his private key to decrypt it. After that she also decides that she'd like to sign the message so that Bob could be sure it's really coming from her and not from some impersonator. So, in order to achieve this she also signs the message using her private key P(A). Finally she sends out the message to Bob.
Bob receives the message. Seeing that the message seems to be signed, first he uses Alice's public key - U(A) - to verify the message integrity. Once he's verified this, he sees that the message is also encrypted with his public key. Using his private key - P(B) - he manages to decrypt the message and read it. Upon reading the message, he sits down, writes a reply, and uses the same procedure for securing the message. He encrypts it using Alice's public key - U(A) - and signs it using his own private key - P(B).
Let's say that during all those transmissions above an evil hacker, Eve, is eavesdropping and picking up all the communication between Alice and Bob. During this process she has managed to obtain Alice's and Bob's public keys and a private message sent from Bob to Alice.
Eve first attempts to verify who from the message really is. Seeing that the message is marked as signed by Bob, she uses Bob's public key - U(B) - to verify the message. She succeeds, but now finds herself in trouble. All she's got are the public keys, and using those she is unable to decrypt the content of message. She may have intercepted the communication, and may know that Alice and Bob are exchanging important information, but the content of messages remains mystery to her. Public-key cryptography has served its purpose of securing the communication between Alice and Bob.
The most popular public-key cryptography algorithm in use today is RSA (Rivest Shamir Adleman). The details of this algorithm fall outside of scope of this book, but there is some nice introductory reading (as always) at [Wikipedia](https://en.wikipedia.org/wiki/RSA).
The remaining chapters of this section deal with the trust models, which have been only slightly touched-upon within the examples above.