Saturday, 30 March 2013

Cryptography Basics and Implementation with Borland Delphi 7



This document was created with the aim to promote and Meng-kriptografikan cryptographic community. Because, there are many comrades who considers cryptography as a science that is very high and difficult to learn, and some even categorize this as an occult science. : D I'm not going to say that the science is easy, and can be learned overnight. Not friends, all that good science takes the process.


Document is not meant to be the main reference for cryptography. Because cryptography is too broad to be recorded in a book as thick as a novel Lord of the Ring, let alone in a single document like this facebook! : D more appropriate if the document is used as a "signpost" to the world of cryptography exciting and fun with the possibilities of exploitation without limit. To my friends who have higher knowledge and experience in this field, with all humility, I ask permission and understand the mistakes that may exist in this document.
The initial goal of cryptography is to hide the message content that can not be read by those who do not want, but can still be understood by the addressee. With changing times, cryptography is widely applied in various fields, ranging from information exchange in the second world war, to the computerized world we experience today.
This document, of course, would not discuss how the Nazis operated "Enigma", thus confusing the allies. This document is "only" going to discuss cryptography and its implementation in the field of IT. Implementation will be demonstrated as well just basic implementation and is "offline", not sophisticated implementations such as "transaction mail," "pay pal autorization", "cryptographic handshake", etc.. But, believe me guys, all sophisticated implementations, based on the basic implementation.
In this document will use the term commonly used in cryptography documents. An example is the "plain text", which is the original message that would be submitted. "Cipher text", which means that the message has been encrypted. And other terms that may be confusing, sorry if that happens.
True to its title, this document uses Borland Delphi 7 as the compiler used codes. Why? Yep, because I was most comfortable using Delphi with versions for things that smell cryptography. You can choose to use another compiler is your favorite. For cryptographic library which is used as a reference, I always entrust to bang Barton, on the site: http://www.cityinthesky.co.uk/opensource/dcpcrypt. In addition to open source, the reason I use it is a complete library of cryptographic methods are used. But again, this is not binding. You are free to use any library that suits you, or if you have quite understand the flow of the algorithm is a method, you can write your own libraries.
Implementation files are included in this document is the source that is ready to be compiled. For my friends who have Borland Delphi 7 installed on their computers, can directly click the file with the extension "dpr". For those who do not install the compiler, to know the content of the program code, can open a file with the extension "fit" with notepad or other text editor. File "exe" is also included to see the direct implementation.
From the resulting output, cryptography can be divided into two, namely cryptography "one-way" and "two-way". And in cryptography "two-way" was, in view of the kind of "key" that is used, can be divided into two, namely "symetric key" and "Asymetric key". That's about the framework of this document. Although fragmented as it is, did not rule out any type of cryptography are implemented together, or better known as the "hybrid". At the end of the document, will be demonstrated implementation of this hybrid method.
In the implementation of the IT world, plain text inputted into cryptography, the shape can vary. Can exe files, documents, pictures, music, and all other digital files. Depending on the user Meng implement it. This is why the possibilities become infinite in cryptography and very interesting! :)
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
Cryptography One Direction
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
As the name implies, cryptography can only run in one direction. Or in other words, can only transform plain text into cipher text, but can not return the cipher text into plain text. Cryptography type is often referred to as a "hash". The special feature is a hash, cipher text generated different (unique) for any plain text in the input. Because of this special feature, the hash is often used as a "fingerprint" of a file or document.
Methods commonly used hash is MD5, SHA1, RipeMD128. Actually there are many other methods, but the implementation will be presented, the hash method used is the third method. Implementation can be downloaded at:
http://www.mediafire.com/?4yz9vj0v8sbydf3
By executing the file "exe" of these implementations, we can see that the tool is able to calculate the hash of the plain text or text with the type of file. But, do not try to calculate the hash of the file size> = 10 MB with this tool, unless your processor is quite capable. For indeed, the implementation is designed with no attention to optimization of reading of the file. Please optimizations at will. :)
In this implementation, the main code is in the file "Unit1.pas". File "DCPmd5.pas", "DCPsha1.pas" and "DCPripemd128.pas" is a library of relevant methods provided by DCPCrypt. Meanwhile, the file "DCPcrypt2.pas", "DCPconst.pas", "DCPblockciphers.pas" and "DCPbase64.pas" is a file that is "required" by
DCPCrypt to be placed in the same folder as the main code. Or if you've installed the Borland Delphi 7, you can see it all by opening the file "Project1.dpr". Other files can be ignored, because it is the result of "generated" from the compiler.
Okay, let's try surgical "source code" of the tool is to clarify the implementation of cryptographic one-way or "hash" is. Let's look at the main code in line 42 to line 100. There we can see the three functions to calculate the hash of each method. These functions are made by me based on the instructions recommended by DCPCrypt use, which means the function is not "standard", and yes, you can create different versions according to the needs of the implementation you want.
Of the three functions, we can see that the function takes one parameter that has type String, and String will return as well. This I did with a view to maintaining initial format cryptography, from Plain Text Cipher Text-shaped string into string form as well. But again, this is not standard, you can change the type of input parameters into an array of bytes for example, to read the stream.
Then in line 122 to line 171 in the main code, we can see the hash calculation done when user clicks on the "Hash It". We can see, if the user chooses to use the mode "Text", then the code is quite simple, just assign variable "strPlain" with what he was input by the user. Its a bit "tricky" is if the user selects the mode "File", or calculate the hash of the file is selected. Various approaches can be done here. Given three functions for calculating hash Plain Text'd need to form a string, then the approach I chose was to change all the "content" file into a string, then assign that string into strPlain. After variable strPlain ready, we put the variable as a parameter in a function to compute a hash with user selected method.
That is just one example of a one-way cryptographic implementations. This implementation can be used to check the aslian a file, because when a file is modified, the hash value will change. If you are interested to see the details of the algorithm of each method, you can see the file "pas" is concerned. Or my advice, try to debug the program, so you can "feel" how the algorithm flow.
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
Cryptography Two Directions
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
This type of cryptography may often referred to as cryptographic "real", because it has the "encrypt" to transform plain text into cipher text, and the "decrypt" to restore cipher text into plain text. In this type of cryptography, in addition to plain text and cipher text, it takes another parameter called "key". Currently there are two types of keys are used, the "Symmetric Key" and "Assymetric Key".
The thing to remember in this process is a two-way cryptography, encrypt and decrypt process should have a conflicting operation. For example, the encrypt process we use the operator "+" (plus), then decrypt the process we have to use the operator "-" (minus). Therefore, the implementation of the encrypt and decrypt kriptografinya made into two different implementations, but complementary. To decrypt processes, implementation will emulate a software serial verification process, while the process encrypt, implementation will act as a "keygen" it.
--------
Symmetric Key
--------
What is meant by symmetric key or symmetric key is, key parameters used in the encrypt and decrypt, the same. The length of this key parameter varies depending on the method used. In the implementation of which will be given, the method used is RC4 and Blowfish, with a key length of 128 bits or 16 bytes.
For some reason, the implementation will be given and discussed starting from the first decrypt. Decrypt Implementation can be downloaded at:
http://www.mediafire.com/?iefq1v1pzvqbt9l
Again, this implementation emulate a serial verification software, but of course, is made as simple as possible, so that the more prominent kriptografinya. Thus, this implementation is a "recipient" message, then tried to decrypt the message and posted verikasikan whether the message originated from the "sending" a message is legitimate or not.
The files contained in this implementation, it is not much different from what is on the implementation of cryptographic one-way, only the library has changed the method in question. Main code, again located in the "Unit1.pas". Let us open the code to start dissecting this implementation.
Lines 33 through to 87, shows two function to download decrypt with each method. The process decrypt the two methods is made uniform in order to avoid confusion, and certainly not a standard form. What is different of course, the core algorithm of each method are contained in the *. Fitting respectively. Because of this uniformity, the discussion of the method can represent only one method.
On line 35 we can see, the initialization of key constants are prepared to decrypt process. Key was made with the format of the byte array with width 16. Or, in other words, has a key length of 16 bytes, or 128 (16 * 8) bits. It should be noted is that in Delphi, the notation "$" indicates that the number is written in hexadecimal form. Thus, the declaration of the array could be written like this:
key: array [0 .. 15] of byte = (71,114,105,110,100,83,116,111,110,101,73,115,71,111,111,100);
Above the array declaration using decimal notation, with the same value. Then, why the implementation is written with hexadecimal notation? I just love hex, nothing more.
Okay, let's look at lines 39 and 40. There are two variables declared array to hold the input and output of the process decrypt it. Because what is done today is the decrypt, then the input is accommodated in the Cipher Text arrCipher variable, while the output is accommodated in Plain Text arrPlain variable. Both variables are created equal length is 8 bytes. It is worth noting, because it will affect the use of this function, we will get to it later.
Decrypt function accepts a String as a parameter in the form of input, as shown in line 33, while variables that are inputs to the core decrypt shaped array of bytes, as shown in line 51. Thus, lines 44 to 48, the duty to convert the String variable into an array of bytes. There are many methods that can be used for this task, such as taking the ASCII value of each character in the string, and then store them into an array of bytes. However, in this implementation, the method to be used is, take every 2 (two) characters from the string, and then added the notation "$" in front of it to then change into the form of a byte (integer) and stored in the array. And this is done with the loop as much as 8 (0-7) rounds, because the length of an existing variable placeholders (arrCipher) is 8. Therefore, the length of a valid string to be input into this function is 16 (8 * 2) character, and the character that is to be character hexadecimal number, ie 1-9 and AF. Let us take an example to clarify:
For example, strings that are input are:
"1234567890ABCDEF"
Then the array of bytes that is formed is:
($ 12, $ 34, $ 56, $ 78, $ 90, $ AB, $ CD, $ EF)
or in decimal form is:
(18, 52, 86, 120, 144, 171, 205, 239)
Hopefully the above is clear enough to describe the "flow" of such conversion algo.
Lines 49 through 52 is the code to decrypt the core process required by this DCPCrypt library. And the results of the core is stored in decrypt variable arrPlain shaped array of bytes. Line 53 to prepare (empty) strResult variable that will hold the result of the function decrpyt this. If the input we had to convert from String variable to an array of bytes, now we are doing the opposite for output, change the variable array of bytes into a String. And that is done on lines 54 to 57.
The method used now is take each byte of the array, and then download the subtitusikannya with the character that has the ASCII value is concerned, to be added in the strResult variable. This method is much simpler than previous methods, but let's take an example to better clarify:
For example, the variable arrPlain are:
(71, 119, 84, 97, 109, 112, 97, 110)
then in the strResult variable is created:
"GwTampan"
Hopefully, the above example could clarify what exactly is self-explanatory. : D
Thus the explanation of the functions that used to decrypt. These functions need to be fully understood if we are to make the process necessary to make encrypted keygen of "software" verification. We will discuss it in the next implementation. For now, let's go to the core functions of the program, which is executed when the user presses the button "Check".
This function is, of course, lies in the lines 89 to 144. Seen a lot of ramifications "if" in there, which may confuse some people. If that's the case, with all humility, I apologize for my poor pemogramman way. But I think, these subdivisions do not need to discuss all, because just to check basic errors from user input. Its worth mentioning is branching on line 113. You can see that if the "Name" the user inputted more than 8 characters, it is taken only the first 8 characters only. And if the "name" that is inputted is less than 8 characters, then the variables are duplicated continuously for more than 8, to then take the first 8 characters. Again, an example to clarify:
For example, the name of the input is:
"CryptographyIsSuperFun"
then, taken only:
"Cryptogr"
And, if the name of the input is:
"FUN"
hence, the result will be:
"FUNFUNFU"
With this, we are "forced" strName variable to fixed length of 8 characters, no matter how many characters are inputted by the user. Why? Before you answer, I hope you still remember the characteristics of the above function decrypt earlier, valid input must be a string with 16 characters (this explains the ramifications on line 107), and the resulting output is a string with 8 characters. And on line 133, there is a "final check" that compares the variable strName strPlain which is the result of the series that Decrypt inputted. Auto, strPlain will always have a length of 8 characters, that's why we need to "force" strName to still have a length of 8 characters.
Actually there is a little "bug" that is quite disturbing in this implementation. That is, when we enter the serial with length of 16 characters, but the character of the input is not a hexadecimal character, it will display an error message that is less good. : D But of course, it can be overcome by adding branching to check whether the serial input is hexadecimal character or not. You can add it if you want! : D :)
Yep, so just one example of the implementation process that mimics decrypt a series of software verification process. To complete it, all that remains is the process that will be applied to encrypt keygen for verification.
Okay, implementation to encrypt process can be downloaded at:
http://www.mediafire.com/?coc0u4o61wc6jb2
In this implementation, if we execute the file "exe", then we can generate a valid serial verification prior to implementation. For indeed, this implementation acts as a keygen or in other words as a "sender" message. While the implementation of the previously acted as a "recipient" message.
The files that exist in the implementation is the same as the one on the implementation of the verification, only the file "exe" and "Unit1.pas" is different, because it uses a library implementation of a method similar to the implementation of verification. Let's open the main code is in the implementation "Unit1.pas" to begin this exciting surgical codes.
To remember before surgery is, that the implementation process is interdependent with keygen on implementation verification code. And, we must see that this keygen code conflicting with verification. Which means, if verification of "open", then the keygen to "cap", if verification did "add", then the keygen do "less", and so on.
Okay, we start of surgery. We can look at line 33 to 83, there are two functions encrypt that, of course, be made uniform, as well as on implementation verification. And yes, as before, the discussion of the method can represent only one method. Line 35 shows the key constants had to be equal in value to decrypt processes, so that the two processes are interrelated.
Because the process is contrary to encrypt decrypt process, then we will do the discussion in reverse from the bottom up, while reviewing the verification code. So it's good, if we open the verification code (decrypt) and the keygen (encrypt) simultaneously, unless you are already familiar and understand the overall verification code.
In lines 44 to 48 in the verification code, as already discussed, that the function decrypt take any two characters from strings that are input or cipher text and then convert it into a hex value and store it into an array of bytes. Thus, we encrypt function that produces a valid cipher text for the process, we should do the opposite function, yes, change the array of bytes into a string with hex format. Lines 51 to 55 do so. The built-in Delphi "IntToHex" generate hex representation of the integer (or byte) is inputted. For example, please review the above example the verification process, but read from the bottom up.
Then in lines 49 through 52 in the verification code, function decrypt decrypt perform core functions in accordance with the method used for an array of bytes converted from strings that are input before (cipher text). So on line 47 to 50 in the code keygen, encrypt function encrypts must perform core functions in accordance with the method used for an array of bytes that will be converted to a string output (cipher text), as discussed in the previous paragraph.
Then in line 54 to 57 in the verification code, decrypt function again convert the array of bytes that have been in "decrypt" into a string, but this time by way of substitution byte values ​​with corresponding ASCII values, or in other words to convert byte to ASCII. Thus, the function encrpyt we should do the opposite, yap, converting ASCII to bytes. Lines 43 to 46 on the code keygen demonstrate this, and yes, the built-in Delphi "Ord" help us to do so. And again, for example, please see the example "GwTampan" on top from the bottom up.
That the contents of the encrypted function which is the inverse of a function decrypt. You can close the verification code now, if you want. Next, let's discuss the use of the encrypt function is, when the user presses the "G 3 N" located on lines 85 to 119.
Not much was done in this code. Lines 97 to 108 did the same to that of the code verification, ie forcing plain text it has a length of 8 characters, as discussed previously. And, if you have not noticed, for the name "CryptographyIsFun" and "CryptographyIsSuck" will result in the same series, because this coercion. Then in lines 109 to 117, the code did encrypt the name of the (plain text) into cipher text inputted then be output to serial.
Thus the whole process of implementation of this symmetric key cryptography with. And okay, to further clarify, let us summarize the whole process with the direction of the "straight" or to decrypt encrypted. This time the keygen will we call the sender, and the verifier shall we call recipient.
First, download the encrypted sender name in the input-to-output then be serialized. Then, the sender sends couples name and the recipient of this series. Then, the recipient spouse's name and serial check is sent in a way, download serial for decrypt later than the name. If appropriate, the spouse's name and serial is valid and it comes from a genuine sender, if it does not match, then the sender is fake.
Yep, like that's one example of the implementation of two-way cryptography with symmetric keys. We are sorry if the explanations seem convoluted and confusing, but I have tried my best to explain things that are very technical. And yes, try to "feel" how the code is running, make it easier to understand. :)

1 comments:

  1. Yes its true that cryptography is difficult to learn and study but its a very interesting concept. The detail which you have posted is correct and easy to understand.
    eSignature

    ReplyDelete