Learn Cryptography #4 - Encryption using Python | "Symmetric" | "Block Cipher"

in #utopian-io7 years ago (edited)

learn cryptography.png

Hey Guys!!..

In this tutorial, we will be learning "Encryption using Python". Basically, encrypting any content using Python programming language.

We use encryption in many applications like - Chat apps, etc.

Previous tutorials in "Learn Cryptography" series:

  1. Learn Cryptography #1 - Hashing vs Encryption
  2. Learn Cryptography #2 - Hashing using Python
  3. Learn Cryptography #3 - Hashing using Python - part 2 | "File Integrity Checker "

Here, we will cover Symmetric encryption.

Follow the installation here
There are 2 types of ciphers used - Block and Stream.

  • Block - fixed size (8 or 16 bytes). see Wiki
  • Stream - byte-by-byte. see Wiki

Block ciphers

Let's use the simplest algorithm first i.e. DES.

Data Encryption Standard (DES) is a symmetric encryption algorithm.
des_structure.jpg

In the image above, it is described:

  • uses 16 round Fiestal structure (i.e. symmetric encryption)
  • Plain-text --> 64-bit or 8-bytes (i.e. multiple of 8 in length, otherwise ERROR occurs).
  • key --> 64-bit or 8-bytes (actually 56-bit, rest 8 bits are not used).
  • Cipher-text --> 64-bit or 8-bytes.

Installation

Install an additional library for DES algorithm using pip in cmd as follows:
pip install pydes
For more, refer here

Performance:The code (of this package) is not written for speed or performance, so not for those needing a fast DES implementation, but rather a handy portable solution ideal for small usages. The speed at which pyDes encrypts/decrypts is around 10Kb/s (using the DES method) - that's very SLOW!!

Coding

# import the DES library using a customized package
import pyDes
# import this library for generating random number by the system
import os
# input
i = input("Enter any string: ")

# Padding function: add ' ' until the string length is multiples of 8
def padded_text(s):
    while len(s)%8 !=0 :
        s += ' '
    return s

p = padded_text(i)

1.png

There are different modes of generating keys -

ECB

The message is divided into blocks, and each block is encrypted separately.


# key should be 8 bytes long.
k_ecb = pyDes.des("DESCRYPT", pyDes.ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=None)

# encrypted data i.e. in bytes
e_ecb = k_ecb.encrypt(str.encode(p))
print("\n The encrypted string(in bytes) - \n", e_ecb)

# extract the input text from the encrypted input using decryption
d_ecb = k_ecb.decrypt(e_ecb)
print("\n The actual input(in bytes) -  \n", d_ecb)

2.png

CBC

each block of plaintext is XORed with the previous ciphertext block before being encrypted.


Initialisation vector IV can be done using
os.urandom(8) # 8-bytes in this case

Source

When generating random data for use in cryptographic operations, such as an initialization vector for encryption in CBC mode, you do not want to use the standard random module APIs. This is because they do not provide a cryptographically secure random number generator, which can result in major security issues depending on the algorithms in use.

Therefore, it is our recommendation to always use your operating system’s provided random number generator, which is available as os.urandom().

# key should be 8 bytes long. IV vector given some random value (8 bytes for XOR operation).
k_cbc = pyDes.des("DESCRYPT", pyDes.CBC, os.urandom(8), pad=None, padmode=None)

# encrypted data i.e. in bytes
e_cbc = k_cbc.encrypt(str.encode(p))
print("\n The encrypted string(in bytes) - \n", e_cbc)

# extract the input text from the encrypted input using decryption
d_cbc = k_cbc.decrypt(e_cbc)
print("\n The actual input(in bytes) -  \n", d_cbc)

2.png

NOTE: Now a days, its key length is too short. And also not secure as it can be brute-forced with some effort.

For more, read here.

Triple DES (3DES) is an improved version of DES.
It is secure. key-length is long in this case.
See Wiki

applies the DES cipher algorithm three times to each data block.

That's it for now.

Stay tuned for more such content...

Follow the series in Github



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Glad i found you all i am gonna say for now going to go watch the rest of your posts before commenting further cheers bud.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @abhi3700 I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

If you go to a coffee shop or at the airport, and you're using open wireless, I would use a VPN service that you could subscribe for 10 bucks a month. Everything is encrypted in an encryption tunnel, so a hacker cannot tamper with your connection.

I've eaten lion, leopard, crocodile, python. I don't recommend lion. It tastes exactly like when a tomcat comes into your house and sprays. Snake and crocodile are great - a cross between lobster and chicken.

Coin Marketplace

STEEM 0.21
TRX 0.27
JST 0.040
BTC 102367.04
ETH 3705.53
USDT 1.00
SBD 3.23