analitics

Pages

Showing posts with label pycryptodome. Show all posts
Showing posts with label pycryptodome. Show all posts

Saturday, June 27, 2020

Python 3.8.3 : PyCryptodome python package - part 001.

In the last tutorial, I wrote on Sunday, June 16, 2019, you can see a simple example of this python package with KDF with PBKDF2 function.
I guess it should be interesting for visitors to this blog to read more about this package because it is very useful and interesting.
Today I come up with another tutorial covering how to use A.E.S. standard encryption and decrypting text in a binary file.
The A.E.S. is a standard?
The Federal Information Processing Standards Publications (FIPS PUBS) announcing the A.E.S. on November 26, 2001, on the Federal Information Processing Standards Publication 197.
A.E.S. known as Advanced Encryption Standard is a symmetric block cipher standardized by NIST.
The N.I.S.T is an abbreviation National Institute of Standards and Technology.
This python package named PyCryptodome is a self-contained Python package of low-level cryptographic primitives, see the readthedocs.io webpage.
First, you need to see if this python package is not on conflict with another one named PyCrypto.
Then use pip3 tool to install.
pip3 uninstall PyCrypto
WARNING: Skipping PyCrypto as it is not installed.
...
pip3 install pycryptodome
Collecting pycryptodome
...
Installing collected packages: pycryptodome
Successfully installed pycryptodome-3.9.8
Here is the source code commented for a better understanding of the encryption and decryption steps.
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

# the data input needs to be encoded, else you will receive this error
# python TypeError: Object type  cannot be passed to C code

data = "Hello World! by catafest!".encode("utf8")
# first step - prepare for encrypt
key = get_random_bytes(16)
print("print key is: ", key)
cipher = AES.new(key, AES.MODE_EAX)
print("print cipher is: ", cipher)
ciphertext, tag = cipher.encrypt_and_digest(data)
print("print ciphertext is: ", ciphertext)

# open the binary file 
file_out = open("AES_encrypted.bin", "wb")
# write to binary file 
[ file_out.write(x) for x in (cipher.nonce, tag, ciphertext) ]

# the file is close 
file_out.close()

# next step - prepare for decrypt
new_cipher = AES.new(key, AES.MODE_EAX, cipher.nonce)

# open again the binary file 
file_in = open("AES_encrypted.bin", "rb")
# read all data from file
nonce, tag, ciphertext = [file_in.read(x) for x in (16, 16, -1) ]
# the file is close 
file_out.close()

# show data from file
print("print nonce is: ", nonce)
print("print tag is: ", tag)
print("print ciphertext is: ", ciphertext)

# create a new cipher
cipher = AES.new(key, AES.MODE_EAX, nonce)

# use this to decrypt
data = cipher.decrypt_and_verify(ciphertext, tag)
# show the result 
print(data)
The result of running the python script is this:
python.exe .\pycryptodome_AES_001.py
print key is:  b'\xbdEX\xf8\x1d!\xc5\xceI\x87\x81\xf1\xd5\xba\x8c\r'
print cipher is:  <Crypto.Cipher._mode_eax.EaxMode object at 0x000001D5F32DE100>
print cipher is:  <Crypto.Cipher._mode_eax.EaxMode object at 0x000001E062AFE100>
print ciphertext is:  b'ON\x1d\xb9\xb7\xa8\xf5\xd6\x0c\x91\xc5`B\xf4\x95u\xe1D\xb5\x88&I\x15\xc5\xc5'
print nonce is:  b'3\xa6R8\xbb\n \x9cimp$\xe4\xee\xf5-'
print tag is:  b'\xb8)\xe4\xe7\x08uE~\x84s]\xedX\xf5\xf9\xea'
print ciphertext is:  b'ON\x1d\xb9\xb7\xa8\xf5\xd6\x0c\x91\xc5`B\xf4\x95u\xe1D\xb5\x88&I\x15\xc5\xc5'
b'Hello World! by catafest!'
You can read about this process on this website.

Sunday, June 16, 2019

Python 3.7.3 : Using the pycryptodome python module.

This python module can be used with python 3.
More information can be found here.
PyCryptodome is a self-contained Python package of low-level cryptographic primitives.
It supports Python 2.6 and 2.7, Python 3.4 and newer, and PyPy.

The install of this python module is easy with pip tool:
C:\Python373\Scripts>pip install pycryptodome
Collecting pycryptodome
...
Installing collected packages: pycryptodome
Successfully installed pycryptodome-3.8.2
All packages from this python module are:
  • Cipher;
  • Signature;
  • Hash;
  • Publickey;
  • Protocol;
  • IO;
  • Random and Util;
Let start with a few examples:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Inte
l)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.Random import get_random_bytes
>>> key16 = get_random_bytes(16) # 16 bytes * 8 = 128 bits (1 byte = 8 bits)
The python module comes with features for encryption and dencryption, like RSA, AES, DES and many options.
Another good feature is KDF with PBKDF2 function:
>>> from Crypto.Protocol.KDF import PBKDF2
>>> center = b'go'
>>> password = 'go'
>>> output = PBKDF2(password, center, dkLen=64)
>>> print(output)
b'E\x0b\x91\x87\xde\xb2E\xe5Gv\x03\x86fVe8\x1e%\xb5l\xa0\xdb\xbfI\x01\xb5\xdf\x8
1\xad\x82@\x00\xacr\xc7\xa26\xc6\xe92\x1e\xf8\xe9\x0b\x9e\x93\x1dj\x1c\xff\x1c4\
xc2\x0e6\xc2\x8eYc2N\x995\x87'
>>>
If you are a fan of encryption and decryption math, then this module will enable you to use it.
By studying this module you can give a more in-depth view of what exists in this field and what does not exist.

Saturday, August 27, 2016

The python module pycryptodome - part 001.

The tutorial for today come with this subject: python module pycryptodome.
According to the official website:
PyCryptodome is a self-contained Python package of low-level cryptographic primitives.
It supports Python 2.4 or newer, all Python 3 versions and PyPy.
official website.
Also this python module can be used with Windows and Linux (Ubuntu and Fedora distro linux).
I don't see anything about Mac OS - Apple OS Mac_OS - wikipedia.
First step of this tutorial:

C:\Python27\Scripts>pip install pycryptodome
Collecting pycryptodome
Downloading pycryptodome-3.4-cp27-cp27m-win_amd64.whl (7.5MB)
100% |################################| 7.5MB 88kB/s
Installing collected packages: pycryptodome
Successfully installed pycryptodome-3.4
You need to have command.com shell admin rights or you got errors:
C:\Python27\Scripts>python -m Crypto.SelfTest
Access is denied.
You can test it the instalation with:
C:\Python27>python -m Crypto.SelfTest
Skipping AESNI tests
...........................................................................................................................................................................................................................................................................................................................................................................

.......................
----------------------------------------------------------------------
Ran 22263 tests in 171.266s

OK
One simple test with this module:
C:\Python27>python.exe
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.Cipher import AES
>>> from Crypto.Random import get_random_bytes
>>>
Most of resurces and features can be found here also can have some example at Matthew Green.
I will come with another tutorial about this python module.
Have a great day.