While I was studying the time consumed by some methods I found that encryption take more time than decryption. Is this right? I am using AES (the same steps will be taken during the enc and dec)
Several symmetric block ciphers (specifically ones like AES, DES, Blowfish, RC5) will take the same amount of time (within measurement error) for encryption and decryption, when operating on a single block (e.g., 128-bits for AES).
However, there are a couple reasons why it appears different when encrypting/decrypting multiple blocks. For example, with cipher-block chaining (CBC), encryption must be done sequentially (encrypt block 0 before you can encrypt block 1 before you can encrypt block 2 ...), while decryption can be parallelized as the XOR step (with the previous block of ciphertext) is done after the block cipher is applied.
See the diagrams below (XOR is denoted with a circled plus ⊕). To encrypt the second block of plaintext
p, you use
c = AES_Encrypt(p XOR c), this means you can't generate
c until you are finished generating
Meanwhile to decrypt the second block of ciphertext, you use
p = c XOR AES_Decrypt(c). This is not dependent on the previous block of plaintext
p so can be completely parallelized (and can run much faster on multi-core systems).
If you want fast decryption and encryption, you may consider using CTR as it can be parallelized on both encryption and decryption.
You also potentially should be wary of tests that are skewed because of caches on your system. If you randomly decide to read a file from disk and encrypt it, it will take several milliseconds to read each chunk of the file; however the next time you access it the file will generally be cached in memory and be accessed much quicker. Meanwhile for your decryption test that encrypted file that was recently written to disk will likely still be in a cache in memory and will not suffer the penalty from reading from disk.
$ time openssl aes-256-ctr -e -salt -pass pass:passwd -in Fedora_64-bit.vmdk -out vmdk.encrypted real 0m58.217s user 0m5.788s sys 0m8.493s $ time openssl aes-256-ctr -e -salt -pass pass:passwd -in Fedora_64-bit.vmdk -out vmdk.encrypted real 0m34.780s user 0m4.800s sys 0m7.748s $ time openssl aes-256-ctr -e -salt -pass pass:passwd -in Fedora_64-bit.vmdk -out vmdk.encrypted real 0m34.989s user 0m4.120s sys 0m6.444s $ time openssl aes-256-ctr -d -salt -pass pass:passwd -in vmdk.encrypted -out decrypted real 0m35.944s user 0m4.140s sys 0m7.008s
The first three encryptions show the effect of the disk cache, where the first access of a file was much slower than subsequent accesses. It also shows that for CTR that both encryption / decryption of this 7.4 G virtual machine took roughly 35 seconds. (On repetition occasionally, decryption would be faster or slower).