📜  Python|使用矩阵编码解码

📅  最后修改于: 2022-05-13 01:55:10.559000             🧑  作者: Mango

Python|使用矩阵编码解码

本文是关于我们如何使用矩阵对文本消息和简单字符串进行编码和解码。
编码过程:

  1. 将字符串转换为如下所示的相应数字
  2. 转换为二维矩阵(数组)。现在我们有 2×2 矩阵!
  3. 当我们将此矩阵与编码矩阵相乘时,我们得到编码的 2×2 矩阵。
  4. 现在转换为向量(一维数组)并显示给用户

解码过程

  • 将编码数字转换为二维矩阵(数组)
  • 逆编码矩阵!
  • 将编码矩阵与编码矩阵的逆相乘。
  • 转换为一维矩阵(数组)。然后转换为相应的字母。

    代码:编码.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

    时间复杂度: O(n)(其中 n 是消息的长度)
    空间复杂度: O(n)

    输出:

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

    代码:解码.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
    Decoded message = india