Python | Codificación Decodificación usando Matrix

Este artículo trata sobre cómo usamos la array para codificar y decodificar un mensaje de texto y strings simples.
Proceso de codificación:

  1. Tome una conversión de string al número correspondiente que se muestra a continuación
  2. convertir a array 2D (array). ¡Ahora tenemos una array de 2×2!
  3. Cuando multiplicamos esta array con la array de codificación, obtenemos una array codificada de 2 × 2.
  4. ahora convertir a vector (array 1D) y mostrar al usuario

Proceso de decodificación

  • Tome el número codificado convertido en array 2D (array)
  • ¡Array de codificación inversa!
  • Multiplique la array codificada por la inversa de la array de codificación.
  • convertir a array 1D (array). Luego convertir a los alfabetos correspondientes.
  • Código: Encode.py

    # loading libraries
    import numpy as np
      
    a  =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    c  =  [[0,0,0,0,0,0,0,0,0,0],
           [0,0,0,0,0,0,0,0,0,0]]
      
    # encode matrix
    ecm = [[3,4], [3,6]]
    i = 0
    l = 0
       
    # Lists of Alphabets and its values
    smallalpha = [" ","a", "b", "c", "d", "e", "f", "g", "h",
                  "i", "j", "k", "l", "m", "n", "o", "p", "q",
                  "r", "s", "t", "u", "v", "w", "x", "y", "z"]
    capitalalpha = [" ","A", "B", "C", "D", "E", "F", "G", "H",
                    "I", "J", "K", "L", "M", "N", "O", "P", "Q",
                    "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
    alphavalues = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                   13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
                   23, 24, 25, 26, 27]
      
    # string to convert 
    b = "India"
    listb = list(b)
    lenb = len(listb)
       
    # Loop to convert Word to Values that 
    # are further useful for Encoding
    for i in range(lenb):
        for j in range(27):
            if(listb[i]  == smallalpha[j]):
                a[i] = alphavalues[j]
                if(j  == 23):
                    j = 0
                break
            if(j  == 23):
                for k in range(27):
                    if(listb[i]  == capitalalpha[k]):
                        a[i] = alphavalues[k]
                        break
                         
                       
    if(lenb%2 == 1):
        lenb = lenb+1
    a = a[0:lenb]
    tb = b
       
       
    # convert this array to 2D array for further 
    # multiplication with encoding matrix
      
    j = 0
    k = 0
      
    # b[m][n] m is always 2
    n = int(lenb/2)
    for i in range(0,lenb):
        if(j<n):
            c[k][j] = a[i]
            j = j+1
        else:
            k = k+1
            j = 0
            c[k][j] = a[i]
            j = j+1
               
               
    # Multiplay matrix by Encoding 2x2 matrix 
    c = np.matmul(ecm, c)
       
       
    # Convert to 1D array for displaying 
    i = 0
    j = 0
    k = 0
    for i in range(2):
        for j  in range(int(lenb/2)):
            a[k] = c[i][j]
            k = k+1
       
              
    a = a[0:lenb]
    print("Encoding matrix  =  ", ecm)
    print("encrypted form =  ", a)
    

    Complejidad de tiempo: O(n)(donde n es la longitud del mensaje)
    Complejidad de espacio: O(n)
     

    Producción:

    Encoding matrix =  [[3, 4], [3, 6]]
    Encrypted form =  [63, 46, 12, 81, 48, 12]
    

    Código: Decode.py

    # importing libraries
    import numpy as np
    from numpy.linalg import inv
      
      
    # Initial values
    a =[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0]
      
    tdm =[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
      
    # encoding matrix
    ecm =[[3, 4],
         [3, 6]]
      
    # Lists of Alphabets and its values
    smallalpha = [" ","a", "b", "c", "d", "e", "f", "g", "h",
                  "i", "j", "k", "l", "m", "n", "o", "p", "q",
                  "r", "s", "t", "u", "v", "w", "x", "y", "z"]
    capitalalpha = [" ","A", "B", "C", "D", "E", "F", "G", "H",
                    "I", "J", "K", "L", "M", "N", "O", "P", "Q",
                    "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
    alphavalues = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                   13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
                   23, 24, 25, 26, 27]
      
      
    # Take inputs
    # elements in Encrypted Matrix
    lenb = 6
    a = [63, 46, 12, 81, 48, 12]
      
    sobj = slice(lenb)
    a = a[sobj]
      
      
    # convert array to 2d matrix to further 
    # multiplication with inverse of 2d matrix
    j = 0
    k = 0
      
    # b[m][n] m is always 2
    n = int(lenb / 2)
    for i in range(0, lenb):
        if(j<n):
            tdm[k][j]= a[i]
            j = j + 1
        else:
            k = k + 1
            j = 0
            tdm[k][j]= a[i]
            j = j + 1
      
              
    # Multiply by inverse matrix
    dcm = inv(ecm)
    dcm = np.matmul(dcm, tdm)
      
      
    # Convert to 1d array for extracting word
    i = 0
    j = 0
    k = 0
    for i in range(2):
        for j in range(int(lenb / 2)):
            a[k]= dcm[i][j]
            k = k + 1
               
      
    # Creating a decoded word
    text = ""
    for i in range(0, lenb):
        for j in range(0, 27):
            if(a[i]== alphavalues[j]):
                text =''.join([text, smallalpha[j]])
      
    print("Decoded message = "+text)
    


    Complejidad de tiempo:
    O(n)(donde n es el número de elementos)
    Complejidad de espacio: O(n)

    Producción:

    Decoded message = india 

    Publicación traducida automáticamente

    Artículo escrito por mwadile03 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

    Deja una respuesta

    Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *