Finite Field multiplication of two bytes over the field GF(2^8) Process - Java java.lang

Java examples for java.lang:byte

Description

Finite Field multiplication of two bytes over the field GF(2^8) Process

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte a = 2;
        byte b = 2;
        System.out.println(FFMul(a, b));
    }/* w w w.  j  a  v  a  2  s .co m*/

    /**
     * Finite Field multiplication of two bytes over the field GF(2^8)
     * 
     * Process defined by Neal Wagner's \The Laws of Cryptography\.
     * 
     * @param n
     * @param o
     * @return
     */
    public static byte FFMul(byte a, byte b) {
        byte r = 0;

        while (a != 0) {
            if ((a & 1) != 0)
                r = (byte) (r ^ b);

            // Repeatedly multiply by (1)
            b = (byte) (b << 1);

            // If the result is of degree 8
            // add m(x)
            if ((byte) (b & 0x80) != 0)
                b = (byte) (b ^ 0x1b);

            a = (byte) ((a & 0xff) >> 1);
        }

        return r;
    }
}

Related Tutorials