我写了这段代码:
from Crypto.Cipher import AES
import numpy as np
import cv2, base64
BLOCK_SIZE = 16
PADDING = '{'
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
secret = "youwanttoknowmysecretdontyouhaha"
cipher = AES.new(secret)
video = cv2.VideoCapture(0)
a = 0
while True:
a = a + 1
check, frame = video.read()
encrypted = EncodeAES(cipher, str(frame))
img = DecodeAES(cipher, encrypted)
cv2.imshow("Capturing", np.ndarray(img))
key = cv2.waitKey(1)
if key == ord('q'):
break
但是它不能像预期的那样工作。
我希望它播放我的实时摄像机,但它给我以下错误:
回溯(最近一次通话最后一次):文件“ tester.py”,第28行,在 cv2.imshow(“ Capturing”,np.ndarray(img))ValueError:序列太大;不能大于32
我真的很想知道我做错了什么,不只给我解决方案,而是请我解释一下,我仍然在学习
编辑
我使用python 2.7
编辑
以下代码有效,但是我在主机将加密的文件发送到另一台计算机的程序中使用了它。在这种情况下,您不能使用frame.dtype
。
from Crypto.Cipher import AES
import numpy as np
import cv2, base64
BLOCK_SIZE = 16
PADDING = '{'
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
secret = "youwanttoknowmysecretdontyouhaha"
cipher = AES.new(secret)
video = cv2.VideoCapture(0)
a = 0
while True:
a = a + 1
check, frame = video.read()
encrypted = EncodeAES(cipher, frame.tobytes())
decrypted = DecodeAES(cipher, encrypted)
img = np.frombuffer(decrypted, dtype=frame.dtype).reshape(frame.shape)
cv2.imshow("Capturing", img)
key = cv2.waitKey(1)
if key == ord("q"):
break
video.release()
但是我可以将frame.dtype发送到另一台计算机。
当我将frame.dtype
和frame_shape
的加密形式发送到计算机并对其解密并在其中使用时,出现以下错误
img = np.frombuffer(decod_ed, dtype=img_dtype).reshape(img_shape)
TypeError: data type "ûÿùùÖÿùÖÿùÖÿÖÖÿÖÖÿÜÜÖøøÜ£ÜÖ£ÜÖÜÜÖÜÜÖÿÜÜÿÜÜÿÜøÖø£øøøøøøØ£ÿØ£ÿØ£ÿƒ×ÜØ×Ü׃ø؃ÖøØùØØùØØùƒ£ÿ×øùƒ£ÿƒ£ÿƒø܃øÜíØ£íØ£íØ£ó×Øó×Øó×Øóƒøóƒøúá£óƒøáƒøáƒø׃ø׃ø£ƒø£ƒø£ƒø£ƒøƒ×ÜáƒøúíøúíøÑáøÑáøñ×øу£ñØ×Ñ׃ñáƒñáƒóíØíá£áó£ƒíø×áÜ×áÜøƒÖ£áÜ£ƒø£ƒøƒØ£ƒØ£á×Øá×ØØØ£ØØ£××؃ƒ×ƒƒ×××Ø׃ø׃ø£ƒø£ƒø£×Ø£×Ø£×Ø£×ØØá£Øá£ØíøØíøáó£áó£×ó£Øíø܃øù£ÿôÜÿôÜÿÿÜÜÿÜÜøøøØØØíƒ×óáƒíƒ×íƒ×óáƒóáƒñúƒñúƒªÑíñúƒñúƒñúƒúó×úó×úó×úó×óó£óó£úñøúñøúúØññ×óñ×óñ×óúƒóúƒóóíóóíóúƒñÑíóªááñ××ñ××ñ××úƒáÑí׺á׺á£ñƒ£ñƒáúƒáúƒññúóóíúíáúíáííáññúññúññúñÑíñÑíóúƒñÑíñÑíñÑíóÑíóÑíóúƒóúƒñÑíñÑíñÑíóúƒáúƒóÑíóÑíáúƒƒó×áúƒóÑíóÑíóÑíáúƒáúƒƒó׃ó××íØØíøØíøØóÖØóÖØíø£áÜáá܃ƒÖíƒÖƒØùØ×òƒáùØáùÜØöÜØöÜØöÖ×òÿØöù£ôù£ôûÜöûÜööÜööÜöÆøöÆøööÜööÜöûÜööÿÆûùôûùôÿûòûöôÆöôöûòöûòöûòöûòÆöôÉòæÉòæÉûÉÉûÉÅòÅÅòÅæòÅÉöÄÉöÄÉöÄÉöÄÅôìÉÆîÅæïææïÉÉèÉÅåÉÅåîÅåèìäçîâåïéêîüêîüçï~çï~àïÇàïÇë|å|" not understood
答案 0 :(得分:1)
我假设 frame 是[SciPy]: numpy.ndarray。有两个问题。下面是一个示例:
code.py :
#!/usr/bin/env python3
import sys
from Crypto.Cipher import AES
import numpy as np
import cv2
import base64
BLOCK_SIZE = 16
PADDING = b"{"
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
CIPHER_MODE = AES.MODE_CBC
SECRET = b"youwanttoknowmysecretdontyouhaha"
def main():
img0 = cv2.imread("c:/valmand.png")
encrypt_cipher = AES.new(SECRET, CIPHER_MODE) # Python 2: AES.new(SECRET)
print("Original image data - Type: {:}, Size: {:d}, Attrs: {:}".format(type(img0), img0.size, img0.shape))
img_stream_wrong = str(img0).encode()
print("\nWrong img0 stream length: {:}".format(len(img_stream_wrong)))
print("\nWrong img0 stream: {:}".format(img_stream_wrong))
img_stream = img0.tobytes()
print("\nCorrect img0 stream length: {:}".format(len(img_stream)))
encrypted = EncodeAES(encrypt_cipher, img_stream)
print("\nEncrypted length: {:d}".format(len(encrypted)))
decrypt_cipher = AES.new(SECRET, CIPHER_MODE) # Python 2: AES.new(SECRET)
decrypted = DecodeAES(decrypt_cipher, encrypted)
print("\nDecrypted length: {:d}".format(len(decrypted)))
img1 = np.frombuffer(decrypted, dtype=img0.dtype).reshape(img0.shape)
print("\nFinal image data - Type: {:}, Size: {:d}, Attrs: {:}".format(type(img1), img1.size, img1.shape))
#cv2.imshow("Capturing", img1)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
注释:
AES.new
签名已更改([PyCryptodome]: AES)错误:
相反,当尝试反序列化时,在字节流上调用 ndarray 构造函数(它被悄悄转换为 tuple int s的em>-构造函数期望-与流的长度相同)
这里的问题是构造函数不接受大于 32 的序列:
>>> np.ndarray([0] * 32) array([], shape=(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), dtype=float64) >>> np.ndarray([0] * 33) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: sequence too large; cannot be greater than 32
要修复此问题,应使用[SciPy]: numpy.frombuffer。为了使新的 ndarray 等于原始的 ,还需要2件事(原始数据需要数据):
输出:
e:\Work\Dev\StackOverflow\q054326620>"e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" code.py Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32 Original image data - Type: <class 'numpy.ndarray'>, Size: 1493331, Attrs: (799, 623, 3) Wrong img0 stream length: 629 Wrong img0 stream: b'[[[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 255]\n [255 255 255]\n [255 255 255]]\n\n [[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 255]\n [255 255 255]\n [255 255 255]]\n\n [[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 255]\n [255 255 255]\n [255 255 255]]\n\n ...\n\n [[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 255]\n [255 255 255]\n [255 255 255]]\n\n [[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 255]\n [255 255 255]\n [255 255 255]]\n\n [[255 255 255]\n [255 255 255]\n [255 255 255]\n ...\n [255 255 255]\n [255 255 255]\n [255 255 255]]]' Correct img0 stream length: 1493331 Encrypted length: 1991128 Decrypted length: 1493331 Final image data - Type: <class 'numpy.ndarray'>, Size: 1493331, Attrs: (799, 623, 3)
@ EDIT0 :
显然,存在一些兼容性问题。我使用 Python 3 和 pycrpytodome 3.7.2 开发了答案。我升级到最新版本( 3.7.3 ),也将其安装在 Python 2 上,并且上面的代码也有效。这是我在控制台中得到的:
>>> from Crypto.Cipher import AES >>> AES <module 'Crypto.Cipher.AES' from 'e:\Work\Dev\VEnvs\py_064_02.07.15_test0\lib\site-packages\Crypto\Cipher\AES.pyc'> >>> AES.new("") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: new() takes at least 2 arguments (1 given) >>> import Crypto >>> Crypto.__version__ '3.7.3'
因此,您可能有一个非常旧的 pycryptodome 版本。无论如何,我会盲目发布您的代码所需的更改,以合并这些修复程序( while 循环仅):
# The rest of your code (exactly as in the question)
while True:
a = a + 1
check, frame = video.read()
original_meta = frame.dtype.name, frame.shape
encrypted_meta = EncodeAES(cipher, bytes(original_meta))
encrypted_data = EncodeAES(cipher, frame.tobytes())
# Here is the separation
decrypted_meta = DecodeAES(cipher, encrypted_meta)
decrypted_data = DecodeAES(cipher, encrypted_data)
meta = ast.literal_eval(decrypted_meta)
img = np.frombuffer(decrypted_data, dtype=np.dtype(meta[0])).reshape(meta[1])
cv2.imshow("Capturing", img)
key = cv2.waitKey(1)
if key == ord("q"):
break