Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.util.Base64;
import android.util.Log;

public class Main {
    private static final String TAG = "HidingUtil";

    /**
     * A convenience method that generates a XOR key pair for a given key. It was used to generate
     * the key for {@link MainActivity#useXorStringHiding(String)} method.
     *
     * @param key The source key to use in generating the XOR key halves
     * @return a two-value string array containing both parts of the XOR key
     */
    public static String[] generateKeyXorParts(String key) {
        String[] keyParts = new String[2];

        byte[] xorRandom = new byte[key.length()];
        byte[] xorMatch = new byte[key.length()];
        byte[] keyBytes = key.getBytes();
        for (int i = 0; i < key.length(); i++) {
            xorRandom[i] = (byte) (256 * Math.random());
            xorMatch[i] = (byte) (xorRandom[i] ^ keyBytes[i]);
        }
        keyParts[0] = Base64.encodeToString(xorRandom, 0);
        keyParts[1] = Base64.encodeToString(xorMatch, 0);
        Log.i(TAG, "XOR Key Part 0: " + keyParts[0]);
        Log.i(TAG, "XOR Key Part 1: " + keyParts[1]);

        return keyParts;
    }
}