D
D
Domus2018-03-29 20:18:47
Java
Domus, 2018-03-29 20:18:47

Is it possible to switch from byte[] to int so famously?

Good afternoon!
It is necessary to perform operations on bits. I have such a chain String -> byte[8] -> int(32) 2 pcs. And in reverse order.
I wrote the code, but it gives an error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 22
at com.company.ng.Ng.s(Ng.java:67)
This is a line in the s() method. Apparently, something happens at the moment when I translate the data, because in the loop I get bytes[z] values ​​equal to -1, -22 .. Please tell me, what is my mistake?bytes[z] = (byte) block[z][bytes[z]];

package com.company.ng;

import java.nio.ByteBuffer;

public class Ng {
    private byte[] L, R, C;
    private int bufL, bufR;
    private int[][] block = {
            {4,10,9,2,13,8,0,14,6,11,1,12,7,15,5,3},
            {14,11,4,12,6,13,15,10,2,3,8,1,0,7,5,9},
            {5,8,1,13,10,3,4,2,14,15,12,7,6,0,9,11},
            {7,13,10,1,0,8,9,15,14,4,6,12,11,2,5,3},
            {6,12,7,1,5,15,13,8,4,10,9,14,0,3,11,2},
            {4,11,10,0,7,2,1,13,3,6,8,5,9,12,15,14},
            {13,11,4,1,3,15,5,9,0,10,14,7,6,8,2,12},
            {1,15,13,0,5,7,10,4,9,2,3,14,6,11,8,12}
    };

    public String encrypt(String message, String key){
        String answer = "";
        if(key.length() < 32){
            System.out.println("Ключ дожен состоять из 32+ символов.");
        }else{
            byte[][] K = new byte[8][4];
            for(int z=0; z<K.length; z++){
                K[z] = key.substring(z*4, z*4+4).getBytes();
            }
            L = message.substring(0, 4).getBytes();
            R = message.substring(4, 8).getBytes();

            bufL = 0;
            bufR = 0;
            for(int i=0; i<30; i++){
                int j = 0;
                if(i<=23) j = i%8;
                else j = 31-i;

                bufL = ByteBuffer.wrap(L).getInt();
                bufR = ByteBuffer.wrap(L).getInt();

                int V = bufR;
                bufR = bufL ^ f(bufR, ByteBuffer.wrap(K[j]).getInt());
                bufL = V;
            }
            bufL = bufL ^ f(bufR, ByteBuffer.wrap(K[0]).getInt());

            L = ByteBuffer.allocate(4).putInt(bufL).array();
            R = ByteBuffer.allocate(4).putInt(bufR).array();
            C = new byte[8];
            System.arraycopy(L,0,C, 0, L.length);
            System.arraycopy(R,0,C, 4, L.length);
            answer = new String(C);
        }
        return answer;
    }

    private int f(int R, int K){
        int v = (R+K)%(2^32);
        v = s(v);
        v = rol11(v);

        return v;
    }
    private int s(int v){
        byte[] bytes = ByteBuffer.allocate(4).putInt(v).array();
        for(int z=0; z<bytes.length; z++){
            bytes[z] = (byte) block[z][bytes[z]];
        }
        v = ByteBuffer.wrap(bytes).getInt();

        return v;
    }
    private int rol11(int v){
        v = ((v << 11) | (v >>> 21));
        return v;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question