I want to encrypt a sound signal(.wav
) using AES. For this I'm reading the signal into a numpy array as following:
a = read("C:\\Users\\Kaushal28\\Desktop\\test.wav")
data = np.array(a[1],dtype=int)
Now, this data
array, when printed looks like as following:
[-2,5]
[0,-3]
[1,1] etc.
I'm using following class for AES encryption:
import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
class AESCipher(object):
def __init__(self, key):
self.bs = 32
self.key = hashlib.sha256(key.encode()).digest()
def encrypt(self, raw):
raw = self._pad(raw)
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(raw))
def decrypt(self, enc):
enc = base64.b64decode(enc)
iv = enc[:AES.block_size]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')
def _pad(self, s):
return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
@staticmethod
def _unpad(s):
return s[:-ord(s[len(s)-1:])]
Which can be found here:This SO link..
Now using this class, I want to encrypt my data
array.
I've tried following:
lol = []
for i in range (0, data.size):
lol.append(AESCipher("a11a454508421079").encrypt(""+data[i]+""))
But this is giving me some strange errors:
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U11') dtype('<U11') dtype('<U11')
What I'm doing wrong here? How can I encrypt whole data
array?
In this case, your variable data contains two columns for each individual item in that numpy array.
So what you can do is separate the numpy array into two individual column wise split arrays.
You can use data=np.hsplit(data,2). Refer - https://docs.scipy.org/doc/numpy/reference/generated/numpy.hsplit.html
Now you can go ahead decrypting the data contents.