2017-10-06 2 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 "bake it in"이란 무엇을 의미합니까? –

답변

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: 
    # ... 

일에서 당신을 중지 아무것도 없다 매우 중요 : 메시지 끝 부분에 널 바이트를 추가하면 추출 시퀀스가 ​​언제 멈출지를 알 수 있습니다. 문자열 (또는 바이트 배열)을 압축하거나 암호화하면 null 바이트가 해당 스트림의 일부가되어 추출 시퀀스가 ​​손상됩니다. 이 경우 프로그램에 미리 추출 할 비트 수를 알려주는 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)()' 나는 변환 이진과 내 비트 열로를 추가하려고에 '비트 스트링 = 용지함 이름 + nullDelimiter + binDataSize + nullDelimiter + encryptedData' 하지만 내 getPixel와 기능에 저장하려고 할 때 내가 얻을 : 인터넷 용 잘못된 문자() 2 진수 : '0000000b' –

+0

나는 아직도 파이썬에 대한 초보자이기 때문에 나랑 벗겨주세요. –

+0

@PaulCabz 아마도 integer-to-bitstring 변환에 실수를 저질렀을 것입니다. 'bin()'은''0b ''로 시작하는 문자열을 주며, 어떤 식 으로든 'b'는 타기를 위해 태그가 붙여져 있습니다. 전체 추적 및 관련 코드로 어디서 잘못되었는지 말할 수는 없지만 알아낼 수없는 경우 새로운 질문을해야합니다. 이것은 당신이 처음에 물었던 것에 관한 것이다. 이진 데이터를 암호화합니다. – Reti43

관련 문제