是否可以加密整数?

Is it possible to encrypt integers?

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

我试过转换为字符串然后加密,但加密充满了特殊字符和字母,因此无法转换回整数以进行插入。

有人知道我能否以某种方式加密整数吗?它不必非常安全。

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

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 然后将其添加进去。但是 pixels[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)

您对计算机如何查看任何类型的数据存在根本性误解。

你读了一个文件的字节流,它看起来像一个字符串,但实际上每个字符都是一个字节,一个从0到255的值。恰好其中一些是用常规的字符串字符表示的。尝试 print(bytes(range(256)) 查看全部。大多数标准加密函数接收一个字节数组并吐出一个字节数组。碰巧你得到了更多没有 "simple" 表示的字节。但是它们并不比你最初输入的字节少。

您的 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

您最好使用 bitwise operations 进行隐写术。您获取字节,而不是在它们和像素之间使用按位运算,而是将两者都转换为二进制字符串,对它们进行切片和拼接,然后将它们转回整数。

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

可以通过将每个数字添加到 0 到 9 范围内的随机整数流来加密整数,当总和 > 9 时减去 10。由于含糊不清,应避免取模。