zoukankan      html  css  js  c++  java
  • GF(2^8)生成元

    目的是找出所有GF(2^8)的生成元。

    方法很简单,从2开始遍历,将每个元素都与自身相乘255次,看是否能得到1~255。若能,则是生成元。

    #include<iostream>
    #include<fstream>
    using namespace std;
    unsigned char GFmul(unsigned char a, unsigned char b){
        //GF(2^8) 乘法
        unsigned char result = 0;
        if((b&1) == 1)result = a;
        b >>= 1;
        for(int i = 1; i < 8; i ++){
            if(a > 127){
                a = (a << 1) ^ 0x1b;
            }
            else{
                a <<= 1;
            }
            if((b&1) == 1){
                result ^= a;
            }
            b >>= 1;
        }
        return result;
    }
    int findGenerator(int result[]){
        //找出所有GF(2^8)的生成元
        unsigned char x = 2;        //从2开始查找
        int len = 0;
        do{
            int count[256], i;
            for(i = 0; i < 256; i ++)count[i] = 0;
            count[x] ++;
            unsigned char tmp = x;
            for(i = 2; i < 256; i ++){
                tmp = GFmul(tmp, x);
                count[tmp] ++;
            }
            for(i = 1; i < 256; i ++){
                //查看是否所有的数都有生成,若是,则x为生成元
                if(count[i] != 1)break;
            }
            if(i == 256)result[len ++] = x;
            x ++;
        }while(x != 0);
        return len;
    }
    int main(){
        //单元测试。输出所有的生成元。
        int result[256];
        int len = findGenerator(result);
        ofstream write("Test.txt");
        for(int i = 0; i < len; i ++)write<<result[i]<<endl;
        write.close();
        return 0;
    }
    C++
  • 相关阅读:
    Qtcreator中printf()/fprintf()不显示问题处理方法
    C++实现斐波那契数列
    DAPP超详细解释
    自底向上的合并排序算法
    Python 生成哈希hash--hashlib模块
    使用js的一些小技巧
    js——事件
    django学习
    js——js特效
    js--DOM学习
  • 原文地址:https://www.cnblogs.com/7hat/p/3383524.html
Copyright © 2011-2022 走看看