8920731afc1f33f15f9ba1311ba02bab
8920731afc1f33f15f9ba1311ba02bab
8920731afc1f33f15f9ba1311ba02bab
Introduction
1.1 Introduction
Data exchanging via internet has become an inevitable factor in our day to day life. A variety of
confidential data and information are exchanged through internet including ATM passwords,
personal details, bank dealings, medical records and information etc. If the confidentiality of the
information is very high it should be protected. Digital documents face more threats as compared
to analog documents as they are easier to copy and distribute. In today’s corporate world, access
to information in lesser time is required for the goal of running the enterprise smoothly and
efficiently. Moreover, it is very important to provide right information to the right people at the
right time. As a result security and privacy of the world’s information has become a prime
concern. Cryptography is one such technique which is responsible for secure transmission of
information.
There are a number of cryptographic techniques that can be used to assure security and privacy
of information or data transmission. But all of them don’t provide the same performance as there
are considerable differences between each technique. So, selection of cryptographic techniques
has become a major problem. We must have sufficient knowledge in choosing the cryptographic
method which provides greater advantages than the others.
1.3 Objectives
1
1.4 Related Works
1. Nidhi singhal and J.P.S Raina, “Comparative Analysis of AES and RC4 algorithms for Better
Utilization”.
2. S. Soni, H. Agarwal, M. Sharma, “Analysis and comparison between AES and DES
Cryptographic Algorithm”.
3. Pechalaiah, N. and Seshadri “Effective Comparison and Evaluation of DES and AES
Algorithm”. 4. Singh, S Preet and Maini, Raman. “Comparison of Data Encryption
Algorithms”
2
Chapter 2
Background & Literature Review
3
✓ Plain Text: The original message that the person wishes to communicate with the other is
defined as Plain Text. In cryptography the actual message that has to be send to the other
end is given a special name as Plain
Text. For example, Alice is a person wishes to send “Hello Friend, how are you” message
to the person Bob. Here “Hello Friend, how are you” is a plain text message.
✓ Cipher Text: The message that cannot be understood by anyone or meaningless message
is what we call as Cipher Text. In Cryptography the original message is transformed into
non readable message before the transmission of actual message. For example,
“Ajd672#@91ukl8*^5%” is a Cipher Text produced.
✓ Key: A Key is a numeric or alpha numeric text or may be a special symbol. The Key is
used at the time of encryption takes place on the Plain Text and at the time of decryption
takes place on the Cipher Text. The selection of key in Cryptography is very important
since the security of encryption algorithm depends directly on it.
✓ S-Boxes: In cryptography, an S-box or substitution box is a basic component of
symmetric key algorithms which performs substitution. In block ciphers, they are
typically used to obscure the relationship between the key and the cipher text.
i. Asymmetric Cryptography
ii. Symmetric Cryptography
Asymmetric cryptography, is a cryptographic system that uses pairs of keys: public keys which
may be disseminated widely, and private keys which are known only to the owner. The
generation of such keys depends on cryptographic algorithms based on mathematical problems to
produce One-way functions. Effective security only requires keeping the private key private; the
public key can be openly distributed without compromising security. In such a system, any
person can encrypt a message using the receiver's public key, but that encrypted message can
only be decrypted with the receiver's private key. Asymmetric cryptography is also known as
public key cryptography.
4
Fig 1: Asymmetric Cryptograph y
Symmetric cryptography is a technique that uses the same cryptographic key for both encryption
of plain text and decryption of cipher text. The keys may be identical or there may be a simple
transformation to go between the two keys. The keys, in practice, represent a shared between two
or more parties that can be used to maintain a private information link. This requirement that
both parties have access to the secret key is one of the main drawbacks of symmetric encryption,
in comparison to asymmetric encryption.
5
Fig 2: Symmetric cryptography
There are hundreds of different symmetric key algorithms available. Each has its own strengths
and weaknesses. Some of the more common examples are DES, 3DES, AES, Blowfish, Twofish,
IDEA, RC4, and RC5 etc. In this paper we will compare AES, DES Blowfish algorithm.
2.3.3 AES
AES also known as Rijndael, is an encryption standard recommended by NIST in 2001. AES has
a fixed block size of 128 bits a key size of 128, 192, 256 bits. The algorithm is referred to as
AES-128, AES-192, AES- 256 depending on the key length. During encryption-decryption
process, AES system goes through 10 rounds for 128 bit keys, 12 rounds for 192 bit keys and 14
rounds for 256 bit keys in order to deliver final cipher text or to retrieve the plain text. AES
allows a 128 bit data length that can be divided into four basic operational blocks. These blocks
are treated as array of bytes and organized as a matrix
of the order of 4×4 that is called the state. AES is based on a design principle known as a
Substitution permutation network. AES does not use a Feistel network. Four different stages are
used.
6
• Substitute bytes: Uses an S box to perform byte by byte substitution of the block.
• Mix columns: A substitution that makes use of arithmetic over finite field.
• Add round key: A simple bitwise XOR operation of the current block with a portion of
the expanded key.
The first n-1 rounds consist of four stages. The final round contains only three stages. There is
initial single stage before the first round that can be considered as round 0. There is a key
expansion function that generates (n+1) round keys. Decryption is the reverse process of
encryption and using inverse functions: Inv. Sub Bytes, Inv. Shift Rows, Inv. Mix Columns.
2.3.4 DES
DES was first adopted in 1977 by NIST. For DES, data are encrypted in 64 bit blocks using a 56
bit key (initially 64 bit). DES is based on the concept of feistel structure. There are 16 rounds in
total and the sub key size is 48 bit. The algorithm transforms 64 bit input in a series of steps into
64 bit output. DES results in a permutation among the 2^64 possible arrangement of 64 bits.
Each block of 64 bits is divided into two blocks of 32 bits each, a left half block L and right half
R. The DES algorithm turns 64-bit messages block M into a 64-bit cipher block.
2.3.5 Blowfish
Blowfish is a symmetric block encryption algorithm, designed in 1993 and included in many
cipher suites and encryption products. Blowfish provides a good encryption rate in software and
no effective cryptanalysis of it has been found to date. Blowfish has a 64-bit block size and a
variable key length, from 32 bits up to 448 bits. It is a 16-round Feistel structure and uses large
key-dependent S-boxes.
7
of this paper shows that blowfish has a better performance than other common encryption
algorithms. AES showed poor performance results compared to other algorithms since it requires
more processing power.
(Nadeem, 2005) In this paper, the popular secret key algorithms including DES, 3DES, AES
(Rijndael), Blowfish, were implemented, and their performance
was compared by encrypting input files of varying contents and sizes. The algorithms were
implemented in Java programming, using their standard
specifications, and were tested on two different hardware platforms, to present the comparison.
The two different machines are: P-II 266 MHz and P-IV 2.4
GHz.
(Dhawan, 2002) has also done experiments for comparing the performance of the different
encryption algorithms implemented inside .NET framework. Their results are close to the ones
shown before. The comparison was performed on the following algorithms: DES, Triple DES
(3DES), RC2 and AES (Rijndael). The results shows that AES outperformed other algorithms in
both the number of requests processes per second in different user loads, and in the response
time in different user-load situations.
(N. Penchalaiahet.al., 2010) discussed the principal advantages of AES with respect to DES, as
well as its limitations. They said that AES can be quite comfortably implemented in high level or
low level languages.
(Elminaam et. al., 2010) presented a comparison of AES, DES, 3DES, RC2, Blowfish and RC6.
They used different settings for each algorithm such as different sizes of data blocks, different
data types, battery power consumption, different key size and finally encryption/decryption
speed. They concluded that in case of changing packet size Blowfish showed better performance
than other algorithms followed by RC6. AES had better performance than RC2, DES, and
3DES. In case of changing key size – it was concluded that higher key size leads to clear change
in the battery and time consumption.
(Singhal and Raina, 2011) presented a comparative analysis between AES and RC4 for better
utilization. In this paper authors tried to find out performance comparison between block ciphers
(AES) and stream cipher (RC4) algorithm. Based on the analysis and result, this paper concluded
that which algorithm is better to use based on different performance metrics.
8
Seth Shashi Mehrotra, Mishra Rajan made a comparative analysis of three algorithms, DES, AES
and RSA considering certain parameters such as computation time, memory usages and output
byte. A cryptographic tool was used for conducting experiments. It was concluded that RSA
consumes longest encryption time and memory usage is also very high.
9
Chapter 3
Methodology
3.1 Methodology
This section describes the techniques and simulation choices made to evaluate the performance
of the selected algorithms. In addition to that, this section will discuss the methodology related
parameters like: system parameters, experiment factors and experiment initial settings.
In this experiment, we will encrypt and decrypt different sizes of data blocks using the same key
with our selected algorithms. We will also encrypt and decrypt same block of data using different
keys with our selected algorithms. These implementations are thoroughly tested for analyzing the
performance of each algorithm.
• Encryption Time
• Decryption Time
• Encryption Throughput
• Decryption Throughput
• Decrypted Text Size
• Avalanche Effect
The time taken to convert the plain text into cipher text is called encryption time. Encryption
time impacts performance of the system. It must be less making the system fast and
responsive.
The time to recover plain text from cipher text is called decryption time. It is also desired to be
less to make the system fast and responsive. We measured both encryption and decryption
time in milliseconds.
10
3.2.3 Encryption throughput
In the case of Encryption scheme throughput is calculated as the average of total plain text in k
bytes divided by the average Encryption time.
The execution results are taken on machine Intel® Core™ i3-6006U CPU @ 2.00 GHZ with 4
GB RAM and 64-bit Windows 10 operating system. The python platform is used for
implementation. Python 3.0 and Pycharm, an integrated environment for python computer
programming language are used for encryption algorithm implementation.
11
Algorithm: AES, DES, Blowfish
12
Plaintext
MS Excel
13
3.5 Block Diagram of AES 128 Encryption & Decryption
14
3.6 Block Diagram of DES Algorithm
15
3.7 Block Diagram of Blowfish algorithm
16
Chapter 4
Results & Discussion
4.1 Result Analysis
To calculate the parameters three algorithms are generated in python programming language. In
this experiment, all tests are performed using different text file.
In figure-6, the relation between encryption time and shown whereas in figure-7, relation
between decryption time and text size is illustrated briefly.
17
Fig 4.1: Encryption time Vs text size
Here, we can see that blowfish encryption is the fastest of all three. Blowfish is one of the fastest
symmetric encryption techniques in widespread use. Blowfish has strong points regarding speed
because bulk encryption reduces to an alternation of an 8 to 32 bit table lookup and one or two
32-bit operations (addition or XOR). That structure is very well suited with a short pipeline and
fast cache of at least 4KB. Thus Blowfish can be a little faster than AES and DES.
Figure 7 shows us the decryption time Vs text size. Here also Blowfish seems to be the faster
18
one. The reason is the same as encryption process.
Moreover, encryption and decryption time increases with the increase in text size. As data
increases in the text, it requires more time for each algorithm to perform encryption and
decryption process. That means there is greater complexity while operating on large files.
19
Fig 4.4: Decryption Throughput
From Fig 8 and 9, we can see that in both cases Blowfish has shown better throughput than the
other two. It takes lesser time in case of different text file. It is more efficient in terms of
throughput. The result shows the superiority of Blowfish algorithm over the other algorithms in
terms of the throughput of Decryption process. Because more the throughput; more the speed of
the algorithm & less will be the power consumption. Second point can be noticed here that AES
has advantage over DES.
Here we will compare the decrypted text of cipher text generated by each algorithm. A tabular
and a graphical form are represented for this purpose. From the table and graph we can see that
Blowfish algorithm expand the plaintext. It generates cipher text which is almost of the plain
text. In case of AES, decrypted text is not as big as the plain text. The performance of DES
algorithm is closer to that of AES algorithm.
20
Table 4.2: Plain text size Vs Decrypted Text
21
4.1.4 Avalanche Effect
Avalanche effect on AES, DES and Blowfish algorithm is displayed in the following figure. It
is tested on the same plain text.
AES 68
DES 52
Blowfish 38
Figure-11 shows that AES exhibits highest Avalanche effect whereas Blowfish exhibits least
Avalanche effect. Avalanche effect tells the degree of diffusion in our information. A change of
one bit in plain text leads to a significant change in bits of output information. AES uses a
substitution permutation network that includes multiplicative inverse and affine transformations.
It creates high mixing of information leading to high diffusion in output.
22
4.2 Result Summary
After performing all the tests, it can be seen that in terms of encryption and decryption time
Blowfish is superior to other two algorithms. It takes lesser time and also uses lesser power. It
also provides greater throughput. But exhibits weakness in decrypted text size and avalanche
effect. It has least performance in these two parameters. On the other hand, although AES
performs encryption-decryption process slower than Blowfish, it shows highest avalanche effect
making the data more secure. It also does not expand the plaintext too much. The other
algorithm, DES has not performed greatly that much. It shows better Avalanche effect than
Blowfish. That means it can be more secure than Blowfish.
23
Chapter 5
Conclusion & Future Works
__________________________________________________________
5.1 Conclusion
Each of the encryption techniques has its own strong and weak points. In order to apply a
suitable cryptography algorithm to an application, we should have knowledge regarding
performance, strength and weakness of the algorithms. From the experiment results, it is evident
that for fast software implementation Blowfish is the best choice between these three algorithms.
We can conclude that AES can be used in applications where confidentiality and integrity is of
highest priority. If cryptographic strength is a major factor in the application, AES is the best
suited algorithm. For secure communication, DES should be the second choice.
24
References
[1] Singhal, Nidhi and Raina, J P S. “Comparative Analysis of AES and RC4 Algorithms for
Better Utilization”, International Journal of Computer Trends and Technology, ISSN: 2231 -280,
July to Aug Issue 2011, pp. 177-181.
[2] Singh, S Preet and Maini, Raman. “Comparison of Data Encryption Algorithms”,
International Journal of Computer Science and Communication, vol. 2, No. 1, January-June
2011, pp. 125-127.
[3] Elminaam, D S Abd; Kader H M Abdual and Hadhoud, M Mohamed. “Evaluating the
Performance of Sysmmetric Encryption Algorithms”, International Journal of Network Security,
Vol. 10, No. 3, pp. 216-222, May 2010.
[4] Penchalaiah, N. and Seshadri, R. “Effective Comparison and Evaluation of DES and Rijndael
Algorithm (AES)”, International Journal of Computer Science and Engineering, Vol. 02, No. 05,
2010, 1641 -1645.
[5] Stallings, W; ”Cryptography and Network Security: Principals and Practices”, Prentice Hall,
8 th Edition, 2009.
[6] Tamimi, A Al; “Performance Analysis of Data Encryption Algorithms”, Oct 2008.
[8] Nadeem, Aamer; "A Performance Comparison of Data Encryption Algorithms", IEEE 2005.
[9] Comparison of symmetric and asymmetric cryptography with existing vulnerabilities and
countermeasures by Yogesh Kumar, Rajiv Munjal and Harsh (IJAFRC) Volume 1, Issue 6, June
2014. ISSN 2348 – 4853.
[10] Evaluation of Blowfish Algorithm based on Avalanche Effect by Manisha Mahindrakar
gives a new performance measuring metric avalanche effect. International Journal of Innovations
in Engineering and Technology (IJIET) 2014
[11] Efficient Implementation of AES, RituPahal, Vikaskumar, Volume 3, Issue 7, July 2013
ISSN: 2277 128X, © 2013, IJARCSSE.
[12] Seth Shashi Mehrotra, Mishra Rajan, “Comparative analysis of
Encryption algorithm for data communication”, International Journal of Computer Science and
Technology, vol. 2, Issue 2, June 2011, pp.
[13] https://en.wikipedia.org/wiki/Category:Symmetric-key_cryptography.
[14] https://en.wikipedia.org/wiki/Category:Asymmetric-key_algorithms292-294.
25
Appendix
import os
import time
import aes128
print('Step 1:')
while True:
print('Press 1 for encryption smth and 2 for decryption')
way = input()
if way not in ['1', '2']:
print('Action denied')
continue
else:
break
print()
print('Step 2:')
while True:
print('Enter full name of file')
input_path = os.path.abspath(input())
if os.path.isfile(input_path):
break
else:
print('This is not a file')
continue
print()
print('Step 3:')
while True:
print(
'Enter your Key for encription/decription. The Key must be less
than 16 symbols. Please, don\'t forget it!')
key = input()
26
alphabet and numbers')
continue
break
print('\r\nPlease, wait...')
time_before = time.time()
# Input data
with open(input_path, 'rb') as f:
data = f.read()
if way == '1':
crypted_data = []
temp = []
for byte in data:
temp.append(byte)
if len(temp) == 16:
crypted_part = aes128.encrypt(temp, key)
crypted_data.extend(crypted_part)
del temp[:]
else:
# padding v1
# crypted_data.extend(temp)
# padding v2
if 0 < len(temp) < 16:
empty_spaces = 16 - len(temp)
for i in range(empty_spaces - 1):
temp.append(0)
temp.append(1)
crypted_part = aes128.encrypt(temp, key)
crypted_data.extend(crypted_part)
# Ounput data
with open(out_path, 'xb') as ff:
ff.write(bytes(crypted_data))
27
else:
# padding v1
# decrypted_data.extend(temp)
# padding v2
if 0 < len(temp) < 16:
empty_spaces = 16 - len(temp)
for i in range(empty_spaces - 1):
temp.append(0)
temp.append(1)
decrypted_part = aes128.encrypt(temp, key)
decrypted_data.extend(crypted_part)
# Ounput data
with open(out_path, 'xb') as ff:
ff.write(bytes(decrypted_data))
time_after = time.time()
sbox = [
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf,
0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1,
0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3,
0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39,
0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
0x50, 0x3c, 0x9f, 0xa8,
28
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21,
0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d,
0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62,
0x91, 0x95, 0xe4, 0x79,
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea,
0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
0x4b, 0xbd, 0x8b, 0x8a,
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9,
0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9,
0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
0xb0, 0x54, 0xbb, 0x16
]
inv_sbox = [
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,
0x81, 0xf3, 0xd7, 0xfb,
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44,
0xc4, 0xde, 0xe9, 0xcb,
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b,
0x42, 0xfa, 0xc3, 0x4e,
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49,
0x6d, 0x8b, 0xd1, 0x25,
0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc,
0x5d, 0x65, 0xb6, 0x92,
0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57,
0xa7, 0x8d, 0x9d, 0x84,
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05,
0xb8, 0xb3, 0x45, 0x06,
0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03,
0x01, 0x13, 0x8a, 0x6b,
0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce,
0xf0, 0xb4, 0xe6, 0x73,
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8,
0x1c, 0x75, 0xdf, 0x6e,
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e,
0xaa, 0x18, 0xbe, 0x1b,
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe,
0x78, 0xcd, 0x5a, 0xf4,
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59,
0x27, 0x80, 0xec, 0x5f,
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f,
0x93, 0xc9, 0x9c, 0xef,
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c,
29
0x83, 0x53, 0x99, 0x61,
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63,
0x55, 0x21, 0x0c, 0x7d
]
rcon = [[0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36],
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
]
Args:
input_bytes -- list of int less than 255, ie list of bytes. Length of
input_bytes is constantly 16
key -- a strig of plain text. Do not forget it! The same string is
used in decryption
Returns:
List of int
"""
key_schedule = key_expansion(key)
state = sub_bytes(state)
state = shift_rows(state)
state = add_round_key(state, key_schedule, rnd + 1)
30
output[r + 4 * c] = state[r][c]
return output
Args:
cipher -- list of int less than 255, ie list of bytes
key -- a strig of plain text. Do not forget it! The same string is
used in decryption
Returns:
List of int
"""
# let's prepare our algorithm enter data: State array and Key Schedule
state = [[] for i in range(nb)]
for r in range(4):
for c in range(nb):
state[r].append(cipher[r + 4 * c])
key_schedule = key_expansion(key)
rnd = nr - 1
while rnd >= 1:
state = shift_rows(state, inv=True)
state = sub_bytes(state, inv=True)
state = add_round_key(state, key_schedule, rnd)
state = mix_columns(state, inv=True)
rnd -= 1
return output
31
def sub_bytes(state, inv=False):
for i in range(len(state)):
for j in range(len(state[i])):
row = state[i][j] // 0x10
col = state[i][j] % 0x10
# Our Sbox is a flat array, not a bable. So, we use this trich to
find elem:
# And DO NOT change list sbox! if you want it to work
box_elem = box[16 * row + col]
state[i][j] = box_elem
return state
count = 1
return state
for i in range(nb):
32
mul_by_03(state[3][i])
s3 = mul_by_03(state[0][i]) ^ state[1][i] ^ state[2][i] ^
mul_by_02(state[3][i])
else: # decryption
s0 = mul_by_0e(state[0][i]) ^ mul_by_0b(state[1][i]) ^
mul_by_0d(state[2][i]) ^ mul_by_09(state[3][i])
s1 = mul_by_09(state[0][i]) ^ mul_by_0e(state[1][i]) ^
mul_by_0b(state[2][i]) ^ mul_by_0d(state[3][i])
s2 = mul_by_0d(state[0][i]) ^ mul_by_09(state[1][i]) ^
mul_by_0e(state[2][i]) ^ mul_by_0b(state[3][i])
s3 = mul_by_0b(state[0][i]) ^ mul_by_0d(state[1][i]) ^
mul_by_09(state[2][i]) ^ mul_by_0e(state[3][i])
state[0][i] = s0
state[1][i] = s1
state[2][i] = s2
state[3][i] = s3
return state
def key_expansion(key):
33
tmp[j] = sbox_elem
else:
# just make XOR of 2 columns
for row in range(4):
s = key_schedule[row][col - 4] ^ key_schedule[row][col - 1]
key_schedule[row].append(s)
return key_schedule
state[0][col] = s0
state[1][col] = s1
state[2][col] = s2
state[3][col] = s3
return state
res = array[:]
for i in range(count):
temp = res[1:]
temp.append(res[0])
res[:] = temp[:]
return res
34
def right_shift(array, count):
"""Rotate the array over count times"""
res = array[:]
for i in range(count):
tmp = res[:-1]
tmp.insert(0, res[-1])
res[:] = tmp[:]
return res
def mul_by_02(num):
"""The function multiplies by 2 in Galua space"""
def mul_by_03(num):
"""
return (mul_by_02(num) ^ num)
def mul_by_09(num):
# return mul_by_03(num)^mul_by_03(num)^mul_by_03(num) - works wrong, I
don't know why
return mul_by_02(mul_by_02(mul_by_02(num))) ^ num
def mul_by_0b(num):
# return mul_by_09(num)^mul_by_02(num)
return mul_by_02(mul_by_02(mul_by_02(num))) ^ mul_by_02(num) ^ num
def mul_by_0d(num):
# return mul_by_0b(num)^mul_by_02(num)
return mul_by_02(mul_by_02(mul_by_02(num))) ^ mul_by_02(mul_by_02(num)) ^
num
def mul_by_0e(num):
# return mul_by_0d(num)^num
return mul_by_02(mul_by_02(mul_by_02(num))) ^ mul_by_02(mul_by_02(num)) ^
mul_by_02(num)
35