I'm trying to decrypt a value which I'm reading from a CSV file. (I've encrypted the value of an integer and wrote it to a CSV file separately).
Encryption:
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import scrypt
from Crypto.Util.Padding import pad,unpad
from random import randrange
from Crypto.Hash import SHA256
import binascii
import pandas as pd
import numpy as np
import hashlib
BUFFER_SIZE = 1024 * 1024 # The size in bytes that we read, encrypt and write to at once
password = 'QWERTY123'
salt = 'QWERTY123'
print("Salt is created")
key = scrypt(password, salt, key_len=32, N=2**17, r=8, p=1) # Generate a key using the password and salt
print("Key is created",key)
cipher = AES.new(key, AES.MODE_GCM) # Create a cipher object to encrypt data
nonce = cipher.nonce
print("Nonce is created")
print ("Key is :", type(key))
print ("Nonce is :", type(nonce))
val=100
plain_text =val.to_bytes(2, 'big')
print("Plaintext is ",plain_text)
cipher_decrypt= AES.new(key, AES.MODE_GCM, nonce=nonce)
cipher_text = cipher.encrypt(pad(plain_text,AES.block_size))
print("Encrypted text:", cipher_text)
The generated output is as follows:
Salt is created
Key is created b'\x1eev\xb1\x95,\xa7\xb2&Dk\x12\x88n\xcf\xe1\xe3\xda\xf13p\x8f;\x02>\x99\x82L+\x9a\x8a$'
Nonce is created
Key is : <class 'bytes'>
Nonce is : <class 'bytes'>
Plaintext is b'\x00d'
Encrypted text: b'\x02(\x0bM\x00I\x07M\xc7J;\xdc\xe7h\xd5\x00'
I copy the value of encrypted text and paste it into a CSV file. The password and the salt are the same (I'm aware that this is not recommended) because the same key has to be generated for the decryption which is happening separately.
Decryption:
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import scrypt
from Crypto.Util.Padding import pad,unpad
from random import randrange
from Crypto.Hash import SHA256
import binascii
import pandas as pd
import numpy as np
import hashlib
import ast
BUFFER_SIZE = 1024 * 1024 # The size in bytes that we read, encrypt and write to at once
password = 'QWERTY123'
salt = 'QWERTY123'
print("Salt is created")
key = scrypt(password, salt, key_len=32, N=2**17, r=8, p=1) # Generate a key using the password and salt
print("Key is created",key)
cipher = AES.new(key,AES.MODE_GCM) # Create a cipher object to encrypt data
nonce = cipher.nonce
print("Nonce is created")
print ("Key is :", type(key))
print ("Nonce is :", type(nonce))
cipher_decrypt= AES.new(key, AES.MODE_GCM, nonce=nonce)
input_file = pd.read_csv('/path/random.csv')
encrypt_val=input_file.iat[0,1]
print("Remaining are",encrypt_val)
print(type(encrypt_val))
a=ast.literal_eval(encrypt_val)
print(a)
print(type(a))
text = unpad(cipher_decrypt.decrypt(a),AES.block_size)
print(text)
print(type(text))
b=ast.literal_eval(text)
This gives the following output:
Salt is created
Key is created b'\x1eev\xb1\x95,\xa7\xb2&Dk\x12\x88n\xcf\xe1\xe3\xda\xf13p\x8f;\x02>\x99\x82L+\x9a\x8a$'
Nonce is created
Key is : <class 'bytes'>
Nonce is : <class 'bytes'>
Remaining are b"H\x13\x0c\xa0J\x07\x98<QN])z'S\xdc"
<class 'str'>
b"H\x13\x0c\xa0J\x07\x98<QN])z'S\xdc"
<class 'bytes'>
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Untitled-2.ipynb Cell 1 in <module>
36 print(a)
37 print(type(a))
---> 38 text = unpad(cipher_decrypt.decrypt(a),AES.block_size)
39 print(text)
40 print(type(text))
File /opt/homebrew/lib/python3.9/site-packages/Crypto/Util/Padding.py:92, in unpad(padded_data, block_size, style)
90 padding_len = bord(padded_data[-1])
91 if padding_len<1 or padding_len>min(block_size, pdata_len):
---> 92 raise ValueError("Padding is incorrect.")
93 if style == 'pkcs7':
94 if padded_data[-padding_len:]!=bchr(padding_len)*padding_len:
ValueError: Padding is incorrect.