Python 可以加密整数吗?

Python 可以加密整数吗?,python,encryption,steganography,Python,Encryption,Steganography,所以我的程序是一个速记程序,它将一个图像插入另一个图像,我试图在将数据插入封面图像之前对数据进行加密。然而,大多数加密模块都需要字符串,我试图传递整数 def bytes_to_bits(stream): for byte in stream: for shift in range(7, -1, -1): yield (byte >> shift) & 0x01 secret_bits = tuple(bytes_to_bit

所以我的程序是一个速记程序,它将一个图像插入另一个图像,我试图在将数据插入封面图像之前对数据进行加密。然而,大多数加密模块都需要字符串,我试图传递整数

def bytes_to_bits(stream):
    for byte in stream:
        for shift in range(7, -1, -1):
            yield (byte >> shift) & 0x01

secret_bits = tuple(bytes_to_bits(encoded_data))

# simplified for one colour plane
for x in range(image_height):
    for y in range(image_width):
        # (pixel AND 254) OR bit - the first part zeroes out the lsb
        pixels[x,y] = (pixels[x,y] & 0xfe) | secret_bits[count]
        count += 1

# -------------------------------------

# to extract the bit from a stego pixel
bit = pixel & 0x01
我尝试过先转换为字符串,然后再进行加密,但加密中充满了特殊字符和字母,因此无法转换回整数进行插入

有人知道我能不能加密一个整数吗?它不必非常安全

我正在尝试在此处添加加密:

for i in range(0,3):
    #verify we have reached the end of our hidden file
    if count >= len(Stringbits):
        #convert the bits to their rgb value and appened them
        for rgbValue in pixelList:
            pixelnumbers1 = int(''.join(str(b) for b in rgbValue), 2)
            #print pixelnumbers1
            rgb_Array.append(pixelnumbers1)
        pixels[x, y] = (rgb_Array[0], rgb_Array[1], rgb_Array[2])
        print "Completed"
        return imageObject.save(output)
我一直在尝试加密
pixelnumbers1
,然后添加它。但是
像素[x,y]
需要一个整数

以下是案例中的其余代码:

def write(mainimage, secret, output):
    #string contains the header, data and length in binary
    Stringbits = dcimage.createString(secret)
    imageObject = Image.open(mainimage).convert('RGB')
    imageWidth, imageHeight = imageObject.size
    pixels = imageObject.load()
    rgbDecimal_Array = []
    rgb_Array = []
    count = 0

    #loop through each pixel
    for x in range (imageWidth):
        for y in range (imageHeight):
            r,g,b = pixels[x,y]
            #convert each pixel into an 8 bit representation
            redPixel = list(bin(r)[2:].zfill(8))
            greenPixel = list(bin(g)[2:].zfill(8))
            bluePixel = list(bin(b)[2:].zfill(8))
            pixelList = [redPixel, greenPixel, bluePixel]

            #for each of rgb
            for i in range(0,3):
                #verify we have reached the end of our hidden file
                if count >= len(Stringbits):
                    #convert the bits to their rgb value and appened them
                    for rgbValue in pixelList:
                        pixelnumbers1 = int(''.join(str(b) for b in rgbValue), 2)
                        #print pixelnumbers1
                        rgb_Array.append(pixelnumbers1)
                    pixels[x, y] = (rgb_Array[0], rgb_Array[1], rgb_Array[2])
                    print "Completed"
                    return imageObject.save(output)

                #If we haven't rached the end of the file, store a bit
                else:
                    pixelList[i][7] = Stringbits[count]
                    count+=1
            pixels[x, y] = dcimage.getPixel(pixelList)

你对计算机如何看待任何类型的数据有一个根本性的误解

您读取文件的ByTestStream,它看起来像一个字符串,但每个字符实际上是一个字节,一个从0到255的值。碰巧它们中的一些是由传统的字符串表示的。请尝试打印(字节(范围(256))以查看所有字节。大多数标准加密函数都会输入字节数组,然后输出字节数组。碰巧您会得到更多没有“简单”表示的字节。但它们的字节数并不比您最初输入的字节数少

您的dcimage.py具有以下功能:

#get the file data in binary
fileData = bytearray(open(secret, 'rb').read())#opens the binary file in read or write mode
for bits in fileData:
    binDataString += bin(bits)[2:].zfill(8)#convert the file data to binary
没有什么能阻止你这么做

fileData = open(secret, 'rb').read() # a bytes object by default
encryptedData = myEncryptionFuction(fileData) # also a bytes object
for bits in encryptedData:
    # ...
非常重要:在消息末尾添加一个空字节,以便提取序列知道何时停止。如果压缩或加密字符串(或字节数组),很可能一个空字节将成为该流的一部分,这将中断您的提取序列。在这种情况下,您希望使用一个空字节来提前告诉您的程序要提取多少位


顺便说一下,字节已经是整数形式了

>>> some_byte = b'G'
>>> some_byte[0]
71
你最好使用隐写术。你获取字节,而不是在它们和像素之间使用位运算,你把它们都变成二进制字符串,切片和缝合它们,然后把它们变成整数

def bytes_to_bits(stream):
    for byte in stream:
        for shift in range(7, -1, -1):
            yield (byte >> shift) & 0x01

secret_bits = tuple(bytes_to_bits(encoded_data))

# simplified for one colour plane
for x in range(image_height):
    for y in range(image_width):
        # (pixel AND 254) OR bit - the first part zeroes out the lsb
        pixels[x,y] = (pixels[x,y] & 0xfe) | secret_bits[count]
        count += 1

# -------------------------------------

# to extract the bit from a stego pixel
bit = pixel & 0x01

你对计算机如何看待任何类型的数据有一个根本性的误解

您读取文件的ByTestStream,它看起来像一个字符串,但每个字符实际上是一个字节,一个从0到255的值。碰巧其中一些字符由常规字符串表示。请尝试打印(字节(范围(256))查看它们。大多数标准的加密函数都会输入一个字节数组,然后输出一个字节数组。碰巧你得到了更多没有“简单”表示的字节。但它们的字节数不会比你最初输入的字节数少

您的dcimage.py具有以下功能:

#get the file data in binary
fileData = bytearray(open(secret, 'rb').read())#opens the binary file in read or write mode
for bits in fileData:
    binDataString += bin(bits)[2:].zfill(8)#convert the file data to binary
没有什么能阻止你这么做

fileData = open(secret, 'rb').read() # a bytes object by default
encryptedData = myEncryptionFuction(fileData) # also a bytes object
for bits in encryptedData:
    # ...
非常重要:在消息末尾添加一个空字节,以便提取序列知道何时停止。如果压缩或加密字符串(或字节数组),很可能一个空字节将成为该流的一部分,这将中断您的提取序列。在这种情况下,您希望使用一个空字节来提前告诉您的程序要提取多少位


顺便说一下,字节已经是整数形式了

>>> some_byte = b'G'
>>> some_byte[0]
71
你最好使用隐写术。你获取字节,而不是在它们和像素之间使用位运算,你把它们都变成二进制字符串,切片和缝合它们,然后把它们变成整数

def bytes_to_bits(stream):
    for byte in stream:
        for shift in range(7, -1, -1):
            yield (byte >> shift) & 0x01

secret_bits = tuple(bytes_to_bits(encoded_data))

# simplified for one colour plane
for x in range(image_height):
    for y in range(image_width):
        # (pixel AND 254) OR bit - the first part zeroes out the lsb
        pixels[x,y] = (pixels[x,y] & 0xfe) | secret_bits[count]
        count += 1

# -------------------------------------

# to extract the bit from a stego pixel
bit = pixel & 0x01

大多数加密系统要么处理任意二进制数据,要么处理字符串,要么同时处理两者。“整数”这不是一个他们可以处理的概念,因为不同系统的整数格式差异很大。您可以始终将整数转换为字符串,对其进行加密,然后将其烘焙。加密数据通常是原始二进制数据,对其进行字符串化需要使用Base64或类似的编码。整数、字符串等只是二进制值的解释ues。如果你能加密一种类型,你就可以全部加密。@tadman你说的“嵌入”是什么意思?假设你说的是“嵌入”而不是“嵌入”,则“嵌入”是将加密数据嵌入另一种媒体的过程。大多数加密系统都可以处理任意二进制数据、字符串或两者兼而有之。“整数”这不是一个他们可以处理的概念,因为不同系统的整数格式差异很大。您可以始终将整数转换为字符串,对其进行加密,然后将其烘焙。加密数据通常是原始二进制数据,对其进行字符串化需要使用Base64或类似的编码。整数、字符串等只是二进制值的解释ues。如果你能加密一种类型,你就可以全部加密。@tadman你说的“烘焙”是什么意思?假设你说的不是,那么“烘焙”就是将加密数据嵌入另一种媒体的过程。我想我差不多明白了,我照你说的做了。
fileData1=open(secret,'rb')。read()encryptedData=cipher\u suite.encrypt(fileData1)encryptedData=bin(int(binascii.hexlify(encryptedData),16))
我将其转换为二进制,然后尝试将其添加到我的位字符串
bitString=binName+nullDelimiter+binDataSize+nullDelimiter+encryptedData
中,但在试图将其保存到getPixel函数中时,我得到一个:int()的无效文本对于base 2:'0000000b'我对Python充其量还是个新手,所以请告诉我。@PaulCabz您在整数到位字符串的转换中可能有错误。
bin()
给你一个以
'0b'
开头的字符串,不知怎的,“b”在旅途中被标记了。不可能知道你在整个回溯和相关代码中哪里出了问题,但是如果你不能弄清楚,你应该问一个新问题。这是关于你最初问的问题;加密二进制数据。@PaulCabz I用一些信息更新了答案,如果你决定加密你的邮件,这些信息可能会变得相关。我想我已经知道了,我按照你说的做了。
f