Python | Encoding Decoding using a matrix



This article is about how we use a matrix to encode and decode text messages and simple strings.
Coding process:

  1. Take the string conversion to the appropriate number shown below

  2. convert to 2D matrix (array). We now have a 2 × 2 matrix!
  3. When we multiply this matrix by the encoding matrix we get a 2 × 2 encoded matrix.
  4. now convert to vector (1D array) and display for user

Decoding process

  • Take encoded number converted to 2D matrix (array)
  • Inverse encoding matrix !
  • Re-encoded matrix with inverse encoding matrix.
  • Convert to 1D matrix (array). then convert to the corresponding alphabets.

  • Code: Encode.py

    # loading libraries

    import numpy as np

     

    =   [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]

    =   [[ 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 their meanings

    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
    # also useful for coding

    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 2 D array for further
    # multiply by encoding matrix

     

    j = 0

    k = 0

     
    # b [m] [n] m always 2

    n = int (lenb / 2 )

    for i in range ( 0 , lenb):

      if (j & lt; n):

    c [k] [j] = a [i]

    j = j + 1

      else :

    k = k + 1

      j = 0

    c [k] [j] = a [i]

    j = j + 1

     

     
    # Multi-user matrix with 2x2 matrix encoding

    c = np.matmul (ecm, c)

     

     
    # Convert to 1D array for display

    i = 0

    j = 0

    k = 0

    for i in r ange ( 2 ):

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

    Time complexity: O (n) (where n — message length)
    Space complexity: O (n)

    Output :

     

    Code: 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 their 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 ]

     

     
    # Accept input
    # elements in the encrypted matrix

    lenb = 6

    a = [ 63 , 46 , 12 , 81 , 48 , 12 ]

     

    sobj = slice (lenb)

    a = a [sobj]