反转矩阵

时间:2015-12-11 22:25:37

标签: matrix

我正在尝试反转矩阵(实际上它是AES的s-box)。尺寸为10 * 16(10行16列) 这个框填满了数字,我想反转这个框 像这样 : a [0] [0] = 63(例如,在第一行/列中,值为63) 反转后: a [6] [3] = 00我想改变它 我尝试了一些算法,但没有工作

2 个答案:

答案 0 :(得分:0)

这样的事情应该可以解决问题,尽管这可能是一种更有效的方式。

Matrix Invert(Matrix in)
{
    Matrix out = new Matrix[10][16];
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < 16, j++)
        {
            int row = Math.Floor(in[i][j] / 16);
            int column = in[i][j] % 16;
            out[row][column] = i * 16 + j;
        }
    }
    return out;
}

答案 1 :(得分:0)

好吧,在您链接的页面中,矩阵是16x16,并且它存储在一个十六进制值的1D数组中。反演是这个算法中最容易的部分。我会给你一个c ++代码,例如:

#include <iostream>
#include <iomanip>

#define ROWS 16
#define COLS 16
#define N_ELEMENTS 256

void Invert(unsigned char * in, unsigned char * out) {
    for ( int i = 0; i < N_ELEMENTS; i++ ) out[in[i]] = i;
}

void showMatrix(unsigned char * m) {
    for ( int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            std::cout << "  " << std::setbase(16) << std::setfill('0') << std::setw(2)
                << (unsigned int)m[i*COLS + j];
        }
        std::cout << std::endl;
    }
}

int main() {
    unsigned char s[256] = {
        0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
        0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
        0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
        0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
        0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
        0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
        0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
        0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
        0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
        0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
        0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
        0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
        0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
        0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
        0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
        0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
    };
    unsigned char inv[256];

    Invert(s,inv);
    std::cout << "This is the original matrix:\n";
    showMatrix(s);
    std::cout << "\nThis is the inverted matrix:\n";
    showMatrix(inv);

    return 0;
}

输出是这样的:

This is the original matrix:
  63  7c  77  7b  f2  6b  6f  c5  30  01  67  2b  fe  d7  ab  76
  ca  82  c9  7d  fa  59  47  f0  ad  d4  a2  af  9c  a4  72  c0
  b7  fd  93  26  36  3f  f7  cc  34  a5  e5  f1  71  d8  31  15
  04  c7  23  c3  18  96  05  9a  07  12  80  e2  eb  27  b2  75
  09  83  2c  1a  1b  6e  5a  a0  52  3b  d6  b3  29  e3  2f  84
  53  d1  00  ed  20  fc  b1  5b  6a  cb  be  39  4a  4c  58  cf
  d0  ef  aa  fb  43  4d  33  85  45  f9  02  7f  50  3c  9f  a8
  51  a3  40  8f  92  9d  38  f5  bc  b6  da  21  10  ff  f3  d2
  cd  0c  13  ec  5f  97  44  17  c4  a7  7e  3d  64  5d  19  73
  60  81  4f  dc  22  2a  90  88  46  ee  b8  14  de  5e  0b  db
  e0  32  3a  0a  49  06  24  5c  c2  d3  ac  62  91  95  e4  79
  e7  c8  37  6d  8d  d5  4e  a9  6c  56  f4  ea  65  7a  ae  08
  ba  78  25  2e  1c  a6  b4  c6  e8  dd  74  1f  4b  bd  8b  8a
  70  3e  b5  66  48  03  f6  0e  61  35  57  b9  86  c1  1d  9e
  e1  f8  98  11  69  d9  8e  94  9b  1e  87  e9  ce  55  28  df
  8c  a1  89  0d  bf  e6  42  68  41  99  2d  0f  b0  54  bb  16

This is the inverted matrix:
  52  09  6a  d5  30  36  a5  38  bf  40  a3  9e  81  f3  d7  fb
  7c  e3  39  82  9b  2f  ff  87  34  8e  43  44  c4  de  e9  cb
  54  7b  94  32  a6  c2  23  3d  ee  4c  95  0b  42  fa  c3  4e
  08  2e  a1  66  28  d9  24  b2  76  5b  a2  49  6d  8b  d1  25
  72  f8  f6  64  86  68  98  16  d4  a4  5c  cc  5d  65  b6  92
  6c  70  48  50  fd  ed  b9  da  5e  15  46  57  a7  8d  9d  84
  90  d8  ab  00  8c  bc  d3  0a  f7  e4  58  05  b8  b3  45  06
  d0  2c  1e  8f  ca  3f  0f  02  c1  af  bd  03  01  13  8a  6b
  3a  91  11  41  4f  67  dc  ea  97  f2  cf  ce  f0  b4  e6  73
  96  ac  74  22  e7  ad  35  85  e2  f9  37  e8  1c  75  df  6e
  47  f1  1a  71  1d  29  c5  89  6f  b7  62  0e  aa  18  be  1b
  fc  56  3e  4b  c6  d2  79  20  9a  db  c0  fe  78  cd  5a  f4
  1f  dd  a8  33  88  07  c7  31  b1  12  10  59  27  80  ec  5f
  60  51  7f  a9  19  b5  4a  0d  2d  e5  7a  9f  93  c9  9c  ef
  a0  e0  3b  4d  ae  2a  f5  b0  c8  eb  bb  3c  83  53  99  61
  17  2b  04  7e  ba  77  d6  26  e1  69  14  63  55  21  0c  7d