Python Forum
Automatic decipher a binary string ciphered by the CIPHER program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automatic decipher a binary string ciphered by the CIPHER program
#1
The DECIPHERER program allows you, in some cases, to decipher a binary string ciphered by the CIPHER program without knowing the initial condition “Xo” that was used. This program performs an exhaustive search with the purpose to recover the plain text, having as information the ciphered binary string and under the assumption that we know a keyword, which should have at least 8 characters, of the original text. This keyword will be searched only in the first ten positions.

In this program, we require: a binary string ciphered by the CIPHER program and have a keyword of at least 8 characters. As a result, in some cases, we will have the original plain text.

NOTE: We use the ISO norm 8859-1 because It extends the ASCII code with characters used in the Spanish language, e.g., á, Á, ó, Ó, ñ, Ñ.

#Authors:
print('\nDavid H.G. y Hugo C.I.')
print('IPICYT')
#Date:
print('March 2019')
print('Python 3.6\n')

print('=================================================================')
print('This application breaks the logistic ciphering.')
print('=================================================================')
      
#NOTE: We use the ISO norm 8859-1 (latin1) because this program was implemented for the Spanish language.

micodigo='latin1'

MIXX1 = input("Insert the ciphered binary string: \n\n")
LL=len(MIXX1)             
argumento = input("Insert the keyword (It need has at least 8 characters): \n\n")
m=len(argumento)
print('\nWe searched the possible initial condition and the possible plain text:\n')

# We generate the binary string the keyword.
octetos = bytearray(argumento, micodigo)
binario1= ''.join(f'{x:b}'.rjust(8, '0') for x in octetos)
n=len(binario1)
clave=binario1[0:64]

# We start the automatic searching from the 1 position to 10 positions.
posicion=0
continuar = 0
while True:
    LL=len(MIXX1)
    if continuar == 1:
        break
    elif posicion == 10:
        break
    else:
        RI=1         # Initial range.
        RF=5000      # Final range.
        Pa=10000     # Partitioning.
        Ex=6         # Precision.
        
    # We iterate 14 times searching.
        for p in range(1,14):
        
    ## We obtain the new values of "RI" (I).
            for j in range(RI,RF+1):
                x=(RF+RI-j)/Pa
                z=''
                
         # We generate the binary string (z) using the logistic map "4x(1-x)" and the initial condition "x".
                for i in range(70):
                    x=4*x*(1-x)
                    if x >= 0.5:
                        z= z[:] + "1"
                    else:
                        z= z[:] + "0"
                a = z[0:Ex]
                
         # We sum the string (a)-(b) and searching the keyword it.
                b = MIXX1[0:Ex]
                SUMCAD=''
                for i in range(Ex):
                    x=int(a[i])
                    y=int(b[i])
                    c=x^y
                    d=str(c)
                    SUMCAD=SUMCAD+d
                a = SUMCAD[0:Ex]
                b = clave[0:Ex]
                MM=''
                for i in range(Ex):
                    x=int(a[i])
                    y=int(b[i])
                    c=x^y
                    d=str(c)
                    MM=MM+d
                    DC=0
                for i in range(0,Ex):
                    DC=DC+int(MM[i])
                if DC==0:
                    I=RI+RF-j
                 
                ## DC=0, imply that for this initial condition the decipherer string and the ciphered string coincide with the keyword with length "Ex".
                
         ## We obtain the new values of "RF" (F). 
            
            for j in range(RI,RF+1):
                x=j/Pa
                z=''

                # We generate the binary string (z) using the logistic map "4x(1-x)" and the initial condition "x".
                for i in range(70):
                    x=4*x*(1-x)
                    if x >= 0.5:
                        z= z[:] + "1"
                    else:
                        z= z[:] + "0"
                a = z[0:Ex]
                b = MIXX1[0:Ex]
                SUMCAD=''
                for i in range(Ex):
                    x=int(a[i])
                    y=int(b[i])
                    c=x^y
                    d=str(c)
                    SUMCAD=SUMCAD+d
                a = SUMCAD[0:Ex]
                b = clave[0:Ex]
                MM=''
                for i in range(Ex):
                    x=int(a[i])
                    y=int(b[i])
                    c=x^y
                    d=str(c)
                    MM=MM+d
                DC=0
                for i in range(0,Ex):
                    DC=DC+int(MM[i])
                if DC==0:
                    F=j
                  
                ## DC=0, imply that for this initial condition the decipherer string and the ciphered string coincide with the keyword with length "Ex".
    
            RI=I*10
            RF=F*10
            print('RANGO FINAL ---',p,Ex,I,F)    
            Pa=Pa*10
            Ex=Ex+3
        
      ## We refine search use the obtained values. 
      
        # We select the initial range (I) as "RI" new, the final range (F) as "RF" new and we increase the precision.
        RI=I
        RF=F
        Pa=Pa/10
        Ex=60

        for j in range(RI,RF+1):
            x=j/Pa
            z=''
            
            # We generate the binary string (z) using the logistic map "4x(1-x)" and the initial condition "x".
            for i in range(70):
                x=4*x*(1-x)
                if x >= 0.5:
                    z= z[:] + "1"
                else:
                    z= z[:] + "0"
            a = z[0:70]
            b = MIXX1[0:70]
            SUMCAD=''
            for i in range(Ex):
                x=int(a[i])
                y=int(b[i])
                c=x^y
                d=str(c)
                SUMCAD=SUMCAD+d
            a = SUMCAD[0:Ex]
            b = clave[0:Ex]
            MM=''
            for i in range(Ex):
                x=int(a[i])
                y=int(b[i])
                c=x^y
                d=str(c)
                MM=MM+d
            DC=0
            for i in range(0,Ex):
                DC=DC+int(MM[i])
                
            ## DC=0, imply that for this initial condition the decipherer string and the ciphered string  coincide with the keyword with length "Ex".

            if DC==0:
                continuar=1
                F=j
                print('\nThe possible initial condition: ',F/10000000000000000)
                x=j/10000000000000000
                z=''
                
                # We generate the binary string (z) using the logistic map "4x(1-x)" and the possible initial condition "x".
                for i in range(LL):
                    x=4*x*(1-x)
                    if x >= 0.5:
                        z= z[:] + "1"
                    else:
                        z= z[:] + "0"
                a = z
                b = MIXX1
                SUMCAD=''
                for i in range(LL):
                    x=int(a[i])
                    y=int(b[i])
                    c=x^y
                    d=str(c)
                    SUMCAD=SUMCAD+d
                str1 = SUMCAD
                
                # We traduce the binary string "str1" to plain text (message).
                message = ""
                while str1 != "":
                    i = chr(int(str1[:8], 2))
                    message = message + i
                    str1 = str1[8:]
                print('\n____________________________________________')  
                print("Message found:")
                print('____________________________________________ \n')
                print(message)
                print('\n____________________________________________ \n')    
               
    # We print the position where we found the keyword.
    posicion = posicion + 1        
    print('The search finished in the position:', posicion)
    MIXX1=MIXX1[8:LL]

final = input('You press "Intro" to quit.')
Here,you will find the DECIPHER program which allows you to decipher a plain text ciphered with the CIPHER program, knowing the initial condition “Xo” used.

Here, you will find the CIPHER program which allows you to cipher a plain text by using the logistic map and the initial condition “Xo”.

Aquí, encontrarás los programas antes mencionados, pero, en su versión en Español.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Decipher a plain text ciphered by the CIPHER program DavidHG 0 1,424 Apr-27-2019, 10:29 PM
Last Post: DavidHG

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020