2017-10-06 80 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