2017-10-06 111 views
0

所以我的程序是一个速记程序,它将图像插入到另一个图像中,并且我试图在将数据插入到封面图像中之前加密数据。但是,大多数加密模块期望字符串,我试图传递整数。可以加密整数吗?

我试过转换成字符串然后加密,但加密是特殊字符和字母,所以转换回整数插入是不可能的。

任何人都知道我是否可以以某种方式加密一个整数?它不一定非常安全。

我尝试添加加密在这里:

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) 
+2

大多数加密系统可以使用任意二进制数据,字符串或两者兼有。 “整数”不是一个他们可以处理的概念,因为整数的格式在一个系统之间变化很大。您始终可以将整数转换为字符串,然后对其进行加密,然后将其加密。加密数据通常是原始二进制文件,并将其字符串化需要使用Base64或类似的编码。 – tadman

+0

整数,字符串等,只是二进制值的解释。如果你可以加密一种类型,你可以全部加密。 –

+0

@tadman你是什么意思的“烤它”? –

回答

3

你的电脑怎么看任何类型的数据的一个根本性的误解。

您读取了一个文件的字节流,它看起来像一个字符串,但每个字符实际上是一个字节,一个从0到255的值。它只是发生在其中一些字符由常规字符串表示。尝试使用print(bytes(range(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: 
    # ... 

非常重要:您在消息结尾添加空字节,以便您的提取序列知道何时停止。如果压缩或加密字符串(或字节数组),则可能是空字节将成为该流的一部分,这将破坏您的提取序列。在这种情况下,你想使用一个header提前告诉你的程序要提取多少位。


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

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

我想我已经知道了,我做了你所说的。 'fileData1 =开放(秘密, 'RB')。读() 的EncryptedData = cipher_suite.encrypt(fileData1) 的EncryptedData =仓(INT(binascii.hexlify(的EncryptedData),16))' 我转换二进制,然后试图将其添加到我的BITSTRING 'BITSTRING = BINNAME + nullDelimiter + binDataSize + nullDelimiter + encryptedData' 但是,试图将其保存在我的getPixel功能时,我得到一个: 无效字面INT()与基地2:'0000000b' –

+0

我相对还是一个新手充其量与Python,所以请裸露与我。 –

+0

@PaulCabz您的整数到位字符串转换可能有错误。 'bin()'给你一个以''0b'开头的字符串,并且不知何故'b'被标记出来用于乘车。不可能告诉你整个回溯和相关代码出错的地方,但如果你无法弄清楚,你应该提出一个新问题。这是一个关于你最初问的问题;加密二进制数据。 – Reti43